geotiff: jit packbits_decompress#2083
Merged
Merged
Conversation
Split packbits_decompress into a numba @ngjit kernel (_packbits_decode_kernel) and a thin bytes-in / bytes-out wrapper, matching the LZW codec pattern in the same module. The kernel walks the PackBits state machine over a uint8 array with bounded writes; the wrapper sizes the output buffer using the existing decompression-bomb cap (_max_output_with_margin) and turns a cap-exceeded sentinel into the same ValueError the previous Python path raised. On a 256x256 mixed-content tile this is ~7.5x faster (~0.33 ms -> ~0.04 ms per decode). New tests in test_packbits_jit_2048.py cover empty input, single-byte runs, max literal run (128 bytes), max replicate run (128 bytes via header 0x81), the 128-byte literal/replicate boundary, the -128 no-op sentinel, the Wikipedia canonical example, truncated streams, and the bomb-cap round-trip. Existing PackBits coverage in test_features.py and test_decompression_caps.py still passes unchanged.
…2048) Rename the kernel-return local in packbits_decompress from `n` to `n_written` so the bytes-written value isn't reusing the signed-header name from the old loop. The cap-exceeded ValueError now spells out that the produced count crossed `cap` (cap = expected_size * 1.05 + 1) rather than only naming the cap, which makes the message readable without having to reread the docstring. Add a parametrised test that pins the cap boundary at three points: the exact-cap-equals-decode case (must pass), the one-byte-over case (must raise), and a tiny expected_size=1 boundary. These would catch an off-by-one regression in either direction. Existing 20 tests still pass; total is now 23.
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
packbits_decompressinto a numba@ngjitkernel (_packbits_decode_kernel) plus a thin bytes-in / bytes-out wrapper, matching the LZW codec pattern in the same module._max_output_with_margincap and turns a cap-exceeded sentinel into the sameValueErrorthe previous Python path raised, so the decompression-bomb guard still fires on adversarial input.Backends
PackBits is a CPU-only TIFF codec on the reader's bytes path. No GPU or dask code paths change.
Test plan
pytest xrspatial/geotiff/tests/test_packbits_jit_2048.py(20 new tests: empty input, single-byte literal/replicate, max 128-byte runs, 128-byte literal/replicate boundary, -128 no-op sentinel, Wikipedia canonical example, truncated streams, bomb cap)pytest xrspatial/geotiff/tests/test_compression.py test_features.py test_decompression_caps.py test_packbits_jit_2048.py(193 passed, 4 skipped)pytest xrspatial/geotiff/tests/test_fuzz_hypothesis_1661.py(15 passed; PackBits is round-tripped as part of the lossless-codec fuzz pass)Closes #2048