Skip to content

io: saving a drawing of thousands of blocks no longer takes quadratic time; scaling test (FF6) - #1938

Open
francescoBV wants to merge 1 commit into
storytold:mainfrom
francescoBV:pr/dxf-save-speed
Open

francescoBV wants to merge 1 commit into
storytold:mainfrom
francescoBV:pr/dxf-save-speed

Conversation

@francescoBV

@francescoBV francescoBV commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Francesco BV

Before: saving a drawing that was opened from a file took time that grew with the square of its blocks. dxf_raw::finish found each block's ENDBLK by scanning every written record (plan_unknown), and add_class_instance_counts added each CLASS record's group 91 with Vec::insert, which moves the whole tag list once per class. A drawing of 12,000 entities and 6,000 small blocks, opened and saved again as DXF, took 8.4 s (release); 3,000 entities took 0.5 s, so 4x the entities cost 16.7x the time. DWG saves go through the same DXF writer, so they had the same problem.

After: the same save takes 0.6 s (3,000 entities: 0.11 s). Four times the entities now take about four to six times as long to open or save, as DXF or DWG.

How: plan_unknown builds a map from block name to ENDBLK in one pass over the records. add_class_instance_counts collects the 91 groups and inserts them all in one rebuild of the list. finish moves the written groups into its output with mem::replace instead of cloning them. The passes over a written file (head, head_handle, foreign_xdata, record, add_appids, the handle seed) read group text through a borrowed text() instead of Tag::str(), and Rec shares its section name (Rc<str>). In crates/dxf, write_ascii_as writes group codes and reals without format! (push_code, push_real) and copies plain ASCII text without checking each character. The output is byte for byte the same as before. This branch doesn't change reading, so it doesn't overlap #1708. docs/gaps.md row FF6 and its revision history are updated.

Overlaps: #1833 and #1588 also read group text without a copy in the head()/foreign_xdata()/written_records() lines of crates/io/src/dxf_raw.rs (#1833 adds dxf_ext::text), so whichever lands second needs a small rebase there. #1708, #1588, #1833 and #1835 speed up reading. This PR speeds up saving, and they don't duplicate each other. docs/gaps.md (header line, FF6 row, revision history) will conflict textually with my other open PRs #1909, #1910 and #1911 and with the rest of this batch. #1911 also edits crates/dxf/src/lib.rs and crates/io/src/dxf_write.rs, but in other functions.

