Result cache LRU (#165)

* Give the result cache some LRU love

* Fixing LRU cache move-to-start, and simplifying
This commit is contained in:
Jake Archibald
2018-09-11 17:31:08 +01:00
committed by GitHub
parent 4db43ccd4e
commit c591f1f37d

View File

@@ -21,12 +21,13 @@ const SIZE = 5;
export default class ResultCache {
private readonly _entries: CacheEntry[] = [];
private _nextIndex: number = 0;
add(entry: CacheEntry) {
if (entry.encoderState.type === identity.type) throw Error('Cannot cache identity encodes');
this._entries[this._nextIndex] = entry;
this._nextIndex = (this._nextIndex + 1) % SIZE;
// Add the new entry to the start
this._entries.unshift(entry);
// Remove the last entry if we're now bigger than SIZE
if (this._entries.length > SIZE) this._entries.pop();
}
match(
@@ -34,7 +35,7 @@ export default class ResultCache {
preprocessorState: PreprocessorState,
encoderState: EncoderState,
): CacheResult | undefined {
const matchingEntry = this._entries.find((entry) => {
const matchingIndex = this._entries.findIndex((entry) => {
// Check for quick exits:
if (entry.source !== source) return false;
if (entry.encoderState.type !== encoderState.type) return false;
@@ -55,14 +56,20 @@ export default class ResultCache {
return true;
});
if (matchingEntry) {
return {
bmp: matchingEntry.bmp,
preprocessed: matchingEntry.preprocessed,
file: matchingEntry.file,
};
if (matchingIndex === -1) return undefined;
const matchingEntry = this._entries[matchingIndex];
if (matchingIndex !== 0) {
// Move the matched result to 1st position (LRU)
this._entries.splice(matchingIndex, 1);
this._entries.unshift(matchingEntry);
}
return undefined;
return {
bmp: matchingEntry.bmp,
preprocessed: matchingEntry.preprocessed,
file: matchingEntry.file,
};
}
}