Skip to content

perf(streaming): fold eight bytes per step in the DWG CRC-8 (#205) - #360

Merged
DrunkOnJava merged 1 commit into
mainfrom
perf/stream-crc16
Oct 27, 2026
Merged

DrunkOnJava merged 1 commit into
mainfrom
perf/stream-crc16

Conversation

@DrunkOnJava

Copy link
Copy Markdown
Owner

Summary

crc8, the DWG CRC (§2.14.1) of every record, object map chunk and R13 to R2000 section, folds eight bytes per step with slicing tables derived from the published table at compile time, instead of one byte per step through a serial table lookup. Same value for every input, seed and length.

Profile first (Instruments Time Profiler, release build with line tables, the streaming walk of the pinned sample_AC1015.dwg alone): crc8 was 81.7% of all samples, 88.7% with the slice iterator that feeds it. On the R2010 walk it was 37%. The legacy reader checks no CRC, which is a large part of why the walk looked 16 to 18 times slower on R14 and R2000.

Streaming walk, best of 40 in memory, 256-byte reads from a slice (examples/stream_throughput, Apple M2 Max, load average 11 to 52), against origin/main at a315f04:

Release Drawing Before After
R14 sample_AC1014 3.38 ms, 388 MB/s 0.79 ms, 1,652 MB/s x4.28
R2000 sample_AC1015 3.35 ms, 388 MB/s 0.78 ms, 1,669 MB/s x4.29
R2004 sample_AC1018 5.78 ms, 190 MB/s 3.54 ms, 310 MB/s x1.63
R2010 sample_AC1024 7.09 ms, 158 MB/s 4.50 ms, 249 MB/s x1.58
R2013 sample_AC1027 5.47 ms, 200 MB/s 3.29 ms, 332 MB/s x1.66
R2018 sample_AC1032 5.45 ms, 198 MB/s 3.23 ms, 335 MB/s x1.69

The legacy reader's column of the same runs did not move (0.19 to 1.37 ms before and after), which is the noise control: it does not call crc8 on this path.

Spec reference

§2.14.1 (the CRC and its 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_TARGET_DIR=... cargo +1.85 check --all-features (MSRV)
  • cargo test --release --all-features (the full LTO release profile; all existing suites pass; no test added)
  • 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 run crc8 on real drawings, from release builds with overflow checks and debug assertions (the machine's CPU budget makes debug builds impractical; CI runs the debug ones): verify_walk.py (R14 and R2000 container walk), verify_source.py, verify_objects.py (32 files, 8,027 records, every record CRC checked, equal to the independent parse and the legacy reader), verify_wasm_objects.py (native and wasm32 checkpoints byte-identical). All passed. Binary SHAs are in the evidence JSON each writes.
  • A scratch differential check of the new crc8 against the bitwise reflected polynomial 0xA001: 89,579 cases (every 16-bit seed, lengths 0 to 600, a 1 MiB buffer). Not committed (no unit tests): the corpus verifiers above are the repeatable evidence, since a wrong CRC fails every record.
  • Throughput (the table above): cargo build --release --example stream_throughput, then target/release/examples/stream_throughput <corpus>/acadsharp/sample_AC10{14,15,18,24,27,32}.dwg --runs 40, 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.

@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-27T03:39:28.426521Z 6dbbbaf 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.

The record, map-chunk and section CRC (§2.14.1) took one byte per step
through a table, each step waiting on the load before it. Profiled with
Instruments' Time Profiler on the streaming walk of the pinned
sample_AC1015 (R2000): 81.7% of samples were in crc8, 88.7% with the slice
iterator that feeds it; on R2010 it was 37%.

crc8 now folds eight bytes at a time with slicing tables derived from the
published table at compile time (the state XORed into the low two bytes of
the little-endian word, one independent lookup per byte). It returns the same
value for every input, seed and length.

Streaming walk, best of 40 in memory (examples/stream_throughput, 256-byte
reads from a slice), Apple M2 Max under load:

  R14    sample_AC1014  3.38 ms ->  0.79 ms  (388 -> 1,652 MB/s)
  R2000  sample_AC1015  3.35 ms ->  0.78 ms  (388 -> 1,669 MB/s)
  R2004  sample_AC1018  5.78 ms ->  3.54 ms  (190 ->   310 MB/s)
  R2010  sample_AC1024  7.09 ms ->  4.50 ms  (158 ->   249 MB/s)
  R2013  sample_AC1027  5.47 ms ->  3.29 ms  (200 ->   332 MB/s)
  R2018  sample_AC1032  5.45 ms ->  3.23 ms  (198 ->   335 MB/s)
@DrunkOnJava
DrunkOnJava merged commit 645739a into main Oct 27, 2026
25 checks passed
@DrunkOnJava
DrunkOnJava deleted the perf/stream-crc16 branch October 27, 2026 03:48
DrunkOnJava added a commit that referenced this pull request Oct 28, 2026
… to what landed (#205, #206, #207) (#400)

The ledger stopped at the header and the R13-R2000 walk, and the README's
checkpoint contract described the header checkpoint alone and said full object
and decompression checkpoints remain open. Both now cover the ten checkpoint
kinds on main and the walkers of #240, #246, #260, #276, #299, #312, #333, #340
and #363, the perf series #360 to #366, and cite the evidence files of Streaming
E2E run 40532346151. What was not measured is marked NOT MEASURED.
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