-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
private static final VarHandle INT = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); | ||
private static final VarHandle SHORT = MethodHandles.byteArrayViewVarHandle(short[].class, ByteOrder.LITTLE_ENDIAN); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.