RFR: 8284880: Re-examine sun.invoke.util.Wrapper hash tables
Claes Redestad
redestad at openjdk.java.net
Thu Apr 14 11:26:27 UTC 2022
This patch examines and optimizes `Wrapper` lookups.
First wrote a few simple microbenchmarks to verify there are actual speedups from using perfect hash tables in `sun.invoke.util.Wrapper` compared to simpler lookup mechanisms (such as if-else or switch). Turns out there _is_ a speed-up for the case of `char` -> `Wrapper`, but not when mapping from `Class<?>` -> `Wrapper`, so let's drop those. The `forPrimitiveType` case didn't use the `FROM_CHAR` table for some reason, which is remedied.
Micros show benefits across the board for warmed up case:
Baseline, OOTB
Benchmark Mode Cnt Score Error Units
Wrappers.forBasicType avgt 5 14.387 ? 0.127 ns/op
Wrappers.forPrimitive avgt 5 38.818 ? 0.592 ns/op
Wrappers.forPrimitiveType avgt 5 26.085 ? 2.291 ns/op
Wrappers.forWrapper avgt 5 44.459 ? 1.635 ns/op
Patch, OOTB
Benchmark Mode Cnt Score Error Units
Wrappers.forBasicType avgt 5 14.357 ? 0.133 ns/op
Wrappers.forPrimitive avgt 5 23.930 ? 0.071 ns/op
Wrappers.forPrimitiveType avgt 5 14.343 ? 0.017 ns/op
Wrappers.forWrapper avgt 5 27.622 ? 0.022 ns/op
For `-Xint` case (`Wrapper` use is prominent during warmup, e.g., when spinning up of MHs) there are decent or even great wins in all cases but `forPrimitiveType` - which was changed from a simple switch to use the hash lookup. Since the interpreter penalty is small in absolute terms and the win on JITed code is significant this seems like a reasonable trade-off:
Baseline, -Xint
Benchmark Mode Cnt Score Error Units
Wrappers.forBasicType avgt 5 1246.144 ? 149.933 ns/op
Wrappers.forPrimitive avgt 5 4955.297 ? 329.869 ns/op
Wrappers.forPrimitiveType avgt 5 716.840 ? 62.568 ns/op
Wrappers.forWrapper avgt 5 5774.700 ? 367.627 ns/op
Patch, -Xint
Benchmark Mode Cnt Score Error Units
Wrappers.forBasicType avgt 5 1068.096 ? 101.728 ns/op
Wrappers.forPrimitive avgt 5 1146.670 ? 59.142 ns/op
Wrappers.forPrimitiveType avgt 5 998.037 ? 118.144 ns/op
Wrappers.forWrapper avgt 5 3581.226 ? 20.167 ns/op
-------------
Commit messages:
- Revert accidental changes to MethodType
- Fix typo
- Missed some changes refactoring, import DontInline
- Merge branch 'master' into wrappers
- Cleanup, use mask instead of modulo
- Cleanup, add forPrimitiveType micro, reinstantiate char hashing
- Add required add-exports to BuildMicrobenchmark.gmk
- Fix imports
- Tune sun.invoke.util.Wrapper accessors
Changes: https://git.openjdk.java.net/jdk/pull/8242/files
Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=8242&range=00
Issue: https://bugs.openjdk.java.net/browse/JDK-8284880
Stats: 256 lines in 3 files changed: 157 ins; 59 del; 40 mod
Patch: https://git.openjdk.java.net/jdk/pull/8242.diff
Fetch: git fetch https://git.openjdk.java.net/jdk pull/8242/head:pull/8242
PR: https://git.openjdk.java.net/jdk/pull/8242
More information about the build-dev
mailing list