Skip to content

Commit

Permalink
get rid of Struct._setIndex
Browse files Browse the repository at this point in the history
It's faster, but not fast enough to be worth it.
  • Loading branch information
ansis committed Mar 24, 2016
1 parent 25df374 commit afa28ea
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
13 changes: 5 additions & 8 deletions js/data/feature_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,11 @@ FeatureTree.prototype.query = function(args, styleLayers, returnGeoJSON) {

var matching = this.grid.query(minX - additionalRadius, minY - additionalRadius, maxX + additionalRadius, maxY + additionalRadius);
matching.sort(topDownFeatureComparator);
var match = this.featureIndexArray.get(0);
this.filterMatching(result, matching, match, queryGeometry, filter, params.layers, styleLayers, args.bearing, pixelsToTileUnits, returnGeoJSON);
this.filterMatching(result, matching, this.featureIndexArray, queryGeometry, filter, params.layers, styleLayers, args.bearing, pixelsToTileUnits, returnGeoJSON);

var matchingSymbols = this.collisionTile.queryRenderedSymbols(minX, minY, maxX, maxY, args.scale);
var match2 = this.collisionTile.collisionBoxArray.get(0);
matchingSymbols.sort();
this.filterMatching(result, matchingSymbols, match2, queryGeometry, filter, params.layers, styleLayers, args.bearing, pixelsToTileUnits, returnGeoJSON);
this.filterMatching(result, matchingSymbols, this.collisionTile.collisionBoxArray, queryGeometry, filter, params.layers, styleLayers, args.bearing, pixelsToTileUnits, returnGeoJSON);

return result;
};
Expand All @@ -178,7 +176,7 @@ function getLineWidth(paint) {
}
}