Tests

  • crates/io/tests/large_drawing_perf.rs (new; drawings generated in code, no fixtures): generated_drawing_holds_what_was_asked; mid_size_drawing_opens_and_round_trips_in_time (20,000 entities through DXF and DWG, 60 s bound); opening_and_saving_scale_linearly (3,000 vs 12,000 entities with a block for every two: DXF/DWG open and save must stay under 9x; on main the DXF save step fails at 14–17x); write_large_corpus (#[ignore], writes 100k–1M-entity files for profiling)
  • crates/dxf/src/lib.rs: fast_code_and_real_formatting_match_format (codes from -1,000,000 to 1,000,000, reals including -0.0, 1e21, f64::MAX, NaN and infinity, and line breaks, non-ASCII and plain text match the old format! output)
  • cargo fmt --all -- --check, cargo clippy -p cadcraft-dxf -p cadcraft-io --all-targets -- -D warnings, cargo test -p cadcraft-dxf -p cadcraft-io, cargo xtask layers, cargo check --target wasm32-unknown-unknown -p cadcraft-dxf -p cadcraft-io

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance
    • Improved DXF save performance for drawings reopened from their own files. A 12,000-entity drawing now saves in about 0.6 seconds, compared with 8.4 seconds previously.
  • Tests
    • Added performance checks for opening and saving large DXF and DWG drawings.

@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 11248fa and 47b6979.

Files selected for processing: 6
  • crates/dxf/src/lib.rs
  • crates/io/src/dxf_raw.rs
  • crates/io/src/dxf_write/sections.rs
  • crates/io/tests/it/large_drawing_perf.rs
  • crates/io/tests/it/main.rs
  • docs/gaps.md
Files skipped from review as they are similar to previous changes: 1
  • docs/gaps.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

Walkthrough

The change updates DXF formatting and raw-record processing, adds large-drawing performance tests for DXF and DWG operations, and records save-performance results and remaining throughput gaps in the documentation.

Changes: DXF Performance and Scaling

Layer / File(s) Summary
ASCII DXF formatting
crates/dxf/src/lib.rs
ASCII output uses append-based helpers for group codes and values. Tests compare the new formatting with previous formatting and check representative output.
Raw tag processing
crates/io/src/dxf_raw.rs
Raw DXF handling borrows text for eligible tag values, shares section names, and parses handles directly in applicable paths.
Record output and class counts
crates/io/src/dxf_raw.rs, crates/io/src/dxf_write/sections.rs
Output assembly moves existing tags instead of cloning them. Unknown block end records are indexed in one pass, and missing class-count tags are inserted in one pass.
Large-drawing validation and documentation
crates/io/tests/it/large_drawing_perf.rs, crates/io/tests/it/main.rs, docs/gaps.md
Integration tests generate large drawings and check content, round-trip time bounds, and scaling ratios. The documentation records save timings, the scaling threshold, and remaining gaps.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Possibly related issues

  • storytold/cadcraft issue 1077: The issue describes slow DXF saves on large drawings and hash-map work in dxf_raw::finish; this change modifies that save path and indexes block end records in one pass.
  • storytold/cadcraft issue 1076: The issue asks about write-side memory use in the DWG conversion path; this change moves existing tag groups into output instead of cloning them.

Suggested reviewers: vangel76, aiongg

Merge Risk: Minimal · up to 47b69

The DXF performance changes appear ready to merge after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the primary change: saving a drawing with thousands of blocks no longer takes quadratic time. The objectives confirm this is the main goal, with DXF and DWG save time reduced for block…
Docstring Coverage Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the primary change: saving a drawing with thousands of blocks no longer takes quadratic time. The objectives confirm this is the main goal, with DXF and DWG save time reduced for block-heavy drawings. The title also names the scaling test and the FF6 label, which the changeset includes. The title is specific and matches the changeset. It is somewhat long at 94 characters and 16 words, but it stays on topic and does not use vague terms.

Full details: Docstring Coverage

Explanation

Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 24 functions across 5 files. (1 skipped: 1 unsupported.)

Finishing Touches: 💡 1
⚔️ Resolve merge conflicts: 💡
  • Resolve merge conflict in branch pr/dxf-save-speed
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

… time; scaling test (FF6)

Saving a drawing reopened from its own file looked up each block's ENDBLK
by scanning all records (dxf_raw::plan_unknown), and inserted every CLASS
instance count with Vec::insert, moving the whole tag list once per class.
Both are now one pass. dxf_raw::finish moves the written groups into its
output instead of cloning them, the passes over a written file borrow
group text instead of copying it (Rec's section name is shared), and the
ASCII writer formats group codes, reals and plain text without format!.

crates/io/tests/large_drawing_perf.rs generates drawings in code: a
20,000-entity DXF/DWG round trip under a generous bound, and a scaling
check that four times the entities (and blocks) take less than nine times
as long to open and save as DXF and DWG. On main the DXF save step takes
16x as long; with this change about 5x.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
dylanmtaylor added a commit to storytold/craft-libs that referenced this pull request Nov 11, 2026
…ADCraft PRs) (#924)

* dxf: write group codes, reals and plain text without the formatting machinery

write_ascii and its code page forms wrote every group code through
write!("{:>3}"), every real through a String of its own and every string
character by character. A large drawing writes millions of each. Codes 0 to
9999 and reals are now appended in place, and text with no line break or
backslash that is ASCII (or goes out as UTF-8) is copied as it is. Text for a
code page still takes the long path, since a code page may write an ASCII
character another way. The output is byte for byte what it was.

Ported from storytold/cadcraft#1938 (the crates/dxf part).

* dxf: parse_recovering reads a damaged or truncated ASCII file up to the damage

parse fails a file at the first line that is no group code, and at a last
group code with no value. parse_recovering returns the tags before that point
and the line where reading stopped, so a caller can open what the file still
holds and say where it ends. Reading stops at the damage: lines after it are
not paired up by guesswork. Damage before the first group, a truncated binary
file and the group-code limit are still errors.

section_slices ends a section at EOF as well as ENDSEC, so the EOF of a file
cut inside a section is not one of that section's records.

Ported from storytold/cadcraft#1565 (the crates/dxf part).

---------

Co-authored-by: Francesco BV <francesco.esposito@bvitalia.net>
Co-authored-by: Alex Ortiz <76929676+aljormon84@users.noreply.github.com>

This branch has not been deployed

No deployments
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