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 1, 2019
1 parent 3c36a67 commit 6cd5dbc
Show file tree
Hide file tree
Showing 2 changed files with 15 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 data = this._cache.data[parentWrappedKey];
if (data) return data[0].value;
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/source/tile_id.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ export class OverscaledTileID {
}
}

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 6cd5dbc

Please sign in to comment.