8000 Minor speed up using MethodHandles in DataAccess by karussell · Pull Request #3005 · graphhopper/graphhopper · GitHub
[go: up one dir, main page]

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

Minor speed up using MethodHandles in DataAccess #3005

Merged
merged 10 commits into from
May 15, 2024
Merged

Conversation

karussell
Copy link
Member
@karussell karussell commented May 12, 2024

Speedup is not more than 5% for e.g. routingCH.

This lower level approach is also used in DataInput/OutputStream so it should be production ready.

Tried also with "theUnsafe" directly, but it wasn't faster.

@karussell karussell added this to the 10.0 milestone May 12, 2024
@karussell karussell changed the title Minor speed up using MethodHandles Minor speed up using MethodHandles in DataAccess May 12, 2024
8000 Comment on lines 39 to 40
private static final VarHandle INT = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN);
private static final VarHandle SHORT = MethodHandles.byteArrayViewVarHandle(short[].class, ByteOrder.LITTLE_ENDIAN);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private static final VarHandle INT = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN);
private static final VarHandle SHORT = MethodHandles.byteArrayViewVarHandle(short[].class, ByteOrder.LITTLE_ENDIAN);
private static final VarHandle INT = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN).withInvokeExactBehavior();
private static final VarHandle SHORT = MethodHandles.byteArrayViewVarHandle(short[].class, ByteOrder.LITTLE_ENDIAN).withInvokeExactBehavior();

https://bugs.openjdk.org/browse/JDK-8255375

Copy link
Member Author
@karussell karussell May 13, 2024

Choose a reason for hiding this comment

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

Can you explain the benefit? I do not understand it from the provided link.

Copy link
Contributor
@otbutz otbutz May 13, 2024

Choose a reason for hiding this comment

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

The VarHandle has some built-in logic to handle casts. This has some overhead and tends to be a silent performance problem. The withInvokeExactBehavior() disables the casts and instead throws an error if there's a type mismatch.

e.g. without the exact behavior this would work, but with degraded performance:

short value = 5;
INT.set(segments[bufferIndex], index, value);

tl;dr: it doesn't matter as long as the code is correct. But it's hard to tell that there's a cast happening behind the scenes without the option.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I do not fully understand this but it looks like this check is only done at the creation of the VarHandle to avoid affecting performance. Have included this in this PR.

@karussell karussell merged commit 10b0b97 into master May 15, 2024
4 checks passed
@karussell karussell deleted the method_handle_again branch May 15, 2024 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0