Skip to content

perf(streaming): flatten §5.10.1's copy table at compile time (#205) - #365

Merged
DrunkOnJava merged 2 commits into
mainfrom
perf/stream-r2007-lz-emit
Oct 27, 2026
Merged

DrunkOnJava merged 2 commits into
mainfrom
perf/stream-r2007-lz-emit

Conversation

@DrunkOnJava

Copy link
Copy Markdown
Owner

Summary

A literal run of the R2007 LZ dialect (§5.10) is copied out of order in blocks of up to 32 bytes whose bytes follow §5.10.1's copy table. The streaming decoder walked that table recursively for every block, one byte per leaf. The table is now flattened once at compile time into a permutation per block size, so a block is one gather. The decoder's state and checkpoints are unchanged.

Profile (Time Profiler, the streaming R2007 walk of sample_AC1021.dwg alone): the recursion (r2007_lz::emit) was 25.8% of the walk's samples.

A scratch check against the recursive version (not committed) compared all 32 block sizes on 64,000 random blocks and that each table is a permutation of 0..n.

R2007 streaming walk, best of 15 in memory (examples/stream_r2007_timing, Apple M2 Max under load), against origin/main at a315f04:

Drawing Before After
sample_AC1021 (1.06 MB) 20.63 ms, 51.5 MB/s 15.88 ms, 66.9 MB/s x1.30

Spec reference

§5.10 and §5.10.1 (the R2007 LZ dialect and its copy table), unchanged in meaning. Performance only: part of #205. No format finding, no coverage-state change.

Test plan

  • cargo fmt --all -- --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features
  • cargo +1.85 check --all-features (MSRV)
  • cargo test --release --all-features (existing suites only; no test added; built without LTO to fit the machine's CPU budget, perf(streaming): fold eight bytes per step in the DWG CRC-8 (#205) #360 ran the full LTO profile)
  • python3 tools/streaming/check_invariants.py; tools/streaming/no_std.rs compiled natively and for wasm32-unknown-unknown, state bounds hold
  • The streaming verifiers that decode R2007 literal runs on real drawings, from release builds with overflow checks and debug assertions (CI runs the debug ones): verify_r2007.py (the 8 pinned drawings and the files it builds: every output line and every assembled section byte equal to the independent Python reader and the legacy reader, fixed and proptest schedules, fresh-process resumes, forged checkpoints, corruptions) and verify_wasm_r2007.py (native and wasm32 checkpoints byte-identical). Both passed; each writes its binary SHA to its evidence JSON.
  • A scratch check of the compile-time permutation against the recursive emit (all 32 sizes, 64,000 random blocks; each table a permutation of 0..n). Not committed (no unit tests): every literal run of the verifiers above goes through it.
  • Throughput (the table above): cargo build --release --example stream_r2007_timing, then target/release/examples/stream_r2007_timing <corpus>/acadsharp/sample_AC1021.dwg 15, build of origin/main against build of this branch, alternated.

Source-provenance declaration

By submitting this PR I certify that my contribution does not incorporate executable code from any source whose license is incompatible with Apache-2.0. Specifically:

  • No Autodesk DWG SDK source (RealDWG, ObjectARX, ObjectDBX) or NDA-protected Autodesk documentation was consulted or imported.
  • No Open Design Alliance Teigha / Drawings SDK source was consulted or imported. The ODA's public Open Design Specification for .dwg files PDF is explicitly allowed (see CLEANROOM.md).
  • No GPL-licensed DWG implementation source (LibreDWG, etc.) was consulted or imported. This matters because dwg-rs is Apache-2.0 and must remain compatible.

My contribution is based on:

  • The ODA's freely-redistributable Open Design Specification for .dwg files (v5.4.1);
  • Public reverse-engineering notes, research papers, or blog posts cited inline;
  • Reading algorithm-description comments (not executable code) in permissively-licensed projects (MIT / Apache / BSD) to resolve spec ambiguities — disclose any such reading in this PR body so it can be recorded in CLEANROOM.md.

See CLEANROOM.md for the full allowed/forbidden source list and the honest scope of what "clean-room" means for this project.

Developer Certificate of Origin

By submitting this PR, I certify that the contribution complies with the
Developer Certificate of Origin 1.1.
Specifically, I affirm that the contribution is my own work (or rightfully
licensed to me) and that it can be distributed under the project's Apache-2.0
license.

I will sign off commits with git commit -s when asked, which adds a
Signed-off-by trailer to the commit message asserting the DCO.

A literal run of the R2007 LZ dialect is copied out of order in blocks of up to
32 bytes whose bytes follow §5.10.1's copy table. The decoder walked the table
recursively for every block, one byte per leaf. In the Time Profiler profile of
the streaming R2007 walk of sample_AC1021, that recursion (r2007_lz::emit) was
about 27% of the walk's samples.

The table is now flattened once at compile time into a permutation per block
size, so a block is one gather. A scratch check against the recursive version
(not committed) compared all 32 sizes on 64,000 random blocks and that each
table is a permutation of 0..n; the decoder's state and checkpoints are
unchanged.

R2007 streaming walk, best of 15 in memory (examples/stream_r2007_timing),
against origin/main, Apple M2 Max under load:

  sample_AC1021  20.63 ms -> 15.88 ms  (x1.30; 51.5 -> 66.9 MB/s of the file)
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T06:18:24.714730Z 27f5667 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@DrunkOnJava
DrunkOnJava enabled auto-merge (squash) October 27, 2026 06:20
@DrunkOnJava
DrunkOnJava merged commit 8f91442 into main Oct 27, 2026
25 checks passed
@DrunkOnJava
DrunkOnJava deleted the perf/stream-r2007-lz-emit branch October 27, 2026 06:27
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