question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serious performance issue with JavaScript target

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:23 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
ericvergnaudcommented, Feb 15, 2021

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?

0reactions
dhowecommented, Mar 4, 2021

This issue should not (imho) be closed, without at least creating separate issues, b/c there are still 2 real problems:

  1. There is huge bug in the current public release for JS, and no good workaround
  2. The JS releases do not support the most common/basic workflows (as described above)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix target=”_blank” links: a security and performance ...
The second approach is to use JavaScript to fix this issue. Use this when you're managing a CMS or if there are too...
Read more >
Performance and security of target=_blank links with rel ...
Mathias Bynens have demonstrated it very clearly: using a target="_blank" attribute on hypertext links allows the new opened web page to trigger ...
Read more >
Don't Use The Target="_Blank" Link Attribute In These Cases
The target=”_blank” link attribute is risky and opens a website to security and performance issues. Google's Web.dev page on the risks of ...
Read more >
The 10 Most Common JavaScript Issues Developers Face
The common errors that developers make while coding in JavaScript include mistaken thinking about how the "this" keyword works, incorrect assumptions about ...
Read more >
Understanding the Performance Impact of Generated JavaScript
This post is all about the technical prep work needed to ship a 0 bug reported major issue. Maintaining release stability before refactoring....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found