docs: zero-copy mmap-vs-jni story, JMH-backed#101
Merged
Conversation
Adds benchmark/.../LargeFileBenchmark, a JMH benchmark comparing this library's mmap + MemorySegment path (with/without posix_madvise(WILLNEED)) against zstd-jni's classic streaming API, at sizes from 4 MiB to 10 GiB - replacing the ad hoc System.nanoTime scratch harnesses that produced the previously-committed numbers in docs/zero-copy.md. Each @benchmark invocation is one full-file compression, so this uses Mode.SingleShotTime (JMH times individual invocations) rather than the Throughput mode the other benchmarks use for tight loops. The source file for a given size is generated once and cached under the system temp directory, shared across all three variants' JMH forks rather than rewritten per variant. Fixes a real bug the switch to JMH surfaced: the mmap path was bouncing compressed output through a heap byte[] before handing it to the sink, defeating the zero-copy point. Now uses a direct ByteBuffer view over the native destination segment straight into a WritableByteChannel sink. Verified byte-for-byte round-trip against the independent zstd CLI. Also adds integration-tests/.../LargeMemoryMappedFileTest, a disabled-by- default test proving the library's >2 GiB memory-mapping capability claim: the classic ByteBuffer-based FileChannel.map overload rejects a file past Integer.MAX_VALUE, the long-indexed MemorySegment overload maps it in one call and addresses bytes past the boundary correctly, and the whole file round-trips through this library's zero-copy streaming compressor - plus a companion test showing zstd-jni's stream API can still compress a file this large (just not zero-copy map it) and that the frame it produces decodes correctly through this library. Rewrites docs/zero-copy.md's throughput section with the corrected, JMH-backed numbers (time + -prof gc allocation, full reference table with min-max ranges since JMH's 99.9% CI is unstable at n=3, machine specs, reproduce command). The corrected data confirms the shape of the earlier ad hoc finding - mmap ahead at medium sizes, the madvise hint mattering more at 10 GiB - but at roughly a tenth the magnitude the ad hoc timing loops reported, plus a previously-unseen small reversal at 2.25-4 GiB where the WILLNEED hint costs more than it saves. Adds an "Is the extra code worth it?" section arguing the zero-copy MemorySegment path is real but ~4x more code than the easy ZstdOutputStream API, and its throughput edge is only decisive at small files - at large scale it's a few percent either way, which is where the "no 2 GiB cap" capability claim (not throughput) becomes the real argument for the extra code. Independently reviewed: verified the mmap zero-copy fix round-trips byte-for-byte via the zstd CLI, verified (empirically, by reproducing the data generator and compressing both variants) that the benchmark's repeated-8-MiB-chunk source data doesn't inflate the measured compression ratio versus continuously-varying data, and fixed a real 3x-redundant-write inefficiency in the benchmark's own setup by caching the generated source file per size instead of regenerating it per variant. Closes #95.
dfa1
force-pushed
the
feature/large-file-benchmark
branch
from
July 24, 2026 07:26
fd74188 to
a3a722a
Compare
Owner
Author
|
Ran an independent review over the full diff and applied the findings that survived verification:
Also verified two other candidates that came back clean (no change needed):
Full build/tests/checkstyle/javadoc all green. Pushed as |
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.
Summary
benchmark/.../LargeFileBenchmark, a JMH benchmark comparing this library's mmap+MemorySegmentpath (with/withoutposix_madvise(WILLNEED)) againstzstd-jni's classic streaming API, at sizes from 4 MiB to 10 GiB — replacing the ad hocSystem.nanoTimescratch harnesses that produced the previously-committed numbers indocs/zero-copy.md.byte[]before handing it to the sink, defeating the zero-copy point. Now uses a directByteBufferview over the nativedstsegment + aWritableByteChannelsink. Verified byte-for-byte round-trip against the independentzstdCLI.integration-tests/.../LargeMemoryMappedFileTest, a disabled-by-default test proving the library's >2 GiB memory-mapping capability claim (gated behind-Dzstd.test.large=true, too heavy for CI).docs/zero-copy.mdwith the corrected, JMH-backed numbers (time +-prof gcallocation, full reference table with min–max ranges since JMH's 99.9% CI is unstable atn=3, machine specs, reproduce command), an honest account of what the ad hoc harness got wrong, and a new "Is the extra code worth it?" section.Closes #95.
What the real numbers show (vs. the ad hoc ones they replace)
The corrected data confirms the shape of the earlier finding — mmap ahead at medium sizes, the
madvisehint mattering more at 10 GiB — but at roughly a tenth the magnitude the ad hoc timing loops reported (4–5% swings at 10 GiB, not 24–45%). It also surfaces something the ad hoc numbers missed entirely: at 2.25–4 GiB, theWILLNEEDhint is measurably slower than no hint at all, only becoming a net win again at 10 GiB. The one clean, decisive result across the board: at 4 MiB, mmap beats zstd-jni by ~2x — every measured iteration faster than every zstd-jni iteration — driven by fixed per-call overhead, not throughput.Net conclusion for the new "Is the extra code worth it?" section: the zero-copy path is ~4x the code of the simple
ZstdOutputStreamAPI, and its throughput edge is decisive only at small files — at large scale it's a few percent either way. The real argument for the extra code at scale is the unconditional >2 GiB capability claim, not throughput.Test plan
./mvnw -pl zstd,integration-tests,benchmark -am clean verify— builds and tests clean./mvnw -pl benchmark validate/./mvnw -pl zstd validate javadoc:javadoc— checkstyle/javadoc cleanLargeFileBenchmarkvariants at small size (-p size=4194304 -wi 1 -i 1 -f 1)zstdCLI-prof gc) to completion; numbers written into the docs are from that real run, not fabricated🤖 Generated with Claude Code