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

[C++] Optimizations and cleanups and const correctness, oh my #3478

Merged
merged 1 commit into from
Jan 31, 2022

Conversation

jcking
Copy link
Collaborator

@jcking jcking commented Jan 6, 2022

Various changes related to optimizations, cleanup, and const correctness fixes. The optimizations are related to std::shared_ptr and getting rid of unnecessary copying when possible as we as some prerequisite work for future improvements. The cleanup is marking things as final or removing odd whitespace. The const correctness is self explanatory.

Additionally this makes LL1Analyzer final and removes its virtual methods. Its not possible for generated code to override the LL1Analyzer and having virtual in the hot path is not free, unlike Java. In C++ virtual methods effectively guarantee CPU cache misses.

Lastly it moves a few methods in the hot path to be inline, specifically Recognizer::set/getState.

@jcking jcking marked this pull request as ready for review January 14, 2022 19:53
@jcking
Copy link
Collaborator Author

jcking commented Jan 14, 2022

@mike-lischke

@mike-lischke
Copy link
Member

Passing a shared_ptr as const ref is not a good idea. I had a discussion on Stackoverflow 5 years ago, exactly because I was thinking about doing that. You are killing the shared_ptr promise to ensure a pointer is valid as long as at least one place in code holds the shared_ptr.

If you really want to improve that (what I had in mind since I wrote that code) is to pass through raw pointers and make sure there's "one source of truth" that owns the pointer (using a unique_ptr then).

@jcking
Copy link
Collaborator Author

jcking commented Jan 18, 2022

Passing a shared_ptr as const ref is not a good idea. I had a discussion on Stackoverflow 5 years ago, exactly because I was thinking about doing that. You are killing the shared_ptr promise to ensure a pointer is valid as long as at least one place in code holds the shared_ptr.

If you really want to improve that (what I had in mind since I wrote that code) is to pass through raw pointers and make sure there's "one source of truth" that owns the pointer (using a unique_ptr then).

I think I switched most to be pass by value, which avoids incrementing and decrementing the reference count when paired with std::move, increasing throughput. The other changes where I made it const reference was to copying when possible with return values, avoids incrementing/decrementing reference counts.

@mike-lischke
Copy link
Member

mike-lischke commented Jan 23, 2022

I just realised, it's the other way around: you removed passing by reference. This is totally fine then!

However, I wonder why you didn't apply that to all cases. For example: ATNConfig(Ref<ATNConfig> const& c, Ref<SemanticContext> semanticContext); you removed the const& from the second parameter. Why not from the first too?

@jcking
Copy link
Collaborator Author

jcking commented Jan 26, 2022

I just realised, it's the other way around: you removed passing by reference. This is totally fine then!

However, I wonder why you didn't apply that to all cases. For example: ATNConfig(Ref<ATNConfig> const& c, Ref<SemanticContext> semanticContext); you removed the const& from the second parameter. Why not from the first too?

The implementation of the constructor does not actually save a reference to ATNConfig, it only accesses members. Realistically it should probably just accept const ATNConfig &c but that was a larger change that I was willing to do for this specific bit.

Copy link
Member

@mike-lischke mike-lischke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parrt This patch is ready to merge.

@parrt parrt added this to the 4.10 milestone Jan 29, 2022
@parrt
Copy link
Member

parrt commented Jan 29, 2022

something is weird here with the unit test. Let me rerun them. Still looks messed up. Even Python3 says:

java.lang.RuntimeException: Missing system property:antlr-python3-python

Is it possible this PR is based on a previous version where we had the CI builds messed up? @ericvergnaud any ideas?

@jcking
Copy link
Collaborator Author

jcking commented Jan 31, 2022

Rebased off of master. Let's see what happens.

@jcking
Copy link
Collaborator Author

jcking commented Jan 31, 2022

All passed now, was due to being on an older base of master.

@parrt

@parrt parrt merged commit c41663a into antlr:master Jan 31, 2022
@parrt
Copy link
Member

parrt commented Jan 31, 2022

Thanks again for all your help!

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.

3 participants