Skip to content

Improve performance when batch invalidating/removing subgraphs - #87

Closed
jcmosc wants to merge 3 commits into
mainfrom
add-set
Closed

jcmosc wants to merge 3 commits into
mainfrom
add-set

Conversation

@jcmosc

@jcmosc jcmosc commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Tearing down a graph with many subgraphs took minutes. Every removal called Graph::remove_subgraph, which ran std::remove over the sorted _subgraphs vector. That is O(n) per subgraph, so invalidating n subgraphs together was O(n²). This hits graph.invalidate(), withoutSubgraphInvalidation, and invalidations deferred inside Subgraph.forEach.

_subgraphs is now an IAG::set: a vector sorted by address that marks erased elements as removed and compacts them once they outnumber the live ones. Removing a subgraph is a binary search plus amortized O(1) work.

jcmosc and others added 3 commits October 21, 2026 19:40
Prepares for adding a set type alongside the vector types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A set of pointers stored in a vector sorted by address. Erased elements
are marked as removed and skipped when iterating, then compacted away
once they outnumber the remaining elements, so erasing is amortized O(1)
after an O(log n) lookup instead of shifting the rest of the vector.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Removing a subgraph from the graph used std::remove over the sorted
vector of subgraphs, which is O(n), so invalidating n subgraphs together
(e.g. tearing down a graph) was O(n^2). Erasing from IAG::set is
amortized O(1) after a binary search, and iteration order is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jcmosc

jcmosc commented Oct 23, 2026

Copy link
Copy Markdown
Owner Author

No longer needed. The performance impact was only observable at about 300,000 subgraphs, which were reduced downstream.

@jcmosc jcmosc closed this Oct 23, 2026
@jcmosc
jcmosc deleted the add-set branch October 24, 2026 15:43
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.

1 participant