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: Improve performance especially of validate and clone #11298

Merged
merged 4 commits into from
Feb 7, 2022
Merged

perf: Improve performance especially of validate and clone #11298

merged 4 commits into from
Feb 7, 2022

Conversation

Uzlopak
Copy link
Collaborator

@Uzlopak Uzlopak commented Jan 30, 2022

This PR improves the performance of especially validate and clone.

before:

MONGOOSE_DEV=1 node validate
invalid x 18,325 ops/sec ±1.40% (89 runs sampled)
valid x 46,240 ops/sec ±1.30% (91 runs sampled)

MONGOOSE_DEV=1 node clone
Simple x 124,034 ops/sec ±1.41% (91 runs sampled)
AllSchema x 17,409 ops/sec ±0.52% (94 runs sampled)

MONGOOSE_DEV=1 node create
CheckItem x 113,922 ops/sec ±0.34% (94 runs sampled)
Board x 112 ops/sec ±1.26% (74 runs sampled)

node populate
1436 completed queries on mongoose version 6.1.8

after:

MONGOOSE_DEV=1 node validate
invalid x 24,576 ops/sec ±0.89% (88 runs sampled)
valid x 119,331 ops/sec ±0.12% (95 runs sampled)

MONGOOSE_DEV=1 node clone
Simple x 148,691 ops/sec ±1.36% (87 runs sampled)
AllSchema x 22,479 ops/sec ±0.91% (95 runs sampled)

MONGOOSE_DEV=1 node create
CheckItem x 112,564 ops/sec ±0.26% (93 runs sampled)
Board x 122 ops/sec ±0.45% (77 runs sampled)

node populate
1528 completed queries on mongoose version 6.1.8
1528 completed queries on mongoose version 6.1.8

@Uzlopak
Copy link
Collaborator Author

Uzlopak commented Jan 30, 2022

I think the biggest performance bottleneck is the ES6 Proxy in DocumentArray and MongooseArray. Playing currently aroung with using WeakSets to see if the isMongooseArray and isMongooseDocumentArray can be boosted or not.

Also trying to figure out if we can reduce the roundtrips through the proxy layer by using forEach etc. when iterating through the elements of those special arrays.

@Uzlopak
Copy link
Collaborator Author

Uzlopak commented Feb 2, 2022

@vkarpov15
Should i split it into more granular PRs?

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Couple of comments, but general idea looks good

lib/document.js Outdated Show resolved Hide resolved
}

const validator = v.validator;
const validatorProperties = utils.clone(v);
const validatorProperties = Object.assign({}, v);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a little risky because Mongoose clone() does a deep clone, so this may cause issues if there's a validator with nested objects

Co-authored-by: Valeri Karpov <val@karpov.io>
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

LGTM, I'll just make the clone() change in master.

@vkarpov15 vkarpov15 added this to the 6.2.1 milestone Feb 7, 2022
@vkarpov15 vkarpov15 merged commit c7e6eda into Automattic:master Feb 7, 2022
vkarpov15 added a commit that referenced this pull request Feb 7, 2022
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.

None yet

2 participants