Skip to content

Optimize safe encode/decode hot paths - #210

Draft
PSeitz with Copilot wants to merge 5 commits into
mainfrom
copilot/find-performance-improvements
Draft

Optimize safe encode/decode hot paths#210
PSeitz with Copilot wants to merge 5 commits into
mainfrom
copilot/find-performance-improvements

Conversation

Copilot AI commented Mar 14, 2026

Copy link
Copy Markdown

Two targeted improvements to the safe (default) code paths, each eliminating unnecessary work from hot loops.

  • Safe decompression hot loop: direct offset read — Replace read_u16(input, &mut literal_length.clone())? with u16::from_le_bytes([input[literal_length], input[literal_length + 1]]). Since input is &[u8; 16] and literal_length is 0–14 (enforced by does_token_fit), this eliminates a clone, function call, .get() bounds check, and an unreachable ? error path from the innermost loop.

  • Safe compression literal copy: enable actual wild copy — The safe copy_literals_wild was passing an exactly-sized slice, defeating the wild copy optimization entirely. Now passes len + 8 bytes so slice_copy hits efficient fixed-size double_copy_trick paths for small literals, matching what the unsafe path already does. Bounds safety: MFLIMIT guarantees ≥12 bytes of input headroom; get_maximum_output_size margin covers output.

// Before (decompress hot loop) — clone + function call + error path
let offset = read_u16(input, &mut literal_length.clone())? as usize;

// After — direct indexing, provably in bounds
let offset = u16::from_le_bytes([input[literal_length], input[literal_length + 1]]) as usize;

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/main--/rust-lz-fear/commits/HEAD
    • Triggering command: /home/REDACTED/.rustup/toolchains/stable-x86_64-REDACTED-linux-gnu/bin/cargo /home/REDACTED/.rustup/toolchains/stable-x86_64-REDACTED-linux-gnu/bin/cargo test (http block)

If you need me to access, download, or install something from one of these locations, you can either:


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits March 14, 2026 14:09
Co-authored-by: PSeitz <1109503+PSeitz@users.noreply.github.com>
Co-authored-by: PSeitz <1109503+PSeitz@users.noreply.github.com>
… literal copy

1. decompress_safe.rs: Replace clone + read_u16() call with direct array
   indexing in the hot loop. Since input is &[u8; 16] and literal_length
   is 0-14, the indices are provably in bounds. This eliminates a clone,
   function call, bounds check, and error handling path from the hot loop.

2. compress.rs: Pass a wider slice (+8 bytes) to extend_from_slice_wild
   in the safe-encode path, enabling efficient fixed-size copy paths in
   slice_copy for small literals. This matches the unsafe version's
   behavior of unconditionally copying 8/16/24 bytes. Safe because MFLIMIT
   guarantees sufficient input bytes and get_maximum_output_size provides
   sufficient output capacity.

Co-authored-by: PSeitz <1109503+PSeitz@users.noreply.github.com>
Co-authored-by: PSeitz <1109503+PSeitz@users.noreply.github.com>
Copilot AI changed the title [WIP] Investigate and implement performance improvements Optimize safe encode/decode hot paths Mar 14, 2026
Copilot AI requested a review from PSeitz March 14, 2026 14:16
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