On Thu, 14 Apr 2022 11:19:04 GMT, Claes Redestad <redes...@openjdk.org> wrote:
> 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 Build change looks ok. make/test/BuildMicrobenchmark.gmk line 97: > 95: SRC := $(MICROBENCHMARK_SRC), \ > 96: BIN := $(MICROBENCHMARK_CLASSES), \ > 97: JAVAC_FLAGS := --add-exports java.base/sun.security.util=ALL-UNNAMED > --add-exports java.base/sun.invoke.util=ALL-UNNAMED, \ Suggestion: JAVAC_FLAGS := --add-exports java.base/sun.security.util=ALL-UNNAMED \ --add-exports java.base/sun.invoke.util=ALL-UNNAMED, \ ------------- Marked as reviewed by erikj (Reviewer). PR: https://git.openjdk.java.net/jdk/pull/8242