Repository navigation
Grow DXF entity lists fallibly and check room before placing them (part of #1922) - #3647
2 commits merged into
Conversation
Past the 65,536 entries an entity list reserves up front (storytold#1833), the DXF reader grows it by ordinary pushes. Each doubling is one allocation that aborts the process when it can't be had, and the last one leaves up to half the list spare. The new test reads 65,636 LINE records and expects no more room than records; on main the list ends with room for 131,072. Part of storytold#1922 (F-06). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n't fit Opening a large DXF aborted the process (handle_alloc_error) once an entity list grew past its fallible up-front reservation, and the move of the entities into the drawing allocated each of them again while the list still held its copy. - Entity lists grow fallibly (entity_room): doubling as a push would, but no further than their records can fill, so they end at about their final length; past the count (a polygon mesh's faces) they double. The up-front cap of storytold#1833 is unchanged. A mesh's faces are reserved with their polyline and pushed straight into that room. - The handle sets and the block list reserve before they insert. - The first ENTITIES/BLOCKS list is moved, not copied; a repeated section (storytold#865) is appended after a fallible reservation. - Before each list moves into the drawing, room_to_place asks once, fallibly, for about the memory the drawing's copies take and hands it back for them. - read_budgeted takes a test-only ceiling (None when reading) so tests can refuse a reservation as an allocator out of memory would. A release cadcraft-cli opening a 10M-group binary DXF of LINEs in a job with a memory limit now exits 1 with "DXF: out of memory reserving N entities" at every limit from 1.2 to 2.45 GB (main aborted up to 3.0 GB) and opens from 2.46 GB (main needed 3.2 GB; peak commit 3,215 -> 2,532 MB). Part of storytold#1922 (F-06). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
FredApps
left a comment
There was a problem hiding this comment.
The fallible reservations and removal of the extra list copy are useful improvements. Please scope the title and issue status to the allocation failures this change actually handles: the drawing-placement check is a temporary preflight, not a guarantee that the subsequent placement allocations can fail normally. The injected refusal tests verify preflight rejection, not placement failure. If the intention is to claim that placement itself returns an error, use a fallible ownership/reservation path at the actual storage boundary and verify it via a bounded injected failure. No real memory-pressure test is needed for this review.
|
Thanks. I've rescoped the title and the description; the code is unchanged.
|
FredApps
left a comment
There was a problem hiding this comment.
Thanks for narrowing the title and description. The PR now accurately distinguishes fallible DXF list growth from the placement preflight, and leaves #1922 open. That addresses my scope request.
I verified that the Ubuntu gate ran on this exact head: 5,255 tests passed, including the focused list growth, mesh reservation, and preflight checks. Later changes on main do not touch these DXF reader or storage paths. Approving the work as scoped; this is not a claim that drawing placement itself is fallible.
23f60b5
…efore placing them (part of storytold#1922)
Part of #1922 (F-06: memory amplification ends in OOM abort). This change covers the growth of the entity lists while a DXF is read, which is where the measured aborts happened. It does not make placing the entities in the drawing fallible: that step gets a preflight check only (see below).
What was wrong
Opening a large DXF could abort the process (
handle_alloc_error, exit0xC0000409on Windows) instead of returning an error. Since #1833 an entity list reserves its first 65,536 entries fallibly, but past thatparse_entitiesgrew it by ordinary pushes. Each doubling (2^19, 2^20, 2^21 entries × 728 bytes) is one allocation that cannot fail cleanly, and those doublings were where every run failed. On wasm32 the same growth panics withcapacity overflowonce one allocation passes 2 GiB. After reading, the move into the drawing also allocated every entity again (oneArceach) while the read list still held its copy. Those small allocations can't fail cleanly either, and this PR doesn't change that.Measured on
main32ad922 with a releasecadcraft-cli info <file> --compact, run in a Windows job object with a job memory limit. The inputs are synthetic binary/ASCII DXF files of N group codes: LINE entities of 9 groups each, about 1.11M LINEs at 10M groups. The harness is a small script (measure.pystyle) that reports the exit code, the stderr tail and the job's peak commit. Windows 11 x64.mainlines_binary_10Mmemory allocation of 381681664 bytes failed(2^19 × 728)lines_binary_10Mmemory allocation of 763363328 bytes failed(2^20 × 728)lines_binary_10Mmemory allocation of 1526726656 bytes failed(2^21 × 728)lines_binary_10Mlines_binary_20Mlines_ascii_10Mlines_ascii_10MWhat changes
All in
crates/io/src/dxf_read.rsanddxf_read/document.rs:entity_room). A full list doubles, as a push would, but no further than its records can fill. Every record except a follower adds at most one entity, so the list ends at about its final length, without the spare half a last doubling leaves. Only a polygon mesh's faces go past the count, and from there the list doubles again. The up-front reservation is unchanged: at mostMAX_RESERVED_ENTITIESbefore any record is admitted (DXF read: fewer allocations on the per-group and per-entity paths (#1076) #1833). A refused reservation returnsIoError::Format("DXF: out of memory reserving N entities"), the same wordingentity_listalready used.seen_handles,reserved_entity_handles) and the block list reserve before they insert.room_to_place): before each list (ENTITIES, the model space block, a paper space block, a block definition) moves into the drawing, the reader asks once, fallibly, for an estimate of the memory the drawing's copies take: oneArcper entity plus allocator overhead, a draw-order slot and a handle-index entry. It releases that memory at once and returnsIoError::Format("DXF: out of memory reserving N entities in the drawing")if the request is refused. This is a preflight, not a reservation. Placement itself still allocates infallibly: theEntityStorechunks, its handle index and oneArcper entity. If memory runs out after the check, or the estimate is too low, placement aborts as it does onmain.read_budgetedgets anentities_refused_pastargument, carried inRx, which is alwaysNonewhen reading. Tests use it to refuse the entity-list reservations and the preflight the way an allocator out of memory would, so those error paths run without exhausting memory. An in-process allocation failure can't be produced otherwise: a failing global allocator needsunsafe, which the workspace forbids.DWG goes through the same reader:
read_sourceconverts it to a DXF rendition and callsdxf_read::read. It is covered by the same code but was not measured separately.After
The same harness, inputs and limits, with a release build of this branch:
mainlines_binary_10Mout of memory reserving 524288 entitieslines_binary_10Mout of memory reserving 1048576 entitieslines_binary_10Mout of memory reserving 1111109 entitieslines_binary_10Mout of memory reserving 1111109 entities in the drawing(the preflight)lines_binary_10Mlines_binary_20Mout of memory reserving 2097152 entitieslines_binary_20Mlines_ascii_10M... 1048576 entities/... 1111109 entities in the drawing)lines_ascii_10Mlines_binary_5Mcomment_binary_20M(tag list only)The
lines_binary_10Msweep ran in 0.1 GB steps from 1.2 to 3.2 GB and in 0.01 GB steps from 2.41 to 2.49 GB. On this branch every run in that sweep either opened or exited 1 with the error; none aborted. That is what these inputs and limits showed, not a guarantee for placement: a limit that falls between the preflight and the placement allocations would still abort. The peak commit also drops: there is no spare half after the last doubling, and the ENTITIES list is no longer copied. Timings are single runs, so treat them as indicative only.Tests
In
dxf_read::entity_list_tests:a_long_entity_list_ends_within_its_record_count: 65,636 LINE records end in a list with no more room than records. Fails onmain: room for 131,072.entity_room_doubles_up_to_the_record_count: below the count the list doubles, it stops at the count, past the count it doubles again, and it makes room for a mesh's faces at once. Room already there asks for nothing more. A reservation that can't be had, or one past the ceiling, isErr, not an abort.a_mesh_asks_room_for_all_its_faces: a 3 × 3 polygon mesh gives 4 faces. With room refused past 3 it isErr; at 4 it reads.each_list_checks_its_room_before_it_moves_into_the_drawing: for ENTITIES and for*Model_Space,*Paper_Spaceand an ordinary block, a refused preflight makesread_budgetedreturnErr. With room for exactly 3 entities each file reads its 3. This shows that the preflight rejects and that its error reaches the caller. It does not test a failure during placement itself.The existing
entity_listtests,split_entities_section(#865) andskipped_recordsstill pass. The three earlierread_budgetedcallers in tests passNone.Not covered
EntityStorechunks, its handle index and oneArcper entity still allocate infallibly, androom_to_placeonly checks beforehand. Making placement itself fallible at the storage boundary, verified with a bounded injected failure, is a possible follow-up. Whether the per-entityArccan be allocated fallibly on stable Rust was not checked here.inserted,image_defs,paper_views_dropped, ...), andDrawing::reserved_handles. The measured failures were all in the large growth steps, which are now fallible.craft_dxf::record_slices(craft-libs) and the indexes andRawTagcopies indxf_raw::readstill allocate without a fallible reservation. They are smaller than the entity lists for entity-heavy files and run after the lists are freed. Making them fallible needsdxf_raw::readto return aResult, so it is a separate change.Entity(696 bytes on x86_64, 728 bytes with its owner and paper flag). It is out of scope here.How this combines with the other #1922 part e PRs
read_objects' signature line indocument.rsandobjects.rs; this PR doesn't touch either, so they merge without textual conflict and the savings add up.Testing (Windows 11 x64)
cargo xtask ci: fmt, clippy, tests, i18n, assets, layers and wasm all pass ("CI summary: all 7 steps passed").cargo clippy -p cadcraft-io --all-targets -- -D warnings,cargo test -p cadcraft-io.mainas described.append: 18 caught. The 5 survivors are the three handle and block-listtry_reservecalls and the preflight's own reservation, which only matter when the allocator really refuses (the measurements above reach the preflight's refusal at 2.43 – 2.45 GB), plus copying the first section's list instead of moving it, which is equivalent except for memory.🤖 Generated with Claude Code
Summary by CodeRabbit