FeatureTree.prototype.filterMatching = function(result, matching, match, queryGeometry, filter, filterLayerIDs, styleLayers, bearing, pixelsToTileUnits, returnGeoJSON) {
FeatureTree.prototype.filterMatching = function(result, matching, array, queryGeometry, filter, filterLayerIDs, styleLayers, bearing, pixelsToTileUnits, returnGeoJSON) {
var previousIndex;
for (var k = 0; k < matching.length; k++) {
var index = matching[k];
Expand All @@ -187,7 +185,7 @@ FeatureTree.prototype.filterMatching = function(result, matching, match, queryGe
if (index === previousIndex) continue;
previousIndex = index;

match._setIndex(index);
var match = array.get(index);

var layerIDs = this.bucketLayerIDs[match.bucketIndex];
if (filterLayerIDs && !arraysIntersect(filterLayerIDs, layerIDs)) continue;
Expand Down Expand Up @@ -270,11 +268,10 @@ FeatureTree.prototype.makeGeoJSON = function(featureIndexArray, styleLayers) {
var result = {};

featureIndexArray = new FilteredFeatureIndexArray(featureIndexArray);
var indexes = featureIndexArray.get(0);

var cachedLayerFeatures = {};
for (var i = 0; i < featureIndexArray.length; i++) {
indexes._setIndex(i);
var indexes = featureIndexArray.get(i);
var sourceLayerName = this.sourceLayerNumberMapping.numberToString[indexes.sourceLayerIndex];
var sourceLayer = this.vtLayers[sourceLayerName];
var featureIndex = indexes.featureIndex;
Expand Down
2 changes: 1 addition & 1 deletion js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = Style;

function Style(stylesheet, animationLoop) {
this.animationLoop = animationLoop || new AnimationLoop();
this.dispatcher = new Dispatcher(Math.max(browser.hardwareConcurrency - 1, 1), this);
this.dispatcher = new Dispatcher(Math.max(1), this);

This comment has been minimized.

Copy link
@mourner

mourner Apr 8, 2016

Member

LOL

this.spriteAtlas = new SpriteAtlas(512, 512);
this.lineAtlas = new LineAtlas(256, 512);

Expand Down
18 changes: 7 additions & 11 deletions js/symbol/collision_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ function CollisionTile(angle, pitch, collisionBoxArray) {
collisionBoxArray.get(3),
collisionBoxArray.get(4)
];

this.box = collisionBoxArray.get(0);
this.blocking = collisionBoxArray.get(0);
}

CollisionTile.prototype.serialize = function() {
Expand Down Expand Up @@ -109,15 +106,14 @@ CollisionTile.prototype.maxScale = 2;
*/
CollisionTile.prototype.placeCollisionFeature = function(collisionFeature, allowOverlap, avoidEdges) {

var collisionBoxArray = this.collisionBoxArray;
var minPlacementScale = this.minScale;
var rotationMatrix = this.rotationMatrix;
var yStretch = this.yStretch;
var box = this.box;
var blocking = this.blocking;

for (var b = collisionFeature.boxStartIndex; b < collisionFeature.boxEndIndex; b++) {

box._setIndex(b);
var box = collisionBoxArray.get(b);

var anchorPoint = box.anchorPoint._matMult(rotationMatrix);
var x = anchorPoint.x;
Expand All @@ -137,7 +133,7 @@ CollisionTile.prototype.placeCollisionFeature = function(collisionFeature, allow
var blockingBoxes = this.grid.query(x1, y1, x2, y2);

for (var i = 0; i < blockingBoxes.length; i++) {
blocking._setIndex(blockingBoxes[i]);
var blocking = collisionBoxArray.get(blockingBoxes[i]);
var blockingAnchorPoint = blocking.anchorPoint._matMult(rotationMatrix);

minPlacementScale = this.getPlacementScale(minPlacementScale, anchorPoint, box, blockingAnchorPoint, blocking);
Expand Down Expand Up @@ -186,6 +182,7 @@ CollisionTile.prototype.queryRenderedSymbols = function(minX, minY, maxX, maxY,
var sourceLayerFeatures = {};
var result = [];

var collisionBoxArray = this.collisionBoxArray;
var rotationMatrix = this.rotationMatrix;
var anchorPoint = new Point(minX, minY)._matMult(rotationMatrix);

Expand Down Expand Up @@ -214,9 +211,8 @@ CollisionTile.prototype.queryRenderedSymbols = function(minX, minY, maxX, maxY,
blockingBoxKeys.push(blockingBoxKeys2[k]);
}

var blocking = this.blocking;
for (var i = 0; i < blockingBoxKeys.length; i++) {
blocking._setIndex(blockingBoxKeys[i]);
var blocking = collisionBoxArray.get(blockingBoxKeys[i]);

var sourceLayer = blocking.sourceLayerIndex;
var featureIndex = blocking.featureIndex;
Expand Down Expand Up @@ -291,10 +287,10 @@ CollisionTile.prototype.getPlacementScale = function(minPlacementScale, anchorPo
CollisionTile.prototype.insertCollisionFeature = function(collisionFeature, minPlacementScale, ignorePlacement) {

var grid = ignorePlacement ? this.ignoredGrid : this.grid;
var collisionBoxArray = this.collisionBoxArray;

var box = this.box;
for (var k = collisionFeature.boxStartIndex; k < collisionFeature.boxEndIndex; k++) {
box._setIndex(k);
var box = collisionBoxArray.get(k);
box.placementScale = minPlacementScale;
if (minPlacementScale < this.maxScale) {
grid.insert(k, box.bbox0, box.bbox1, box.bbox2, box.bbox3);
Expand Down
17 changes: 1 addition & 16 deletions js/util/struct_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ var structArrayTypeCache = {};
* point = pointArray.get(0);
* assert(point.x === 10);
* assert(point.y === 15);
* point._setIndex(1);
* assert(point.x === 20);
* assert(point.y === 35);
*
* @private
*/
Expand Down Expand Up @@ -220,23 +217,11 @@ function createSetter(member, c) {
*/
function Struct(structArray, index) {
this._structArray = structArray;
this._setIndex(index);
}

/**
* Make this Struct object point to a different instance in the same array.
* It can be cheaper to use .setIndex to re-use an existing Struct than to
* create a new one.
* @param {number} index The index of the struct in the StructArray;
* @private
*/
Struct.prototype._setIndex = function(index) {
this._pos1 = index * this.size;
this._pos2 = this._pos1 / 2;
this._pos4 = this._pos1 / 4;
this._pos8 = this._pos1 / 8;
};

}

/**
* @class StructArray
Expand Down

0 comments on commit afa28ea

Please sign in to comment.