Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid adding to closureBusy before all ATNConfig properties are set #1955

Merged
merged 1 commit into from
Oct 21, 2017

Conversation

sharwell
Copy link
Member

@sharwell sharwell commented Jul 18, 2017

Setting ATNConfig properties can change the hash code of the instance, leading to cases where the closureBusy set places objects in the wrong buckets. While this has not led to known cases of stack overflow, it has led to cases where one or more buckets contains a large number of duplicate objects, and the set's add operation goes from O(1) to O(n).

🔗 This bug was found during investigation of a report by @gkudva in #42 (comment).

Setting ATNConfig properties can change the hash code of the instance, leading
to cases where the closureBusy set places objects in the wrong buckets. While
this has not led to known cases of stack overflow, it has led to cases where
one or more buckets contains a large number of duplicate objects, and the set's
add operation goes from O(1) to O(n).
@daniellansun
Copy link

Sam ( @sharwell ), when we profile the new parser of Apache Groovy, we find closure method costs a lot of time and the performance of parsing is not good. If possible, could you release a new version of your optimized antlr4(e.g. 4.7.0.1?), which includes the improvement Avoid adding to closureBusy before all ATNConfig properties are set.

tp1

Here are grammar files of Apache Groovy:
https://github.com/apache/groovy/blob/master/src/antlr/GroovyLexer.g4
https://github.com/apache/groovy/blob/master/src/antlr/GroovyParser.g4

@parrt parrt added this to the 4.7.1 milestone Oct 21, 2017
@parrt parrt merged commit db5a0ae into antlr:master Oct 21, 2017
@parrt
Copy link
Member

parrt commented Oct 21, 2017

@danielsun1106 can youGive it a try now with latest master?

@sharwell
Copy link
Member Author

@danielsun1106 I'll try to get a build published soon with this fix included. It didn't seem like it would have a big impact for your case, but it's a possibility.

@parrt parrt added the type:bug label Oct 21, 2017
@daniellansun
Copy link

@sharwell That's great! Could you please give me some advices to improve performance of the parser further? Thanks in advance!

@parrt It's hard to switch to the reference implementation of antlr4 because of different API...

@sharwell sharwell deleted the fix-closurebusy branch October 22, 2017 04:59
@parrt
Copy link
Member

parrt commented Oct 23, 2017

Which part of the API is different?

@sharwell
Copy link
Member Author

I was on a very tight schedule, but helped @danielsun1106 set up an experiment related to this change. As I expected, this change wasn't a notable contributor to the scenario. From what I can tell, the number of users who benefit from this change is relatively small, but the impact for those cases can be substantial.

I'll work with Daniel in the coming weeks to see if further optimizations can be made to the internals of the closure operation and/or improve the ways we help users refactor their grammar to improve performance when using the profiler.

@parrt
Copy link
Member

parrt commented Oct 23, 2017

Excellent. thanks @sharwell As usual, you are the performance guru!

@daniellansun
Copy link

@parrt Here is the commit to show the difference of API.
sharwell/groovy-parser@2729139

ewanmellor added a commit to ewanmellor/antlr4 that referenced this pull request Nov 8, 2018
Avoid adding to closureBusy before all ATNConfig properties are set.

This fixes antlr#2372.

This is a port of c8805ab from the Java runtime.  That was PR antlr#1955.
ewanmellor added a commit to ewanmellor/antlr4 that referenced this pull request Nov 8, 2018
Avoid adding to closureBusy before all ATNConfig properties are set.

This fixes antlr#2372.

This is a port of c8805ab from the Java runtime.  That was PR antlr#1955.
parrt added a commit that referenced this pull request Nov 9, 2018
ewanmellor added a commit to ewanmellor/antlr4 that referenced this pull request Nov 11, 2018
Avoid adding to closureBusy before all ATNConfig properties are set.

This fixes antlr#2372.

This is a port of c8805ab from the Java runtime.  That was PR antlr#1955.
ewanmellor added a commit to ewanmellor/antlr4 that referenced this pull request Nov 15, 2018
Avoid adding to closureBusy before all ATNConfig properties are set.

This fixes antlr#2372.

This is a port of c8805ab from the Java runtime.  That was PR antlr#1955.
ewanmellor added a commit to ewanmellor/antlr4 that referenced this pull request Nov 15, 2018
Avoid adding to closureBusy before all ATNConfig properties are set.

This fixes antlr#2372.

This is a port of c8805ab from the Java runtime.  That was PR antlr#1955.
ewanmellor added a commit to ewanmellor/antlr4 that referenced this pull request Nov 15, 2018
Avoid adding to closureBusy before all ATNConfig properties are set.

This fixes antlr#2372.

This is a port of c8805ab from the Java runtime.  That was PR antlr#1955.
@ewanmellor
Copy link
Contributor

This change was ported to the Swift runtime as part of PR #2407.

parrt pushed a commit that referenced this pull request Nov 18, 2018
parrt pushed a commit that referenced this pull request Nov 18, 2018
parrt added a commit that referenced this pull request Nov 18, 2018
parrt added a commit that referenced this pull request Nov 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants