perf: vectorize partial-update bounding rect and segment encoder#94
Merged
Merged
Conversation
compute_bounding_rect was an O(W*H) pure-Python double loop run on every partial-capable upload (13.9ms @800x480, 71.6ms @1600x1200); replace with a numpy diff/any reduction (~300x). encode_segment_wire repeated the per-pixel packing loops; replace with flat np.packbits/LUT packing. Note the partial stream packs flat across row boundaries (no per-row byte padding), so the vectorized version uses flat packing, not axis=1. Verified byte-identical to the previous implementations across non-aligned widths and all color schemes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JRrm95f1qNZzDM9r2SB6KW
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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
Two hot spots on the partial-update path (
partial.py) were pure-Python per-pixel loops:compute_bounding_rect— an O(W·H) double loop run on every partial-capable upload, scanning the full width of every row even when unchanged. Measured 13.9 ms @800×480, 71.6 ms @1600×1200. Replaced with a numpy diff/anyreduction → 49 µs / 166 µs (~300×).encode_segment_wire— repeated the per-pixel packing loops. Replaced with flatnp.packbits/LUT packing.Correctness
Verified byte-identical to the previous implementations:
compute_bounding_rect: random diffs across sizes 122×250, 799×480, 127×3, 1×1, 5×4, 296×128.encode_segment_wire: all color schemes (MONO/BWRY/GRAYSCALE_4/BWGBRY/GRAYSCALE_16) across multiple crop rectangles that produce odd flat lengths.Test plan
uv run pytest -q→ 439 passedruff check,ruff format,mypyclean🤖 Generated with Claude Code