Skip to content

Commit

Permalink
Optimize findLoadedParent
Browse files Browse the repository at this point in the history
Performance optimization when rendering view with a lot of satellite tiles.
Measurements on Chrome (Version 78.0.3904.108 with CPU 4x slowdown in performance tab) on MacMini i7 3.2 Ghz.

`map.repaint = true` in http://localhost:9966/debug/satellite.html#12.5/38.888/-77.01866/0/60

Observed FPS count:
master: 19-20FPS
with this patch: 24.5-26 FPS
  • Loading branch information
astojilj committed Dec 2, 2019
1 parent 3c36a67 commit 7d0431c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,14 @@ class SourceCache extends Evented {
*/
findLoadedParent(tileID: OverscaledTileID, minCoveringZoom: number): ?Tile {
for (let z = tileID.overscaledZ - 1; z >= minCoveringZoom; z--) {
const parent = tileID.scaledTo(z);
if (!parent) return;
const id = String(parent.key);
const tile = this._tiles[id];
const parentKey = tileID.calculateScaledKey(z, 1);
const tile = this._tiles[parentKey];
if (tile && tile.hasData()) {
return tile;
}
if (this._cache.has(parent)) {
return this._cache.get(parent);
}
const parentWrappedKey = tileID.calculateScaledKey(z, 0);
const cachedTile = this._cache.getByKey(parentWrappedKey);
if (cachedTile) return cachedTile;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/source/tile_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ class TileCache {
return data.value;
}

/*
* Get the value with the specified (wrapped tile) key.
*/
getByKey(key: number): ?Tile {
const data = this.data[key];
return data ? data[0].value : null;
}

/**
* Get the value attached to a specific key without removing data
* from the cache. If the key is not found, returns `null`
Expand Down
14 changes: 14 additions & 0 deletions src/source/tile_id.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ export class OverscaledTileID {
}
}

/*
* calculateScaledKey with wrap values 1 or 0, is an optimization: it implements
* this.scaledTo(z).key or this.scaledTo(z).wrapped().key, respectively.
*/
calculateScaledKey(targetZ: number, wrap: 0 | 1) {
assert(targetZ <= this.overscaledZ);
const zDifference = this.canonical.z - targetZ;
if (targetZ > this.canonical.z) {
return calculateKey(this.wrap * wrap, targetZ, this.canonical.x, this.canonical.y);
} else {
return calculateKey(this.wrap * wrap, targetZ, this.canonical.x >> zDifference, this.canonical.y >> zDifference);
}
}

isChildOf(parent: OverscaledTileID) {
if (parent.wrap !== this.wrap) {
// We can't be a child if we're in a different world copy
Expand Down

0 comments on commit 7d0431c

Please sign in to comment.