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 3, 2019
1 parent b3046ea commit ba2d04d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,15 @@ 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, true);
const tile = this._tiles[parentKey];
if (tile && tile.hasData()) {
return tile;
}
if (this._cache.has(parent)) {
return this._cache.get(parent);
}
// TileCache ignores wrap in lookup.
const parentWrappedKey = tileID.calculateScaledKey(z, false);
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
15 changes: 15 additions & 0 deletions src/source/tile_id.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ export class OverscaledTileID {
}
}

/*
* calculateScaledKey is an optimization:
* when withWrap == true, implements the same as this.scaledTo(z).key,
* when withWrap == false, implements the same as this.scaledTo(z).wrapped().key.
*/
calculateScaledKey(targetZ: number, withWrap: boolean) {
assert(targetZ <= this.overscaledZ);
const zDifference = this.canonical.z - targetZ;
if (targetZ > this.canonical.z) {
return calculateKey(this.wrap * +withWrap, targetZ, this.canonical.x, this.canonical.y);
} else {
return calculateKey(this.wrap * +withWrap, 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 ba2d04d

Please sign in to comment.