new Insert(BlockRecord) deep-copies nested blocks: quadratic time and stack overflow
Maintainers usually reply within 3 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 43/100
Research direction
Start with Insert.cs around line 230 and trace the constructor through BlockRecord.Clone() and Insert.Clone(). Reproduce the nested block chain from the issue, then check how reading a DWG handles nested blocks. Done means constructing deeply nested blocks avoids quadratic copying and stack overflow while preserving the expected block reference behavior.
Written by the indexing model from the issue text.
Description
Describe the bug
new Insert(BlockRecord) clones the block record when it already belongs to a document, and the clone copies every Insert inside it, which clones its block record again (Insert..ctor(BlockRecord) → BlockRecord.Clone() → Insert.Clone() → BlockRecord.Clone() → …). A chain of nested block definitions built one new Insert(previous) at a time therefore takes quadratic time (3 000 levels: about 85 s, writing included) and overflows a 1 MB stack at about 1 500 levels (StackOverflowException, the process terminates). For a block record that inserts itself, the copy does not end.
Insert.cs (master, v3.8.10), line 230:
if (block.Document != null)
{
this.Block = (BlockRecord)block.Clone();
}
To Reproduce
On a thread with a 1 MB stack:
var doc = new CadDocument();
BlockRecord previous = null;
for (int i = 0; i < 10_000; i++)
{
var rec = new BlockRecord("B" + i);
rec.Entities.Add(i == 0 ? new Line(XYZ.Zero, new XYZ(1, 0, 0)) : new Insert(previous));
doc.BlockRecords.Add(rec);
previous = rec;
}
Expected behavior
The constructor references the block record (as reading a DWG does: 3 000 nested levels read fine on a 256 KB stack) and copies only when asked.
Version
ACadSharp 3.8.0 (NuGet); same code on master.
- Dominant language
- C#
- Stars
- 873
- Forks
- 223
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 28
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from DomCR/ACadSharp
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 3 days
-
DWG writer: text styles get the text-style table as their xref block handlePossibly taken @menes97 claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 81/100
Maintainers usually reply within 3 days
-
Difficulty 1/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 3 days
-
DwgWriter writes a null shape-file handle for SHAPE entities (WriteShapes = true)Possibly taken @menes97 claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 3 days
-
DwgReader reads a color-book entity's Color as ByBlock instead of the book's colorPossibly taken @menes97 claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 3 days
Similar issues
-
hand-to-AI
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
CactuseSecurity/firewall-orchestrator#5803 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
bug python
Difficulty 1/5 Under an hour Newbie friendliness 88/100
ilia-sokolov/OfficeAgent.NET#89 ·
Maintainers usually reply within 13 days