Repository navigation
perf(documentArray): avoid O(n*m) id() lookup when setting a documentarray to a plain array - #16683
Merged
Merged
Conversation
… array to a plain array Fix #16673
…inconsistent __parentArray pointers
vkarpov15
requested review from
AbdelrahmanHafez and
hasezoey
and
a balanced review from Copilot
November 7, 2026 22:02
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.
Fix #16673
Summary
#16673 points out that setting a document array to a plain array is extremely slow for large document arrays of 5000+ elements because of an
id()lookup in a loop. Thatid()lookup is not strictly necessary; it was originally introduced in 5d5e1c823 (Oct 2011) as a mechanism to reuse subdocuments, presumably for performance. However, now that document array elements have an__parentArraypointer, a document array element should only be in one document array, so reusing an existing subdocument in a new document array is problematic.To avoid extra overhead in the
doc.arr = doc.toObject().arrcase, which is surprisingly common, despite how much I advise against it, I added an extra check to see if the array being set is exactly equal, and, if so, reuse the existing array. That is consistent with the current behavior - currentlydoc.arr = doc.toObject().arrcreates a new document array, but with nothing modified.Examples