Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Kucera2008-02-12 16:48:44 +0000
committerMike Kucera2008-02-12 16:48:44 +0000
commit45dc1738ed273f7f272267d4afa07b228d5e8bf9 (patch)
tree097232c2d693923f81d6700dc1eb77d465018c0b /core/org.eclipse.cdt.core/parser
parent5688a0ac9f9482b9d70252275268ed6cd60b1ff5 (diff)
downloadorg.eclipse.cdt-45dc1738ed273f7f272267d4afa07b228d5e8bf9.tar.gz
org.eclipse.cdt-45dc1738ed273f7f272267d4afa07b228d5e8bf9.tar.xz
org.eclipse.cdt-45dc1738ed273f7f272267d4afa07b228d5e8bf9.zip
cleaned up TemplateParameterManager a bit
Diffstat (limited to 'core/org.eclipse.cdt.core/parser')
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java24
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TemplateParameterManager.java69
2 files changed, 48 insertions, 45 deletions
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java
index 047110410fd..48f2efbb731 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java
@@ -175,7 +175,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
protected CPPASTTranslationUnit translationUnit;
- private static class ScopeStack {
+ private final static class ScopeStack {
private int[] stack;
private int index = -1;
@@ -190,27 +190,28 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
stack = newStack;
}
- final public void push(int i) {
+ public void push(int i) {
if (++index == stack.length)
grow();
stack[index] = i;
}
- final public int pop() {
+ public int pop() {
if (index >= 0)
return stack[index--];
return -1;
}
- final public int peek() {
+ public int peek() {
if (index >= 0)
return stack[index];
return -1;
}
- final public int size() {
+ public int size() {
return index + 1;
}
+
}
/**
@@ -822,8 +823,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @param expression
* @throws BacktrackException
*/
- protected IASTExpression multiplicativeExpression()
- throws BacktrackException, EndOfFileException {
+ protected IASTExpression multiplicativeExpression() throws BacktrackException, EndOfFileException {
IASTExpression firstExpression = pmExpression();
for (;;) {
switch (LT(1)) {
@@ -858,8 +858,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @param expression
* @throws BacktrackException
*/
- protected IASTExpression pmExpression() throws EndOfFileException,
- BacktrackException {
+ protected IASTExpression pmExpression() throws EndOfFileException, BacktrackException {
IASTExpression firstExpression = castExpression();
for (;;) {
@@ -890,15 +889,16 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
/**
* castExpression : unaryExpression | "(" typeId ")" castExpression
*/
- protected IASTExpression castExpression() throws EndOfFileException,
- BacktrackException {
+ protected IASTExpression castExpression() throws EndOfFileException, BacktrackException {
// TO DO: we need proper symbol checkint to ensure type name
if (LT(1) == IToken.tLPAREN) {
IToken la = LA(1);
int startingOffset = la.getOffset();
IToken mark = mark();
consume();
- if (templateIdScopes.size() > 0) { templateIdScopes.push(IToken.tLPAREN); }
+
+ if (templateIdScopes.size() > 0)
+ templateIdScopes.push(IToken.tLPAREN);
boolean popped = false;
IASTTypeId typeId = null;
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TemplateParameterManager.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TemplateParameterManager.java
index 9efa9fd8081..b97c2626f31 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TemplateParameterManager.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TemplateParameterManager.java
@@ -14,66 +14,72 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import org.eclipse.cdt.core.dom.ast.IASTNode;
-public final class TemplateParameterManager
-{
- protected void reset()
- {
- list = Collections.EMPTY_LIST;
+/**
+ * Manages lists of template parameter nodes during the parsing
+ * of names. The purpose of this class is performance, as these
+ * lists are managed using lazy initialization and object pooling.
+ * This class is basically as substitute for List<List<IASTNode>>.
+ *
+ * When using the object pool code must be wrapped in a try-finally
+ * block to ensure the object is returned to the pool.
+ *
+ * TODO: How much of a performance improvement are we talking about here?
+ * It might make sense just to get rid of this class. The extra complexity
+ * might not be worth it.
+ */
+public final class TemplateParameterManager {
+
+ protected void reset() {
+ list = Collections.emptyList();
emptySegmentCount = 0;
}
- private TemplateParameterManager(int i)
- {
+ private TemplateParameterManager(int i) {
reset();
counterId = i;
}
private final int counterId;
- private List list;
+ private List<List<IASTNode>> list;
private int emptySegmentCount;
- public List getTemplateArgumentsList()
- {
+ public List<List<IASTNode>> getTemplateArgumentsList() {
return list;
}
- public void addSegment( List inputSegment )
- {
- if( inputSegment == null )
- {
- if( list == Collections.EMPTY_LIST )
+ public void addSegment(List<IASTNode> inputSegment) {
+ // avoid creating an actual ArrayList instance for as long as possible
+ if(inputSegment == null) {
+ if(list.isEmpty())
++emptySegmentCount;
else
list.add( null );
}
- else
- {
- if( list == Collections.EMPTY_LIST )
- {
- list = new ArrayList();
+ else {
+ if(list.isEmpty()) {
+ list = new ArrayList<List<IASTNode>>();
for( int i = 0; i < emptySegmentCount; ++i )
list.add( null );
}
list.add( inputSegment );
}
}
+
+
+ // An object pool
private static final int NUMBER_OF_INSTANCES = 8;
private static final boolean [] instancesUsed = new boolean[ NUMBER_OF_INSTANCES ];
private static final TemplateParameterManager [] counters = new TemplateParameterManager[ NUMBER_OF_INSTANCES ];
private static int counter = 8;
- static
- {
- for( int i = 0; i < NUMBER_OF_INSTANCES; ++i )
- {
- instancesUsed[ i ] = false;
+ static {
+ for( int i = 0; i < NUMBER_OF_INSTANCES; ++i ) {
counters[ i ] = new TemplateParameterManager( i );
}
}
- /**
- * @return
- */
+
public synchronized static TemplateParameterManager getInstance() {
int index = findFreeCounter();
if( index == -1 )
@@ -82,16 +88,13 @@ public final class TemplateParameterManager
return counters[ index ];
}
- public synchronized static void returnInstance( TemplateParameterManager c )
- {
+ public synchronized static void returnInstance( TemplateParameterManager c ) {
if( c.counterId > 0 && c.counterId < NUMBER_OF_INSTANCES )
instancesUsed[ c.counterId ] = false;
c.reset();
}
- /**
- * @return
- */
+
private static int findFreeCounter() {
for( int i = 0; i < NUMBER_OF_INSTANCES; ++i )
if( instancesUsed[i] == false )

Back to the top