Skip to content

Minor speed up using MethodHandles in DataAccess - #3005

Merged
karussell merged 10 commits into
masterfrom
method_handle_again
May 15, 2024
Merged

karussell merged 10 commits into
masterfrom
method_handle_again

Conversation

@karussell

@karussell karussell commented May 12, 2024 •

Copy link
Copy Markdown
Member

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
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
Copy Markdown
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

@karussell karussell May 13, 2024 •

Copy link
Copy Markdown
Member Author

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.

@otbutz otbutz May 13, 2024 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
@karussell
karussell deleted the method_handle_again branch May 15, 2024 08:41

This branch was previously deployed

1 inactive deployment
benchmarks — 96ebe0be Deployed May 14, 2024 by karussell
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