Skip to content

compression speed: stop LCP match search when no match can lower the cost - #20

Open
BrianPugh wants to merge 2 commits into
sisong:masterfrom
BrianPugh:perf/lcp-early-exit
Open

BrianPugh wants to merge 2 commits into
sisong:masterfrom
BrianPugh:perf/lcp-early-exit

Conversation

@BrianPugh

@BrianPugh BrianPugh commented Sep 29, 2026 •

Copy link
Copy Markdown

Hey Sisong! I'm working on some compressor-speed optimizations; if you are okay with this relatively small PR, I'll open more to follow. These are all assuming that we care more about compressor-speed and compression-ratios over memory-usage/binary-size. Let me know if this assumption is incorrect.

This PR speeds up the compressor ~1.4–1.5x (CPU time) on binary deltas and firmware, up to 3.5x on repetitive text, with byte-identical output, by stopping the LCP match search once no remaining match can lower any cost.

  • A match found by LCP has dict_pos>kShortPos, so its cost is at least
    cost[curi-1] + getSavedDictPosBit(kShortPos+1) + minDictLenBit[mLen]
    
    (minDictLenBit is the smaller of the near/far length bit counts, precomputed once).
  • The search visits curMinLcp in decreasing order, so once no mLen<=curMinLcp can reach cost[curi+mLen-1], this match and every later (shorter) one writes nothing and leaves out_maxMatchLen unchanged.
  • The check only applies below _k_kBMatchStep, so the big-match return is untouched.

On repetitive input the LCP walk used to run to its 2048 limit at most positions, and ~97% of those iterations could never lower a cost.

Benchmarks

Median CPU seconds (user+sys) of 9 runs, tinyuz -c-<dictSize> -p-1, clang -O3, Apple M3. Variants were interleaved in random order each round; in every cell the 9 runs span at most 4% of the median.

input dict 256 dict 4k dict 1m
binary delta, 0.99 MB 2.254 → 1.593 (1.42x) 2.190 → 1.482 (1.48x) 2.078 → 1.371 (1.52x)
binary deltas concatenated, 5.9 MB 10.99 → 7.70 (1.43x) 10.52 → 7.06 (1.49x) 10.12 → 6.66 (1.52x)
ESP32 firmware, 0.99 MB 0.355 → 0.258 (1.38x) 0.362 → 0.245 (1.48x) 0.455 → 0.298 (1.52x)
repetitive text, 0.97 MB 1.206 → 0.580 (2.08x) 1.221 → 0.344 (3.55x) 0.110 → 0.083 (1.32x)
random, 1 MB 0.170 → 0.172 (0.99x) 0.171 → 0.174 (0.98x) 0.214 → 0.218 (0.98x)

On random data (where the LCP walk is already short) the per-position bound check costs ~2%.

Additional Memory Consumption: one (_k_kBMatchStep)×4-byte table (2 KB) per matcher.

Byte-identity

476/476 outputs identical to the baseline (1d74ffa), and each decodes back to its input with the unchanged decoder: 17 files (real binary deltas, raw firmware, random, zeros, repetitive text, 0–65537-byte edge files) × dict {256, 1k, 3000, 4k, 64k, 1m, 16m} × {-c, -ci} × threads {1, 4}.

BrianPugh and others added 2 commits September 29, 2026 20:17
windows-latest now ships Visual Studio 2026, which lacks the v143
(VS 2022) toolset the .vcxproj files target, failing with MSB8020.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A match found by LCP has dict_pos>kShortPos, so it costs at least
cost[curi-1]+posBit(kShortPos+1)+minLenBit(mLen). The search walks
curMinLcp downward, so once every mLen<=curMinLcp (<_k_kBMatchStep)
can't reach cost[curi+mLen-1], this and all later matches write nothing
and no longer change out_maxMatchLen; return early. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@BrianPugh

Copy link
Copy Markdown
Author

stacked on #21 to fix CI

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.

1 participant