Skip to content

Commit

Permalink
avoid expensive sorting for non-symbol coords
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jul 23, 2018
1 parent 4f18076 commit b9a00dd
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/source/source_cache.js
Expand Up @@ -53,10 +53,9 @@ class SourceCache extends Evented {
_shouldReloadOnResume: boolean;
_coveredTiles: {[any]: boolean};
transform: Transform;
_isIdRenderable: (id: number) => boolean;
_isIdRenderableForSymbols: (id: number) => boolean;
_isIdRenderable: (id: number, symbolLayer?: boolean) => boolean;
used: boolean;
_state: SourceFeatureState
_state: SourceFeatureState;

static maxUnderzooming: number;
static maxOverzooming: number;
Expand Down Expand Up @@ -94,9 +93,6 @@ class SourceCache extends Evented {
this._cacheTimers = {};
this._maxTileCacheSize = null;

this._isIdRenderable = this._isIdRenderable.bind(this);
this._isIdRenderableForSymbols = this._isIdRenderableForSymbols.bind(this);

this._coveredTiles = {};
this._state = new SourceFeatureState();
}
Expand Down Expand Up @@ -180,22 +176,24 @@ class SourceCache extends Evented {
* Return all tile ids ordered with z-order, and cast to numbers
*/
getIds(): Array<number> {

const compareKeyZoom = (a_, b_) => {
const a = this._tiles[a_].tileID;
const b = this._tiles[b_].tileID;
const rotatedA = (new Point(a.canonical.x, a.canonical.y)).rotate(this.transform.angle);
const rotatedB = (new Point(b.canonical.x, b.canonical.y)).rotate(this.transform.angle);
return a.overscaledZ - b.overscaledZ || rotatedB.y - rotatedA.y || rotatedB.x - rotatedA.x;
};

return Object.keys(this._tiles).map(Number).sort(compareKeyZoom);
}

getRenderableIds(symbolLayer?: boolean) {
return symbolLayer ?
this.getIds().filter(this._isIdRenderableForSymbols) :
this.getIds().filter(this._isIdRenderable);
const ids = [];
for (const id in this._tiles) {
if (this._isIdRenderable(+id, symbolLayer)) ids.push(+id);
}
if (symbolLayer) {
return ids.sort((a_, b_) => {
const a = this._tiles[a_].tileID;
const b = this._tiles[b_].tileID;
const rotatedA = (new Point(a.canonical.x, a.canonical.y))._rotate(this.transform.angle);
const rotatedB = (new Point(b.canonical.x, b.canonical.y))._rotate(this.transform.angle);
return a.overscaledZ - b.overscaledZ || rotatedB.y - rotatedA.y || rotatedB.x - rotatedA.x;
});
}
return ids.sort(compareKeyZoom);
}

hasRenderableParent(tileID: OverscaledTileID) {
Expand All @@ -206,12 +204,9 @@ class SourceCache extends Evented {
return false;
}

_isIdRenderable(id: number) {
return this._tiles[id] && this._tiles[id].hasData() && !this._coveredTiles[id] && !this._tiles[id].holdingForFade();
}

_isIdRenderableForSymbols(id: number) {
return this._tiles[id] && this._tiles[id].hasData() && !this._coveredTiles[id];
_isIdRenderable(id: number, symbolLayer?: boolean) {
return this._tiles[id] && this._tiles[id].hasData() &&
!this._coveredTiles[id] && (symbolLayer || !this._tiles[id].holdingForFade());
}

reload() {
Expand Down Expand Up @@ -853,6 +848,10 @@ function coordinateToTilePoint(tileID: OverscaledTileID, coord: Coordinate): Poi
);
}

function compareKeyZoom(a, b) {
return ((a % 32) - (b % 32)) || (b - a);
}

function isRasterType(type) {
return type === 'raster' || type === 'image' || type === 'video';
}
Expand Down

0 comments on commit b9a00dd

Please sign in to comment.