Skip to content

Commit

Permalink
Perform some caching in the reference implementation
Browse files Browse the repository at this point in the history
In general we are loathe to introduce this kind of departure from the spec. However, it improves our ability to run the tests quickly by a very large factor. See the context at #488 (comment).
  • Loading branch information
tyoshino authored and domenic committed Aug 4, 2016
1 parent da868e0 commit 8c89f6f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions reference-implementation/lib/queue-with-sizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const { IsFiniteNonNegativeNumber } = require('./helpers.js');
exports.DequeueValue = queue => {
assert(queue.length > 0, 'Spec-level failure: should never dequeue from an empty queue.');
const pair = queue.shift();

queue._totalSize -= pair.size;

return pair.value;
};

Expand All @@ -15,19 +18,19 @@ exports.EnqueueValueWithSize = (queue, value, size) => {
}

queue.push({ value: value, size: size });

if (queue._totalSize === undefined) {
queue._totalSize = 0;
}
queue._totalSize += size;
};

// This implementation is not per-spec. Total size is cached for speed.
exports.GetTotalQueueSize = queue => {
let totalSize = 0;

queue.forEach(pair => {
assert(typeof pair.size === 'number' && !Number.isNaN(pair.size) &&
pair.size !== +Infinity && pair.size !== -Infinity,
'Spec-level failure: should never find an invalid size in the queue.');
totalSize += pair.size;
});

return totalSize;
if (queue._totalSize === undefined) {
queue._totalSize = 0;
}
return queue._totalSize;
};

exports.PeekQueueValue = queue => {
Expand Down

0 comments on commit 8c89f6f

Please sign in to comment.