Skip to content

perf: vectorize partial-update bounding rect and segment encoder#94

Merged
g4bri3lDev merged 1 commit into
OpenDisplay:mainfrom
balloob:perf/vectorize-partial
Jul 5, 2026
Merged

perf: vectorize partial-update bounding rect and segment encoder#94
g4bri3lDev merged 1 commit into
OpenDisplay:mainfrom
balloob:perf/vectorize-partial

Conversation

@balloob

@balloob balloob commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

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/any reduction → 49 µs / 166 µs (~300×).
  • encode_segment_wire — repeated the per-pixel packing loops. Replaced with flat np.packbits/LUT packing.

Note: unlike the full-frame encoders, the partial stream packs flat across row boundaries (no per-row byte padding), so the vectorized version uses flat packing, not axis=1.

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 passed
  • Standalone byte-identical diff vs. the pre-change loop implementations
  • ruff check, ruff format, mypy clean

🤖 Generated with Claude Code

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
@balloob balloob requested a review from g4bri3lDev as a code owner July 4, 2026 06:34
@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 32.00000% with 17 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/opendisplay/partial.py 32.00% 17 Missing ⚠️

📢 Thoughts on this report? Let us know!

@g4bri3lDev g4bri3lDev merged commit 068f7f6 into OpenDisplay:main Jul 5, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants