perf: cache results from getAllSubdocs() on saveOptions, only loop through known subdoc properties#15055
perf: cache results from getAllSubdocs() on saveOptions, only loop through known subdoc properties#15055
Conversation
…rough known subdoc properties Fix #15029
|
Quick check with the |
|
Does it improves performance of create or insertMany ? |
hasezoey
left a comment
There was a problem hiding this comment.
LGTM, aside from the minor jsdoc change necessary. I would recommend to put this into the next minor version for caution.
It will affect any function making use of |
|
|
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 emptyforloop.In my quick experiment with the
saveSimplebenchmark, 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