change: skip cp lookup on no change
Modify `Change.rectify` to look up the current checkpoint only when there was actually some change made. This should improve the performance of `rectifyAll` when called from a regular timer and there were no changes made since the last call. Before this commit, `rectifyAll` would perform N calls of `Checkpoint.current` where N is the number of model instances. With this commit in place, no call is made.
This commit is contained in:
parent
d2aaca7460
commit
62d2b0bf0d
|
@ -175,24 +175,31 @@ module.exports = function(Change) {
|
|||
if (err) throw new Error(err);
|
||||
};
|
||||
|
||||
async.parallel([
|
||||
function getCurrentCheckpoint(next) {
|
||||
change.constructor.getCheckpointModel().current(next);
|
||||
},
|
||||
function getCurrentRevision(next) {
|
||||
change.currentRevision(next);
|
||||
}
|
||||
], doRectify);
|
||||
|
||||
function doRectify(err, results) {
|
||||
change.currentRevision(function(err, rev) {
|
||||
if (err) return cb(err);
|
||||
var checkpoint = results[0];
|
||||
var rev = results[1];
|
||||
|
||||
// avoid setting rev and prev to the same value
|
||||
if (currentRev === rev) {
|
||||
change.debug('rev and prev are equal (not updating anything)');
|
||||
return cb(null, change);
|
||||
}
|
||||
|
||||
// FIXME(@bajtos) Allo callers to pass in the checkpoint value
|
||||
// (or even better - a memoized async function to get the cp value)
|
||||
// That will enable `rectifyAll` to cache the checkpoint value
|
||||
change.constructor.getCheckpointModel().current(
|
||||
function(err, checkpoint) {
|
||||
if (err) return cb(err);
|
||||
doRectify(checkpoint, rev);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
function doRectify(checkpoint, rev) {
|
||||
if (rev) {
|
||||
// avoid setting rev and prev to the same value
|
||||
if (currentRev === rev) {
|
||||
change.debug('rev and prev are equal (not updating anything)');
|
||||
change.debug('ASSERTION FAILED: Change currentRev==rev ' +
|
||||
'should have been already handled');
|
||||
return cb(null, change);
|
||||
} else {
|
||||
change.rev = rev;
|
||||
|
|
Loading…
Reference in New Issue