Skip to content

Commit 72cb0b7

Browse files
committed
perf(document): skip parallel validate check when creating new document in insertMany()
Re: #14719
1 parent 772f665 commit 72cb0b7

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

benchmarks/insertManySimple.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const mongoose = require('../');
4+
5+
run().catch(err => {
6+
console.error(err);
7+
process.exit(-1);
8+
});
9+
10+
async function run() {
11+
await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_benchmark');
12+
const FooSchema = new mongoose.Schema({ foo: String });
13+
const FooModel = mongoose.model('Foo', FooSchema);
14+
15+
if (!process.env.MONGOOSE_BENCHMARK_SKIP_SETUP) {
16+
await FooModel.deleteMany({});
17+
}
18+
19+
const numDocs = 1500;
20+
const docs = [];
21+
for (let i = 0; i < numDocs; ++i) {
22+
docs.push({ foo: 'test foo ' + i });
23+
}
24+
25+
const numIterations = 200;
26+
const insertStart = Date.now();
27+
for (let i = 0; i < numIterations; ++i) {
28+
await FooModel.insertMany(docs);
29+
}
30+
const insertEnd = Date.now();
31+
32+
const results = {
33+
'Average insertMany time ms': +((insertEnd - insertStart) / numIterations).toFixed(2)
34+
};
35+
36+
console.log(JSON.stringify(results, null, ' '));
37+
process.exit(0);
38+
}

lib/document.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -2601,17 +2601,6 @@ Document.prototype.validate = async function validate(pathsToValidate, options)
26012601
let parallelValidate;
26022602
this.$op = 'validate';
26032603

2604-
if (this.$isSubdocument != null) {
2605-
// Skip parallel validate check for subdocuments
2606-
} else if (this.$__.validating) {
2607-
parallelValidate = new ParallelValidateError(this, {
2608-
parentStack: options && options.parentStack,
2609-
conflictStack: this.$__.validating.stack
2610-
});
2611-
} else {
2612-
this.$__.validating = new ParallelValidateError(this, { parentStack: options && options.parentStack });
2613-
}
2614-
26152604
if (arguments.length === 1) {
26162605
if (typeof arguments[0] === 'object' && !Array.isArray(arguments[0])) {
26172606
options = arguments[0];
@@ -2622,6 +2611,18 @@ Document.prototype.validate = async function validate(pathsToValidate, options)
26222611
const isOnePathOnly = options.pathsToSkip.indexOf(' ') === -1;
26232612
options.pathsToSkip = isOnePathOnly ? [options.pathsToSkip] : options.pathsToSkip.split(' ');
26242613
}
2614+
const _skipParallelValidateCheck = options && options._skipParallelValidateCheck;
2615+
2616+
if (this.$isSubdocument != null) {
2617+
// Skip parallel validate check for subdocuments
2618+
} else if (this.$__.validating && !_skipParallelValidateCheck) {
2619+
parallelValidate = new ParallelValidateError(this, {
2620+
parentStack: options && options.parentStack,
2621+
conflictStack: this.$__.validating.stack
2622+
});
2623+
} else if (!_skipParallelValidateCheck) {
2624+
this.$__.validating = new ParallelValidateError(this, { parentStack: options && options.parentStack });
2625+
}
26252626

26262627
if (parallelValidate != null) {
26272628
throw parallelValidate;

lib/model.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3199,12 +3199,14 @@ Model.$__insertMany = function(arr, options, callback) {
31993199
// execute the callback synchronously
32003200
return immediate(() => callback(null, doc));
32013201
}
3202+
let createdNewDoc = false;
32023203
if (!(doc instanceof _this)) {
32033204
if (doc != null && typeof doc !== 'object') {
32043205
return callback(new ObjectParameterError(doc, 'arr.' + index, 'insertMany'));
32053206
}
32063207
try {
32073208
doc = new _this(doc);
3209+
createdNewDoc = true;
32083210
} catch (err) {
32093211
return callback(err);
32103212
}
@@ -3220,7 +3222,7 @@ Model.$__insertMany = function(arr, options, callback) {
32203222
// execute the callback synchronously
32213223
return immediate(() => callback(null, doc));
32223224
}
3223-
doc.$validate().then(
3225+
doc.$validate(createdNewDoc ? { _skipParallelValidateCheck: true } : null).then(
32243226
() => { callback(null, doc); },
32253227
error => {
32263228
if (ordered === false) {

0 commit comments

Comments
 (0)