Repository navigation
Speed up the safe Java implementation on Java 9+ with a multi-release JAR - #189
Open
Dominik-O22 wants to merge 1 commit into
Open
Dominik-O22 wants to merge 1 commit into
Dominik-O22 wants to merge 1 commit into
Conversation
… JAR The safe LZ4 and xxHash implementations read and compare one byte at a time, because Java 7 has no bounds-checked multi-byte array access. LZ4Factory.unsafeInstance() returns the safe instance, so this is the Java path for everyone without the native library. Move the byte-array primitives of SafeUtils and LZ4SafeUtils into net.jpountz.util.ByteArrays and add a Java 9+ variant of that class in META-INF/versions/9. It uses byte-array-view VarHandles, Arrays.mismatch, and the overlapping-copy algorithm of LZ4UnsafeUtils, so every access stays bounds checked. Java 7 and 8 keep the existing code. The build compiles src/java9 with --release 9 and marks the JAR Multi-Release. Failsafe reruns the tests against the JAR so the Java 9+ class is tested too. bnd no longer writes per-release OSGi manifests, whose empty Require-Capability header Felix and Equinox reject, and the JaCoCo report skips META-INF/versions. Closes yawkat#35 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The safe LZ4 and xxHash implementations read and compare one byte at a time, because Java 7 has no bounds-checked way to read several bytes at once. Since
unsafeInstance()returns the safe instance, this is the Java code everyone without the native library runs. #35 asked for Java 9 APIs here, and you said a multi-release JAR is fine as long as Java 7 keeps working.On JDK 11 to 25, LZ4 compression gets 1.25-1.37x faster, decompression 1.16-1.45x, XXH32 1.7-2.2x and XXH64 3-4.4x. JDK 7 and 8 run the same code as before.
Change
SafeUtilsandLZ4SafeUtilsmove into a new internal classnet.jpountz.util.ByteArrays, with the existing byte-by-byte code moved over unchanged.src/java9/net/jpountz/util/ByteArrays.javais the Java 9+ variant, compiled with--release 9intoMETA-INF/versions/9. It uses byte-array-viewVarHandles,Arrays.mismatch, and the overlapping-copy algorithm ofLZ4UnsafeUtils, so every access is still bounds checked and nothing usesUnsafe.SafeUtilsandLZ4SafeUtilsstay single-source and delegate. A future fix tocheckRangeorencodeSequencecan't miss Java 9+.target/classes, whereMETA-INF/versionsdoesn't apply. Failsafe reruns the same tests against the JAR, so the Java 9+ class is tested too.MultiReleaseJarTestchecks that the JAR serves the Java 9ByteArraysand that both variants declare the same public methods.OverlappingMatchTestdecodes hand-built blocks with match distances 1 to 12 and lengths 4 to 40 on every decompressor, since the new copy handles short distances specially.META-INF/versions/9/OSGI-INF/MANIFEST.MFwith an emptyRequire-Capability:, becauselz4-java.bndremoves that header. Felix 7.0.5 and Equinox 3.23 both refuse such a bundle.-jpms-multi-release: falseturns those manifests off;java.lang.invoke, the only new import, comes from the boot loader anyway.MultiReleaseJarTestfails if an empty header comes back.META-INF/versions/**.-Dit.test='!net/jpountz/fuzz/*', because failsafe reruns the fuzz regression tests.Measurements
JMH on a Ryzen 7 9850X3D, pinned to 2 cores,
safeInstance(), data split into 64 KiB blocks.book1is from the Calgary corpus insrc/test-resources;jsonis 1 MiB of synthetic JSON log lines. Each cell is the geometric mean over both data sets. JDK 11 and 25 are two alternating base/JAR pairs each; JDK 8, 17 and 21 are one pair. In every JDK 9+ cell the base and new ranges do not overlap.JDK 8 loads the same classes on both sides, so its column shows the noise of a single run. The JSON data gains more than
book1, likely because its matches are longer: safe decompression 1.35x vs 1.11x on JDK 25.Throughput in MB/s on JDK 25, against the other implementations:
unsafeInsecureInstance())The speedup comes from executing fewer instructions. With
-prof perfnormon JDK 25, instructions per operation drop by the same factor as cycles in every benchmark (fast compressor 0.68x vs 0.69x, XXH64 0.19x vs 0.19x), and IPC stays between 3.4 and 4.8 on both sides.Verification
./mvnw verify -Pcoveragepasses, running the whole suite againsttarget/classesand again against the JAR. So does the riscv64 leg's flag set with no JDK 7 toolchain.mainJAR and this JAR side by side and made about 600k comparisons over random inputs and mutations on JDK 11, 17 and 25:ByteArrays: safe and fast decompressors, fast and HC compressors, XXH64. 0.85M to 148M executions each, no findings.Open questions for you
VarHandleaccesses throwIndexOutOfBoundsExceptionon JDK 11 andArrayIndexOutOfBoundsExceptionon 25. The one place that catches it now catches the superclass, and the differential test found no difference on 11. A JDK 11 test leg would keep it that way, if you want one.ByteArrays, sincesrc/java9isn't a project source root.Multi-Release: truesilently fall back to the Java 7 code. That's worth a line in the release notes.JMH benchmark
Run with
-Dcalgary.dir=src/test-resources/calgaryand the JAR under test on the classpath.Created with Claude Opus 5.5 in Claude Code.