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

perf: speed up mapOfSubdocs benchmark by 4x by avoiding unnecessary O(n^2) loop in getPathsToValidate() #13614

Merged
merged 5 commits into from
Jul 17, 2023

Conversation

vkarpov15
Copy link
Collaborator

Summary

Flame graph helped identify that most of the time was spent in the following loop. With a faster implementation that doesn't loop through all modified paths for every subdocument, Mongoose is now 3-4x faster on the mapOfSubdocs benchmark.

for (const subdoc of subdocs) {
  for (const p of paths) {
    if (p == null || p.startsWith(fullPathToSubdoc + '.')) {
      paths.delete(p);
    }
  }
}

Before this change:

{
  "Average save time ms": 439.4
}

After this change:

{
  "Average save time ms": 145.8
}

Flamegraph now looks a bunch more reasonable too:

image

I think we can now mark #13191 and #13271 as fixed, because $__reset() is no longer a bottleneck and between this PR and #13280 Mongoose is now more than 10x faster on this particular benchmark.

Examples

@vkarpov15 vkarpov15 changed the base branch from master to 6.x July 15, 2023 20:18
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, is this only for 6.x or will this be added to 7.x too?

@vkarpov15
Copy link
Collaborator Author

We still merge everything from 6.x back into master, so this will end up in the next 7.x release too.

Copy link
Collaborator

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Awesome

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! LGTM, thanks! 👍

@vkarpov15 vkarpov15 merged commit 895bc32 into 6.x Jul 17, 2023
34 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-13191-2 branch July 17, 2023 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants