Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$__reset method is slow on document with large number of subdocuments #13271

Closed
2 tasks done
alexd6631 opened this issue Apr 13, 2023 · 1 comment
Closed
2 tasks done
Milestone

Comments

@alexd6631
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the performance issue has not already been reported

Last performant version

6.9.2

Slowed down in version

6.9.2

Node.js version

18

🦥 Performance issue

Context :
I had an issue at work when saving a document with a large number of subdocuments.
The mongoose model (written with Typegoose) use a Map to store unique pair of keys and sub-documents, in a single mongo document.

When adding a set of keys, to a document having already thousand of keys, I noticed the .save() was quite long, at first I suspected that the change detection or write to the db itself was the most expensive. But a profiling session showed that the bottleneck was on __reset method calling isModified.

image

I also noticed the problem was aggregated by the size of the batch of new keys added to the inner Map.

After stepping into the save/reset/isModified methods using a debugger I noticed the following :

So the reset method seems to operate in O(n_subdocs * n_modified_subdocs), which seems the biggest culprit here.

As a workaround, since I am not modifying the object after save() (and I no longer use the object), replacing .save() by model.updateOne(model.getChanges()) yields a large performance improvement (my bench goes from 45s to 1s)

Steps to Reproduce

Unfortunately I can't share the benchmarking code as is.
Having a document with a large set of subdocument (for instance in the map), and trying to modify/add new subdocuments by batch should be enough to reproduce the problem.

Expected Behavior

$__reset should probably not a bottleneck when saving a document.

@alexd6631
Copy link
Author

After second look, it seems to be similar of : #13191
I am keeping the issue open, since the profile flame-graph and pre-analysis done here, could help fix the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants