Repository navigation
fix: make ContiguousSet and InsertionOrderSet IntersectWith/SymmetricExceptWith linear [patch] - #110
Merged
Merged
Conversation
…ExceptWith linear [patch] Both operations dropped elements one at a time. ContiguousSet called Remove, which rescans the array from index 0 and shifts the tail per element, and InsertionOrderSet called List.RemoveAt per element. Dropping most of a set was O(n²): about 2.3 s (Release) or 23 s (Debug) to intersect a 100k-element ContiguousSet with an empty sequence. ContiguousSet now drops elements in a single compaction pass that clears the vacated slots and bumps the version once. InsertionOrderSet uses List.RemoveAll. Both are O(n + m) and keep the surviving elements in order. Fixes #83 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J6q8NVYVFUeBVAH6tJCHnU
# Conflicts: # Containers.Test/ContiguousSetTests.cs
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #83
What was wrong
ContiguousSet<T>.IntersectWithandSymmetricExceptWithcalled the publicRemove(item)for each element they dropped. Each call rescansitemsfrom index 0 and shifts the tail, so dropping most of a set costs O(n²).InsertionOrderSet<T>had the same shape, with oneList.RemoveAtper dropped element.Change
RemoveWheremakes one compaction pass. It keeps a write index, removes dropped items fromuniquenessSet, clears the vacated tail whenSlotClearing.IsNeeded<T>()says to, and bumpsversiononce, only if something was removed. Both operations use it.List.RemoveAllwith the same predicates.otherare still appended afterwards, as before.Tests
*_RunsInLinearTime: n = 100k forContiguousSetand n = 200k forInsertionOrderSet, each with a 1.5 s budget.ContiguousSet) and 15 s and 14 s (InsertionOrderSet).otherare appended after them.ContainsandAddagree with the new contents.This is a separate change from #109 (#78), which touches
AsSpanin the same file. The two changes do not overlap.🤖 Generated with Claude Code
https://claude.ai/code/session_01J6q8NVYVFUeBVAH6tJCHnU
Generated by Claude Code