From e4b3bce29b7d226b70729f7b0459befad8e9dba7 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 16 Apr 2014 17:34:29 -0700 Subject: [PATCH] Minor performance optimization. --- .../parser/scanner/LocationCtxContainer.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxContainer.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxContainer.java index 59a6f5c5c54..58e6aec59ed 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxContainer.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxContainer.java @@ -7,6 +7,7 @@ * * Contributors: * Markus Schorn - initial API and implementation + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.core.parser.scanner; @@ -19,6 +20,7 @@ import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree.IASTInclusionNode; import org.eclipse.cdt.core.parser.util.CharArrayUtils; +import org.eclipse.cdt.core.parser.util.IntArray; /** * Base class for all location contexts that can contain children. @@ -51,7 +53,7 @@ class LocationCtxContainer extends LocationCtx { public void addChild(LocationCtx locationCtx) { if (fChildren == null) { - fChildren= new ArrayList(); + fChildren= new ArrayList<>(); } fChildren.add(locationCtx); } @@ -74,9 +76,9 @@ class LocationCtxContainer extends LocationCtx { public final int getSequenceNumberForOffset(int offset, boolean checkChildren) { int result= fSequenceNumber + fChildSequenceLength + offset; if (checkChildren && fChildren != null) { - for (int i= fChildren.size() - 1; i >= 0; i--) { + for (int i= fChildren.size(); --i >= 0;) { final LocationCtx child= fChildren.get(i); - if (child.fEndOffsetInParent > offset) { // Child was inserted behind the offset, adjust sequence number + if (child.fEndOffsetInParent > offset) { // Child was inserted behind the offset, adjust sequence number result -= child.getSequenceLength(); } else { return result; @@ -208,7 +210,7 @@ class LocationCtxContainer extends LocationCtx { int upper= fChildren.size(); int lower= 0; while (upper > lower) { - int middle= (upper + lower) / 2; + int middle= (upper + lower) >>> 1; LocationCtx child= fChildren.get(middle); int childSequenceNumber= child.fSequenceNumber; if (beforeReplacedChars) { @@ -254,18 +256,13 @@ class LocationCtxContainer extends LocationCtx { } private int[] computeLineOffsets() { - ArrayList offsets= new ArrayList(); final int len= fSource.getLength(); + IntArray offsets= new IntArray(len / 10); // Assuming 10 characters per line on average. for (int i = 0; i < len; i++) { - if (fSource.get(i) == '\n') { - offsets.add(new Integer(i)); - } + if (fSource.get(i) == '\n') + offsets.add(i); } - int[] result= new int[offsets.size()]; - for (int i = 0; i < result.length; i++) { - result[i]= offsets.get(i).intValue(); - } - return result; + return offsets.toArray(); } @Override