perf: track deepClone circular refs with a WeakMap#4734
Open
lukecotter wants to merge 1 commit into
Open
Conversation
Collaborator
|
@lukecotter This does change the behavior though right? It changes it from using |
Contributor
Author
|
@rathboma oof I forgot to add to the map. It’s in my local copy I will sort asap. Sorry about that. |
Replace the O(n) array scan (findIndex per visited object) with an O(1) WeakMap keyed by each source object -> its clone. On large graphs this is ~38x faster at 5k nodes and ~147x at 20k (O(n^2) -> O(n)), matching the reported >5min -> <3s on very large circular graphs. Also fixes a latent correctness bug: the previous list never recorded the root object, so a reference back to the root was cloned as a duplicate object instead of resolving to the same clone. Keying every source object (including the root) preserves shared and circular reference identity. Adds Helpers.deepClone unit tests (deep copy, mutation isolation, shared refs, nested and root circular references).
c293c0f to
55ff567
Compare
Contributor
Author
|
Part of #4917 (overview + combined numbers). |
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.
What
Track circular references in
Helpers.deepClonewith aWeakMap(O(1)) instead of an array scanned per object (O(n)). Record each source object → its clone at entry, and look it up before recursing.Behaviour
Unchanged for normal clones. Also fixes two circular-reference bugs: the array version never recorded the root (a ref back to the root was cloned as a duplicate), and an earlier draft keyed the map by the parent but looked up the child. New
Helpers.spec.jstests cover shared + nested + root circular references. Full suite green.Performance (isolated, Node,
deepCloneof a wide graph)O(n²) → O(n).
Grid impact
Negligible —
deepCloneruns off the render path (Accessor/Localize), and grid rows are small/non-circular (accessorgetDataover 20k rows moved ~7%). The large win is for big/deep/circular data structures. Baselineupstream/master@9539446.