Skip to content

Commit

Permalink
Performance Optimization:
Browse files Browse the repository at this point in the history
- Avoid backtracking during the parsing of annotations.

Part of #115
  • Loading branch information
bd82 committed Jan 8, 2019
1 parent 8c92539 commit 73a888d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 40 deletions.
1 change: 1 addition & 0 deletions packages/java-parser/scripts/parse-samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let failed = 0;

const fullStartTime = new Date().getTime();
javaSampleFiles.forEach(fileDesc => {
// TODO: read the files BEFORE the benchmark started to only bench the parsing speed...
const currJavaFileString = fs.readFileSync(fileDesc.path, "utf8");
const relativePath = path.relative(__dirname, fileDesc.path);
try {
Expand Down
6 changes: 5 additions & 1 deletion packages/java-parser/scripts/single-sample-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

const javaParserChev = require("../src/index");

const input = `private int to;`;
const input = ` @RequestMapping("/product")
public Void put(String key, Object value) {
properties.put(key, value);
return null;
}`;

javaParserChev.parse(input, "classBodyDeclaration");
3 changes: 0 additions & 3 deletions packages/java-parser/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class JavaParser extends Parser {
maxLookahead: 2,
// ambiguities resolved by backtracking
ignoredIssues: {
annotation: {
OR: true
},
annotationTypeMemberDeclaration: {
OR: true
},
Expand Down
54 changes: 18 additions & 36 deletions packages/java-parser/src/productions/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,31 +238,28 @@ function defineRules($, t) {

// https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-Annotation
$.RULE("annotation", () => {
// TODO: performance optimization: implement optimized backtracking here.
$.OR([
{
GATE: $.BACKTRACK($.normalAnnotation),
ALT: () => $.SUBRULE($.normalAnnotation)
},
// "singleElementAnnotation" must appear before "markerAnnotation" due to common
// prefix.
{
GATE: $.BACKTRACK($.singleElementAnnotation),
ALT: () => $.SUBRULE($.singleElementAnnotation)
},
{ ALT: () => $.SUBRULE($.markerAnnotation) }
]);
});

// https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-NormalAnnotation
$.RULE("normalAnnotation", () => {
// Spec Deviation: The common prefix for all three annotation types was extracted to this rule.
// This was done to avoid the use of backtracking for performance reasons.
$.CONSUME(t.At);
$.SUBRULE($.typeName);
$.CONSUME(t.LBrace);

// If this optional grammar was not invoked we have a markerAnnotation
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-MarkerAnnotation
$.OPTION(() => {
$.SUBRULE($.elementValuePairList);
$.CONSUME(t.LBrace);
$.OR([
// normal annotation - https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-NormalAnnotation
{ ALT: () => $.SUBRULE($.elementValuePairList) },
// Single Element Annotation - https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-SingleElementAnnotation
{ ALT: () => $.SUBRULE($.elementValue) },
{
ALT: () => {
/* empty normal annotation contents */
}
}
]);
$.CONSUME(t.RBrace);
});
$.CONSUME(t.RBrace);
});

// https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-ElementValuePairList
Expand Down Expand Up @@ -321,21 +318,6 @@ function defineRules($, t) {
});
});

// https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-MarkerAnnotation
$.RULE("markerAnnotation", () => {
$.CONSUME(t.At);
$.SUBRULE($.typeName);
});

// https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-SingleElementAnnotation
$.RULE("singleElementAnnotation", () => {
$.CONSUME(t.At);
$.SUBRULE($.typeName);
$.CONSUME(t.LBrace);
$.SUBRULE($.elementValue);
$.CONSUME(t.RBrace);
});

// ------------------------------------
// Special optimized backtracking rules.
// ------------------------------------
Expand Down

0 comments on commit 73a888d

Please sign in to comment.