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(document): only call undoReset() 1x/document #15257

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3558,8 +3558,10 @@ Document.prototype.$__undoReset = function $__undoReset() {
}
}

for (const subdoc of this.$getAllSubdocs()) {
subdoc.$__undoReset();
if (!this.$isSubdocument) {
for (const subdoc of this.$getAllSubdocs()) {
subdoc.$__undoReset();
}
}
};

Expand Down
42 changes: 42 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ArraySubdocument = require('../lib/types/arraySubdocument');
const Query = require('../lib/query');
const assert = require('assert');
const idGetter = require('../lib/helpers/schema/idGetter');
const sinon = require('sinon');
const util = require('./util');
const utils = require('../lib/utils');

Expand Down Expand Up @@ -14296,6 +14297,47 @@ describe('document', function() {

delete mongoose.Schema.Types.CustomType;
});

it('handles undoReset() on deep recursive subdocuments (gh-15255)', async function() {
const RecursiveSchema = new mongoose.Schema({});

const s = [RecursiveSchema];
RecursiveSchema.path('nested', s);

const generateRecursiveDocument = (depth, curr = 0) => {
return {
name: `Document of depth ${curr}`,
nested: depth > 0 ? new Array(2).fill().map(() => generateRecursiveDocument(depth - 1, curr + 1)) : [],
__v: 5
};
};
const TestModel = db.model('Test', RecursiveSchema);
const data = generateRecursiveDocument(10);
const doc = new TestModel(data);
await doc.save();

sinon.spy(Document.prototype, '$__undoReset');

try {
const d = await TestModel.findById(doc._id);
d.increment();
d.data = 'asd';
// Force a version error by updating the document directly
await TestModel.collection.updateOne({ _id: doc._id }, { $inc: { __v: 1 } });
const err = await d.save().then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'VersionError');
// `$__undoReset()` should be called 1x per subdoc, plus 1x for top-level doc. Without fix for gh-15255,
// this would fail because `$__undoReset()` is called nearly 700k times for only 2046 subdocs
assert.strictEqual(Document.prototype.$__undoReset.getCalls().length, d.$getAllSubdocs().length + 1);
assert.ok(Document.prototype.$__undoReset.getCalls().find(call => call.thisValue === d), 'top level doc was not reset');
for (const subdoc of d.$getAllSubdocs()) {
assert.ok(Document.prototype.$__undoReset.getCalls().find(call => call.thisValue === subdoc), `${subdoc.name} was not reset`);
}
} finally {
sinon.restore();
}
});
});

describe('Check if instance function that is supplied in schema option is available', function() {
Expand Down
Loading