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

Slower performance and higher cpu from Mongoose 4 to 5 with large schemas #10275

Closed
mikedavies-dev opened this issue May 20, 2021 · 3 comments
Closed
Milestone

Comments

@mikedavies-dev
Copy link

Do you want to request a feature or report a bug:

Bug

What is the current behavior?

When dealing with larger schemas and loading a large number of records Mongoose 5.x seems to be much slower than 4.x.

On my development machine, loading 10k documents using v4 takes approx 4.7 seconds and v5 takes approx 18 seconds to to load the same documents using the same script.

I've created a test project to see the issue including scripts to test under v4 and v5:
https://github.com/ant-fx/mongoose-performance-v4-v5

What is the expected behavior?
Version 5.x should be at least as fast as v4.x

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node: v14.5.0
Mongoose v4: 4.13.21
Mongoose v5: 5.12.10

@IslandRhythms IslandRhythms added performance has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 21, 2021
@vkarpov15 vkarpov15 added this to the 5.12.13 milestone May 27, 2021
@vkarpov15 vkarpov15 removed the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jun 1, 2021
vkarpov15 added a commit that referenced this issue Jun 1, 2021
@vkarpov15
Copy link
Collaborator

We found a way to shave 10-20% off of this script by being smarter about how we look up map paths: we had a loop that was O(n^2) in the number of paths in the schema when looking up map paths that we improved by only looking at actual map paths. Won't help if you have a schema with 100 paths that are all maps, but in this case it helps a decent amount.

We'll need to do a little bit of work to make our mapPaths improvement more durable by recalculating map paths on schema compilation rather than relying on clone() to get it right. We'll also search for more places where we can improve performance on this particular example.

vkarpov15 added a commit that referenced this issue Jun 1, 2021
vkarpov15 added a commit that referenced this issue Jun 3, 2021
…void extra overhead of splitting when hydrating documents

Re: #10275
vkarpov15 added a commit that referenced this issue Jun 3, 2021
@vkarpov15
Copy link
Collaborator

We tracked down the primary culprit for the extra overhead: #7104. $getAllSubdocs() is fairly slow, and queries call $session(null) if you don't specify a session, so most of the overhead comes from Mongoose repeatedly calling $getAllSubdocs() to set a null session. We patched that up, but we'll do some work to speed up $getAllSubdocs() since that function is also called in save().

vkarpov15 added a commit that referenced this issue Jun 3, 2021
…)` to avoid perf hit when running `$getAllSubdocs()`

Re: #10275
@vkarpov15
Copy link
Collaborator

184f10f should be enough to fix this modulo test failures.

vkarpov15 added a commit that referenced this issue Jun 3, 2021
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

3 participants