geotiff: jit packbits_compress (#2049)#2081
Merged
Merged
Conversation
PackBits encode was the last writer-side codec helper still running in pure Python. The kernel matches the shape of the existing lzw_compress path: an @ngjit function operates on preallocated uint8 buffers, and a thin bytes-in / bytes-out wrapper takes care of the conversion. Output sizing uses the TIFF spec bound (2 * src_len + 1). On 1 MB strips the JIT version runs at 0.6-1.7 ms/call versus 30-49 ms/call for the previous Python loop, a 30-50x speedup.
Self-review follow-up to #2081. The original kernel docstring described the worst case as "one literal header per input byte", which is not actually achievable: the encoder packs up to 128 bytes under a single literal header, so the tight upper bound is src_len + ceil(src_len / 128) + 1. The 2 * src_len + 1 allocation in the wrapper is still a safe overestimate; the comments now say so explicitly. Adds three tests: a golden literal-encoding check on the raw kernel, a parametrized buffer-cap invariant over the regime-boundary inputs, and a random sweep that re-checks the cap on 0..4096-byte streams.
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.
Closes #2049.
Summary
packbits_compressto dispatch into an@ngjitkernel that writes into a preallocatednp.uint8buffer, matching the shape oflzw_compress/_lzw_encode_kernel.2 * src_len + 1(the TIFF spec bound for PackBits: a degenerate stream fills a fresh literal header for every two input bytes).bytesand returnsbytes.Performance
Encode rate on 1 MB strips, single thread, warm JIT cache:
Backend coverage
PackBits is a CPU codec only; no GPU or dask path. The existing
_compression_tagdispatcher in_writer.pyis unchanged.Test plan
TestPackBitsround-trip cases still pass.test_packbits_jit_2049.pycovers regime boundaries: length 0, 1, 2, 128, 129; alternating patterns; runs at start / end; mixed; 4 random seeds; large all-zero buffer; direct kernel checks.Refs #2048 (decode-side companion).