perf: cache results from getAllSubdocs() on saveOptions, only loop through known subdoc properties #15055
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 #15029
Summary
#15029 points out that we spend a lot of unnecessary CPU cycles in
save()
in$getAllSubdocs()
, even for schemas that have no subdocs. That's because currentlysave()
calls$getAllSubdocs()
5 times, and$getAllSubdocs()
loops through every path in the document searching for subdocs.With this PR, we will only call
$getAllSubdocs()
1x if callingsave()
with no subdocs, and also$getAllSubdocs()
will only loop through paths we know can have subdocs based on the schema. So if the schema doesn't have any subdocuments, document arrays, or maps of subdocs, then$getAllSubdocs()
will have an emptyfor
loop.In my quick experiment with the
saveSimple
benchmark, I'm seeing a 2.7% improvement in performance, which isn't huge, but that's 2.7% on every save call.I'll also work on adding a benchmark with subdocuments to see how much this helps with subdocs.
Examples