Serious performance issue with JavaScript target
See original GitHub issueI am new to antlr4 and have a relatively simple recursive grammar (with separate lexer/parser) for a project that runs in both Java and JavaScript. Everything works as expected for short expressions. For longer ones, Java works as expected with parses on the order of 50-100ms, while JavaScript takes many, many seconds for medium length expression and simply hangs and/or does not complete for longer ones. Some simple performance metrics rule out the lexing and visiting phases which take only a few milliseconds. I am at a loss as to how to investigate further…
In the demo below, the output parse-tree is identical, but while the Java parse takes 91 ms the JavaScript parse takes 46 seconds!!
If I increase the length of the string any further, the JavaScript hangs and does not complete.
Java: https://github.com/dhowe/rita4j/tree/master/src/main/java/rita/antlr JS: https://github.com/dhowe/ritajs/tree/master/grammar
Simple demo (js):
$ git clone https://github.com/dhowe/ritajs.git
$ cd ritajs && git checkout perf
$ npm install
$ node perf-test.js
OUTPUT:
[lexing] 0ms
(script (line (expr (chars Lorem ipsum dolor sit amet, ) (choice ( (wexpr (expr (chars consectetur adipiscing elit))) )) (chars morbi ullamcorper porttitor lorem, in faucibus velit ultrices nec . Curabitur convallis luctus felis, sed posuere turpis mollis quis . Suspendisse euismod vel tellus sit amet tempus . Nullam pretium tincidunt pellentesque . Vestibulum tempus eget eros non dignissim . Nullam faucibus et augue a commodo . Curabitur tellus est, elementum sit amet finibus a, posuere in nunc . In libero metus, tempor nec tincidunt eu, vulputate a ex . Aliquam id tincidunt sapien . In pharetra condimentum lacus, non congue arcu tempor nec . Nullam faucibus odio id diam dapibus volutpat sed in quam . Vivamus ex quam, efficitur sit amet ante eu, congue blandit arcu . Suspendisse molestie sit amet diam ac tristique .))) <EOF>)
[parsing] 46028ms
[visiting] 1ms
Same demo (java):
$ git clone https://github.com/dhowe/rita4j.git
$ cd rita4j && git checkout perf
$ mvn compile test-compile exec:java -Dexec.mainClass="rita.test.PerfTest" -Dexec.classpathScope=test
OUTPUT:
[lexing] 50ms
(script (line (expr (chars Lorem ipsum dolor sit amet, ) (choice ( (wexpr (expr (chars consectetur adipiscing elit))) )) (chars morbi ullamcorper porttitor lorem, in faucibus velit ultrices nec . Curabitur convallis luctus felis, sed posuere turpis mollis quis . Suspendisse euismod vel tellus sit amet tempus . Nullam pretium tincidunt pellentesque . Vestibulum tempus eget eros non dignissim . Nullam faucibus et augue a commodo . Curabitur tellus est, elementum sit amet finibus a, posuere in nunc . In libero metus, tempor nec tincidunt eu, vulputate a ex . Aliquam id tincidunt sapien . In pharetra condimentum lacus, non congue arcu tempor nec . Nullam faucibus odio id diam dapibus volutpat sed in quam . Vivamus ex quam, efficitur sit amet ante eu, congue blandit arcu . Suspendisse molestie sit amet diam ac tristique .))) <EOF>)
[parsing] 91ms
[visiting] 18ms
Any suggestions would be very much appreciated.
Issue Analytics
- State:
- Created 3 years ago
- Comments:23 (13 by maintainers)
Top GitHub Comments
3 to 4 times slower is in line with what you can expect. See https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/knucleotide.html, a benchmark which like ANTLR spends most of its time manipulating hash maps.
Can I close this?
This issue should not (imho) be closed, without at least creating separate issues, b/c there are still 2 real problems: