Added a minimongo optimization to avoid some unecessary clones

This commit is contained in:
Lucas Hansen
2015-11-06 14:49:44 -08:00
committed by Avital Oliver
parent 211dd58165
commit 11f1d884d0

View File

@@ -696,12 +696,33 @@ LocalCollection.prototype.update = function (selector, mod, options, callback) {
// it. (We don't need to save the original results of paused queries because
// they already have a resultsSnapshot and we won't be diffing in
// _recomputeResults.)
var qidToOriginalResults = {};
var qidToOriginalResults = {},
docMap = {}, // We should only clone each document once, even if it appears in multiple queries
onlyIDQuery = _.size(selector) === 1 && _.isString(selector._id); // If the selector is just looking up a single _id, then we only need to clone that document
_.each(self.queries, function (query, qid) {
// XXX for now, skip/limit implies ordered observe, so query.results is
// always an array
if ((query.cursor.skip || query.cursor.limit) && ! self.paused)
qidToOriginalResults[qid] = EJSON.clone(query.results);
if ((query.cursor.skip || query.cursor.limit) && ! self.paused) {
// This is to catch LocalCollection._IdMap
if (_.isUndefined(query.results.length)) {
qidToOriginalResults[qid] = EJSON.clone(query.results);
return;
}
qidToOriginalResults[qid] = query.results.map(function(result) {
if (_.isUndefined(result) || _.isUndefined(result._id))
return EJSON.clone(result);
if (!_.has(docMap, result._id)) {
docMap[result._id] = (onlyIDQuery && result._id === selector._id)
? EJSON.clone(result)
: result;
}
return docMap[result._id];
});
}
});
var recomputeQids = {};