PERF: Enlarge zlib read buffer in NIfTI reader for NAS performance#1634
Open
imikejackson wants to merge 1 commit into
Open
PERF: Enlarge zlib read buffer in NIfTI reader for NAS performance#1634imikejackson wants to merge 1 commit into
imikejackson wants to merge 1 commit into
Conversation
The NIfTI reader opened files with a bare gzopen(), leaving zlib at its 8 KB default internal buffer. zlib refills from the underlying file in buffer-sized chunks, so a multi-GB .nii.gz triggered hundreds of thousands of small reads. On network attached storage each refill is a latency-bound round-trip, making reads disproportionately slow compared to a local disk. Call gzbuffer() with a 4 MiB buffer (new shared constant nifti::k_GzReadBufferSize) immediately after every gzopen(), before the first read/seek. This cuts the number of NAS round-trips by ~512x on a 2 GB file while costing only a few MiB of working memory. Larger buffers yield diminishing returns once they exceed the link's bandwidth-delay product and client read-ahead. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
17dea01 to
7a3ede2
Compare
JDuffeyBQ
reviewed
Jun 5, 2026
| * diminishing returns once the buffer exceeds the link's | ||
| * bandwidth-delay product and the client's own read-ahead. | ||
| */ | ||
| inline constexpr usize k_GzReadBufferSize = 1u << 22; // 4 MiB |
Collaborator
There was a problem hiding this comment.
This should be defined to be the type zlib expects
Comment on lines
+122
to
+124
| // Match the voxel reader's enlarged zlib buffer so the (single) header read | ||
| // is served by one large refill rather than zlib's 8 KB default. Harmless | ||
| // here since only 348 bytes are read, but keeps the two open sites consistent. |
Collaborator
There was a problem hiding this comment.
I think we should just direct to the variable's comment rather than repeating the rationale everywhere.
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 NIfTI reader opened files with a bare gzopen(), leaving zlib at its 8 KB default internal buffer. zlib refills from the underlying file in buffer-sized chunks, so a multi-GB .nii.gz triggered hundreds of thousands of small reads. On network attached storage each refill is a latency-bound round-trip, making reads disproportionately slow compared to a local disk.
Call gzbuffer() with a 4 MiB buffer (new shared constant nifti::k_GzReadBufferSize) immediately after every gzopen(), before the first read/seek. This cuts the number of NAS round-trips by ~512x on a 2 GB file while costing only a few MiB of working memory. Larger buffers yield diminishing returns once they exceed the link's bandwidth-delay product and client read-ahead.