Skip to content

Rewrite Triangulation<T> storage: raw arrays + ReadOnlyMemory, stackalloc, ref mutations#9

Merged
MichaConrad merged 2 commits intomainfrom
copilot/rewrite-triangulation-memory-implementation
Feb 22, 2026
Merged

Rewrite Triangulation<T> storage: raw arrays + ReadOnlyMemory, stackalloc, ref mutations#9
MichaConrad merged 2 commits intomainfrom
copilot/rewrite-triangulation-memory-implementation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 22, 2026

Replace the three List<T> backing stores with raw arrays + explicit count fields, exposing contiguous-memory read-only views and eliminating unnecessary copies throughout the hot paths.

Storage & public API

  • List<V2d<T>>V2d<T>[] + int _verticesCount; Vertices now returns ReadOnlyMemory<V2d<T>>
  • List<Triangle>Triangle[] + int _trianglesCount; Triangles now returns ReadOnlyMemory<Triangle>
  • List<int> _vertTrisint[] + int _vertTrisCount
  • Pre-allocation via ArrayEnsureCapacity on first insertion; doubling growth via ArrayAdd
// Before
public IReadOnlyList<V2d<T>> Vertices => _vertices;
public IReadOnlyList<Triangle> Triangles => _triangles;

// After
public ReadOnlyMemory<V2d<T>> Vertices => new(_vertices, 0, _verticesCount);
public ReadOnlyMemory<Triangle> Triangles => new(_triangles, 0, _trianglesCount);

stackalloc

InsertVertices_Randomized and InsertVertices_KDTreeBFS use stackalloc int[n] when n ≤ StackAllocThreshold (512), falling back to heap otherwise. NthElement changed from int[] to Span<int>.

ref mutations — eliminate copy-back pattern

All read-modify-write triangle mutations converted from the var t = …; mutate; _triangles[i] = t pattern to ref var t = ref _triangles[i]:

  • Both ChangeNeighbor overloads
  • TriangulatePseudoPolygonIteration (was doing 4–6 round-trips per call)
  • RemoveTriangles neighbor remapping loop
  • FinalizeTriangulation vertex-offset loop

Span-based utilities

  • VertexXComparer/VertexYComparer take V2d<T>[] directly — removes IReadOnlyList<T> interface dispatch on every compare
  • Box2d<T>.Envelop(ReadOnlySpan<V2d<T>>) overload added; used in InitKdTree
  • CdtUtils.ExtractEdgesFromTriangles and CalculateTrianglesByVertex accept ReadOnlySpan<Triangle> with ref readonly inner loops
  • TopologyVerifier works off .Span from both memory properties

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…loc, ref mutations

Co-authored-by: MichaCo <5837539+MichaCo@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor triangulation for optimized memory management Rewrite Triangulation<T> storage: raw arrays + ReadOnlyMemory, stackalloc, ref mutations Feb 22, 2026
Copilot AI requested a review from MichaConrad February 22, 2026 20:58
@MichaConrad MichaConrad marked this pull request as ready for review February 22, 2026 21:42
@MichaConrad MichaConrad merged commit fa606be into main Feb 22, 2026
@MichaConrad MichaConrad deleted the copilot/rewrite-triangulation-memory-implementation branch February 23, 2026 07:15
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.

2 participants