Skip to content

Commit

Permalink
Optimize processEarlyKeywords TokenStream iteration
Browse files Browse the repository at this point in the history
Iterate the TokenStream (LinkedList implementation) using an iterator where possible, reducing the amount of index-based lookups to a minimum (once per keyword instead of once per token in the TokenStream).
  • Loading branch information
Pieter12345 committed Feb 1, 2024
1 parent 70a74f8 commit df05b89
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/main/java/com/laytonsmith/core/MethodScriptCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2940,25 +2940,30 @@ private static void processLateKeywords(ParseTree tree, Environment env, Set<Con

@SuppressWarnings("ThrowableResultIgnored")
private static void processEarlyKeywords(TokenStream stream, Environment env, Set<ConfigCompileException> compileErrors) {
for(int i = 0; i < stream.size(); i++) {
Token token = stream.get(i);

// Some keywords look like function names, we need those too.
if(token.type != TType.KEYWORD && token.type != TType.FUNC_NAME) {
continue;
}
Token token;
for(ListIterator<Token> it = stream.listIterator(); it.hasNext(); ) {
int ind = it.nextIndex();
token = it.next();

EarlyBindingKeyword keyword = KeywordList.getEarlyBindingKeywordByName(token.val());
if(keyword == null) {
continue;
}

// Now that all the children of the rest of the chain are processed, we can do the processing of this level.
int newInd;
try {
i = keyword.process(stream, env, i);
newInd = keyword.process(stream, env, ind);
} catch (ConfigCompileException ex) {
compileErrors.add(ex);
continue;
}

// Create iterator at new index. Required in all cases to prevent ConcurrentModificationExceptions.
if(newInd >= stream.size()) {
break;
}
it = stream.listIterator(newInd + 1); // +1 to let the next .next() call select that index.
}
}

Expand Down

0 comments on commit df05b89

Please sign in to comment.