From 37a55a3895a270b74f8b8c3a39e783bfd5a7fb2e Mon Sep 17 00:00:00 2001 From: sim Date: Tue, 17 Nov 2026 01:29:38 +0300 Subject: [PATCH 1/2] fix(parser): keep duplicate mesh id fallback linear Probing for a free id restarted from the record index for every duplicate, making files with many duplicate trailer ids O(n^2). Resume from the last fallback id instead. --- src/NwcIO/Internal/Parsing/MeshParser.cs | 36 ++++++++++++++++++------ tests/NwcIO.Tests/MeshParserTests.cs | 15 ++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/NwcIO/Internal/Parsing/MeshParser.cs b/src/NwcIO/Internal/Parsing/MeshParser.cs index 0fa0f00..e7e8b13 100644 --- a/src/NwcIO/Internal/Parsing/MeshParser.cs +++ b/src/NwcIO/Internal/Parsing/MeshParser.cs @@ -162,7 +162,7 @@ private static void ParseViaToc( } int curPos = 0; - var usedIds = new HashSet(); + var usedIds = new MeshIdAllocator(); for (int r = 0; r < recordCount; r++) { int rLen = recordLengths[r]; @@ -281,6 +281,30 @@ private static void ParseViaHeuristicScan( } } + /// Tracks claimed mesh ids. Fallback probing resumes from the last fallback, so many duplicates stay linear. + private sealed class MeshIdAllocator + { + private readonly HashSet _used = []; + private int _nextFallback = int.MinValue; + + public int Claim(int id, int fallbackStart) + { + if (_used.Add(id)) + { + return id; + } + + id = Math.Max(fallbackStart, _nextFallback); + while (!_used.Add(id)) + { + id++; + } + + _nextFallback = id + 1; + return id; + } + } + private static NwcMesh? TryBuildMesh( byte[] buffer, int recordOffset, @@ -290,7 +314,7 @@ private static void ParseViaHeuristicScan( int recordLength, int meshId, bool lines, - HashSet? usedIds = null) + MeshIdAllocator? usedIds = null) { if (vertexOffset < 0 || vertexCount > (buffer.Length - vertexOffset) / 12) { @@ -342,13 +366,9 @@ private static void ParseViaHeuristicScan( // A merged document can repeat ids. Keep the first occurrence exact and give later duplicates a // unique fallback, so clash detection and IFC export do not silently pair unrelated meshes. - if (usedIds is not null && !usedIds.Add(id)) + if (usedIds is not null) { - id = meshId; - while (!usedIds.Add(id)) - { - id++; - } + id = usedIds.Claim(id, meshId); } return new NwcMesh diff --git a/tests/NwcIO.Tests/MeshParserTests.cs b/tests/NwcIO.Tests/MeshParserTests.cs index 0a8e5eb..6748253 100644 --- a/tests/NwcIO.Tests/MeshParserTests.cs +++ b/tests/NwcIO.Tests/MeshParserTests.cs @@ -715,6 +715,21 @@ public void Parse_TrailerIds_PreserveZeroAndMakeDuplicatesUnique() Assert.Equal([5, 0, 2], meshes.Select(mesh => mesh.Id)); } + [Fact] + public void Parse_ManyDuplicateTrailerIds_AreAllUnique() + { + const int count = 2000; + var records = Enumerable.Range(0, count) + .Select(i => TrailerRecord(i, meshId: 5, [0, 1, 2], "M")) + .ToArray(); + + var meshes = ParseRecords(records); + + Assert.Equal(count, meshes.Count); + Assert.Equal(count, meshes.Select(mesh => mesh.Id).Distinct().Count()); + Assert.Equal(5, meshes[0].Id); + } + [Theory] [InlineData("magic")] [InlineData("indexCount")] From 97905308029a57e26d2df4f56d3eaea3630a4e5b Mon Sep 17 00:00:00 2001 From: sim Date: Tue, 17 Nov 2026 03:25:39 +0300 Subject: [PATCH 2/2] test(parser): guard duplicate id probing against quadratic regression --- src/NwcIO/Internal/Parsing/MeshParser.cs | 7 ++++++- tests/NwcIO.Tests/MeshParserTests.cs | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/NwcIO/Internal/Parsing/MeshParser.cs b/src/NwcIO/Internal/Parsing/MeshParser.cs index e7e8b13..9290f38 100644 --- a/src/NwcIO/Internal/Parsing/MeshParser.cs +++ b/src/NwcIO/Internal/Parsing/MeshParser.cs @@ -282,11 +282,14 @@ private static void ParseViaHeuristicScan( } /// Tracks claimed mesh ids. Fallback probing resumes from the last fallback, so many duplicates stay linear. - private sealed class MeshIdAllocator + internal sealed class MeshIdAllocator { private readonly HashSet _used = []; private int _nextFallback = int.MinValue; + /// Number of fallback candidates tried so far (exposed for complexity tests). + public int Probes { get; private set; } + public int Claim(int id, int fallbackStart) { if (_used.Add(id)) @@ -295,9 +298,11 @@ public int Claim(int id, int fallbackStart) } id = Math.Max(fallbackStart, _nextFallback); + Probes++; while (!_used.Add(id)) { id++; + Probes++; } _nextFallback = id + 1; diff --git a/tests/NwcIO.Tests/MeshParserTests.cs b/tests/NwcIO.Tests/MeshParserTests.cs index 6748253..a9b282f 100644 --- a/tests/NwcIO.Tests/MeshParserTests.cs +++ b/tests/NwcIO.Tests/MeshParserTests.cs @@ -730,6 +730,27 @@ public void Parse_ManyDuplicateTrailerIds_AreAllUnique() Assert.Equal(5, meshes[0].Id); } + [Fact] + public void MeshIdAllocator_DuplicatesOfOccupiedRange_ProbeLinearly() + { + var allocator = new MeshParser.MeshIdAllocator(); + for (int id = 1000; id < 2000; id++) + { + allocator.Claim(id, id); + } + + var claimed = new HashSet(); + for (int i = 0; i < 1000; i++) + { + claimed.Add(allocator.Claim(1000, fallbackStart: 1000 + i)); + } + + Assert.Equal(1000, claimed.Count); + Assert.DoesNotContain(claimed, id => id is >= 1000 and < 2000); + // A probe restarted from fallbackStart for every duplicate needs about 1,000,000 tries here. + Assert.True(allocator.Probes < 10_000, $"Probes: {allocator.Probes}"); + } + [Theory] [InlineData("magic")] [InlineData("indexCount")]