Skip to content

Commit

Permalink
perf: avoid calls to ownerDocument() when first creating a document
Browse files Browse the repository at this point in the history
Re: #10451
  • Loading branch information
vkarpov15 committed Apr 28, 2022
1 parent 1aa4c4e commit cd6735b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
20 changes: 12 additions & 8 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1455,14 +1455,18 @@ Document.prototype.$set = function $set(path, val, type, options) {
}

if (shouldSet) {
const doc = this.$isSubdocument ? this.ownerDocument() : this;
const savedState = doc.$__.savedState;
const savedStatePath = this.$isSubdocument ? this.$__.fullPath + '.' + path : path;
if (savedState != null) {
const firstDot = savedStatePath.indexOf('.');
const topLevelPath = firstDot === -1 ? savedStatePath : savedStatePath.slice(0, firstDot);
if (!savedState.hasOwnProperty(topLevelPath)) {
savedState[topLevelPath] = utils.clone(doc.$__getValue(topLevelPath));
let savedState = null;
let savedStatePath = null;
if (!constructing) {
const doc = this.$isSubdocument ? this.ownerDocument() : this;
savedState = doc.$__.savedState;
savedStatePath = this.$isSubdocument ? this.$__.fullPath + '.' + path : path;
if (savedState != null) {
const firstDot = savedStatePath.indexOf('.');
const topLevelPath = firstDot === -1 ? savedStatePath : savedStatePath.slice(0, firstDot);
if (!savedState.hasOwnProperty(topLevelPath)) {
savedState[topLevelPath] = utils.clone(doc.$__getValue(topLevelPath));
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions lib/schema/SubdocumentPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ function _createConstructor(schema, baseClass) {
this.$__parent = parent;
Subdocument.apply(this, arguments);

const ownerDocument = this.ownerDocument();
if (ownerDocument == null) {
if (parent == null) {
return;
}
this.$session(ownerDocument.$session());
this.$session(parent.$session());
};

schema._preCompile();
Expand Down
8 changes: 5 additions & 3 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ function _createConstructor(schema, options, baseClass) {
Subdocument || (Subdocument = require('../types/ArraySubdocument'));

// compile an embedded document for this schema
function EmbeddedDocument() {
function EmbeddedDocument(_value, parentArray) {
Subdocument.apply(this, arguments);

this.$session(this.ownerDocument().$session());
if (parentArray == null || parentArray.getArrayParent() == null) {
return;
}
this.$session(parentArray.getArrayParent().$session());
}

schema._preCompile();
Expand Down
6 changes: 1 addition & 5 deletions lib/schema/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,7 @@ SchemaNumber.prototype.enum = function(values, message) {
*/

SchemaNumber.prototype.cast = function(value, doc, init) {
if (SchemaType._isRef(this, value, doc, init)) {
if (typeof value === 'number') {
return value;
}

if (typeof value !== 'number' && SchemaType._isRef(this, value, doc, init)) {
if (value == null || utils.isNonBuiltinObject(value)) {
return this._castRef(value, doc, init);
}
Expand Down
6 changes: 2 additions & 4 deletions lib/schema/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,9 @@ ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
*/

ObjectId.prototype.cast = function(value, doc, init) {
if (SchemaType._isRef(this, value, doc, init)) {
if (!(value instanceof oid) && SchemaType._isRef(this, value, doc, init)) {
// wait! we may need to cast this to a document
if (value instanceof oid) {
return value;
} else if ((getConstructorName(value) || '').toLowerCase() === 'objectid') {
if ((getConstructorName(value) || '').toLowerCase() === 'objectid') {
return new oid(value.toHexString());
}

Expand Down
6 changes: 1 addition & 5 deletions lib/schema/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,7 @@ SchemaString.prototype.checkRequired = function checkRequired(value, doc) {
*/

SchemaString.prototype.cast = function(value, doc, init) {
if (SchemaType._isRef(this, value, doc, init)) {
if (typeof value === 'string') {
return value;
}

if (typeof value !== 'string' && SchemaType._isRef(this, value, doc, init)) {
return this._castRef(value, doc, init);
}

Expand Down
8 changes: 8 additions & 0 deletions lib/types/DocumentArray/methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ const methods = {
return this.toObject(internalToObjectOptions);
},

/*!
* ignore
*/

getArrayParent() {
return this[arrayParentSymbol];
},

/**
* Overrides MongooseArray#cast
*
Expand Down

0 comments on commit cd6735b

Please sign in to comment.