mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-14 09:39:15 +00:00
Result cache LRU (#165)
* Give the result cache some LRU love * Fixing LRU cache move-to-start, and simplifying
This commit is contained in:
@@ -21,12 +21,13 @@ const SIZE = 5;
|
|||||||
|
|
||||||
export default class ResultCache {
|
export default class ResultCache {
|
||||||
private readonly _entries: CacheEntry[] = [];
|
private readonly _entries: CacheEntry[] = [];
|
||||||
private _nextIndex: number = 0;
|
|
||||||
|
|
||||||
add(entry: CacheEntry) {
|
add(entry: CacheEntry) {
|
||||||
if (entry.encoderState.type === identity.type) throw Error('Cannot cache identity encodes');
|
if (entry.encoderState.type === identity.type) throw Error('Cannot cache identity encodes');
|
||||||
this._entries[this._nextIndex] = entry;
|
// Add the new entry to the start
|
||||||
this._nextIndex = (this._nextIndex + 1) % SIZE;
|
this._entries.unshift(entry);
|
||||||
|
// Remove the last entry if we're now bigger than SIZE
|
||||||
|
if (this._entries.length > SIZE) this._entries.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
match(
|
match(
|
||||||
@@ -34,7 +35,7 @@ export default class ResultCache {
|
|||||||
preprocessorState: PreprocessorState,
|
preprocessorState: PreprocessorState,
|
||||||
encoderState: EncoderState,
|
encoderState: EncoderState,
|
||||||
): CacheResult | undefined {
|
): CacheResult | undefined {
|
||||||
const matchingEntry = this._entries.find((entry) => {
|
const matchingIndex = this._entries.findIndex((entry) => {
|
||||||
// Check for quick exits:
|
// Check for quick exits:
|
||||||
if (entry.source !== source) return false;
|
if (entry.source !== source) return false;
|
||||||
if (entry.encoderState.type !== encoderState.type) return false;
|
if (entry.encoderState.type !== encoderState.type) return false;
|
||||||
@@ -55,14 +56,20 @@ export default class ResultCache {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (matchingEntry) {
|
if (matchingIndex === -1) return undefined;
|
||||||
return {
|
|
||||||
bmp: matchingEntry.bmp,
|
const matchingEntry = this._entries[matchingIndex];
|
||||||
preprocessed: matchingEntry.preprocessed,
|
|
||||||
file: matchingEntry.file,
|
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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user