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

Fix UnsafeApi arrayBaseOffset #333

Closed

Conversation

pveentjer
Copy link
Contributor

@pveentjer pveentjer commented Feb 25, 2025

With JDK 25 the Unsafe function arrayBaseOffset changed the return type from int to long.

This PR fixed the UnsafeAPI so that at runtime it is determined which Unsafe method is available using INVOKE_DYNAMIC and then calls it.

There is no API change; an int will always be returned but a guard is added on JDK 25+ to ensure that the returned long doesn't exceed int min/max.

Benchmarks results

INVOKE_DYNAMIC
Benchmark                                                Mode  Cnt   Score    Error   Units
UnsafeArrayBaseOffsetBenchmark.jdk23                     avgt    3   9.267 ±  4.050   ns/op
UnsafeArrayBaseOffsetBenchmark.jdk23:gc.alloc.rate       avgt    3   0.001 ±  0.001  MB/sec
UnsafeArrayBaseOffsetBenchmark.jdk23:gc.alloc.rate.norm  avgt    3  ≈ 10⁻⁵             B/op
UnsafeArrayBaseOffsetBenchmark.jdk23:gc.count            avgt    3     ≈ 0           counts
UnsafeArrayBaseOffsetBenchmark.jdk25                     avgt    3   9.110 ±  5.813   ns/op
UnsafeArrayBaseOffsetBenchmark.jdk25:gc.alloc.rate       avgt    3   0.001 ±  0.001  MB/sec
UnsafeArrayBaseOffsetBenchmark.jdk25:gc.alloc.rate.norm  avgt    3  ≈ 10⁻⁵             B/op
UnsafeArrayBaseOffsetBenchmark.jdk25:gc.count            avgt    3     ≈ 0           counts

And the reflection based solution that currently is in master:

Benchmark                                                Mode  Cnt     Score     Error   Units
UnsafeArrayBaseOffsetBenchmark.jdk23                     avgt    3  1013.438 ± 113.517   ns/op
UnsafeArrayBaseOffsetBenchmark.jdk23:gc.alloc.rate       avgt    3   158.094 ±  17.759  MB/sec
UnsafeArrayBaseOffsetBenchmark.jdk23:gc.alloc.rate.norm  avgt    3   168.001 ±   0.001    B/op
UnsafeArrayBaseOffsetBenchmark.jdk23:gc.count            avgt    3    17.000            counts
UnsafeArrayBaseOffsetBenchmark.jdk23:gc.time             avgt    3    16.000                ms
UnsafeArrayBaseOffsetBenchmark.jdk25                     avgt    3   824.205 ±  12.531   ns/op
UnsafeArrayBaseOffsetBenchmark.jdk25:gc.alloc.rate       avgt    3   194.384 ±   2.860  MB/sec
UnsafeArrayBaseOffsetBenchmark.jdk25:gc.alloc.rate.norm  avgt    3   167.998 ±   0.078    B/op
UnsafeArrayBaseOffsetBenchmark.jdk25:gc.count            avgt    3    20.000            counts
UnsafeArrayBaseOffsetBenchmark.jdk25:gc.time             avgt    3    17.000                ms

@pveentjer pveentjer changed the title Fix unsafe api array class base offset Fix UnsafeApi arrayBaseOffset Feb 25, 2025
@vyazelenko
Copy link
Contributor

Fixed with 21d5603.

@vyazelenko vyazelenko closed this Feb 25, 2025
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