Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LRU cache behavior #4210

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ class SourceCache extends Evented {
if (!tile) {
tile = this._cache.get(wrapped.id);
if (tile) {
this._cache.remove(wrapped.id);
tile.redoPlacement(this._source);
if (this._cacheTimers[wrapped.id]) {
clearTimeout(this._cacheTimers[wrapped.id]);
Expand Down Expand Up @@ -446,7 +447,7 @@ class SourceCache extends Evented {
const tileExpires = tile.getExpiry();
if (tileExpires) {
this._cacheTimers[id] = setTimeout(() => {
this._cache.remove(id);
this._cache.remove(id, true);
this._cacheTimers[id] = undefined;
}, tileExpires - new Date().getTime());
}
Expand Down
16 changes: 9 additions & 7 deletions src/util/lru_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ class LRUCache<T> {
this.order.push(key);

if (this.order.length > this.max) {
const removedData = this.get(this.order[0]);
if (removedData) this.onRemove(removedData);
this.remove(this.order[0], true);
}
}

Expand Down Expand Up @@ -104,8 +103,8 @@ class LRUCache<T> {

const data = this.data[key];

delete this.data[key];
this.order.splice(this.order.indexOf(key), 1);
this.order.push(key);

return data;
}
Expand All @@ -117,12 +116,16 @@ class LRUCache<T> {
* @returns {LRUCache} this cache
* @private
*/
remove(key: string) {
remove(key: string, expired: boolean) {
if (!this.has(key)) { return this; }

const data = this.data[key];
delete this.data[key];
this.onRemove(data);

if (expired) {
this.onRemove(data);
}

this.order.splice(this.order.indexOf(key), 1);

return this;
Expand All @@ -139,8 +142,7 @@ class LRUCache<T> {
this.max = max;

while (this.order.length > this.max) {
const removedData = this.get(this.order[0]);
if (removedData) this.onRemove(removedData);
this.remove(this.order[0], true);
}

return this;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/source/source_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ test('SourceCache#findLoadedParent', (t) => {
t.equal(sourceCache.findLoadedParent(new TileCoord(2, 3, 3), 0, retain), undefined);
t.equal(sourceCache.findLoadedParent(new TileCoord(2, 0, 0), 0, retain), tile);
t.deepEqual(retain, expectedRetain);
t.equal(sourceCache._cache.order.length, 0);
t.equal(sourceCache._cache.order.length, 1);

t.end();
});
Expand Down
29 changes: 25 additions & 4 deletions test/unit/util/lru_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ test('LRUCache', (t) => {
t.deepEqual(cache.keys(), ['washington'], '.keys()');
t.equal(cache.has('washington'), true, '.has()');
t.equal(cache.get('washington'), 'dc', '.get()');
t.equal(cache.get('washington'), null, '.get()');
t.equal(cache.has('washington'), false, '.has()');
t.deepEqual(cache.keys(), [], '.keys()');
t.equal(cache.get('washington'), 'dc', '.get()');
t.equal(cache.has('washington'), true, '.has()');
t.deepEqual(cache.keys(), ['washington'], '.keys()');
t.end();
});

test('LRUCache - get moves key to top of order stack', (t) => {
const cache = new LRUCache(10, (removed) => {
t.equal(removed, 'dc');
});
t.equal(cache.add('washington', 'dc'), cache, '.add()');
t.equal(cache.add('san francisco', 'ca'), cache, '.add()');
t.deepEqual(cache.keys(), ['washington', 'san francisco'], '.keys()');
t.equal(cache.has('washington'), true, '.has()');
t.equal(cache.get('washington'), 'dc', '.get()');
t.deepEqual(cache.keys(), ['san francisco', 'washington'], '.keys()');
t.end();
});

Expand All @@ -33,7 +46,11 @@ test('LRUCache - duplicate add', (t) => {
});

test('LRUCache - remove', (t) => {
const cache = new LRUCache(10, () => {});
let called;
const cache = new LRUCache(10, (removed) => {
t.equal(removed, 'dc');
called = true;
});

cache.add('washington', 'dc');
cache.add('baltimore', 'md');
Expand All @@ -49,6 +66,10 @@ test('LRUCache - remove', (t) => {

t.ok(cache.remove('baltimore'));

t.notOk(called);
cache.remove('washington', true);
t.ok(called);

t.end();
});

Expand Down