Skip to content

Commit

Permalink
Avoid superfluous copies of rawTileData via pbf
Browse files Browse the repository at this point in the history
Refs mapbox/pbf#35 (comment)

A copy, rather than a transfer, _is_ needed when sending the rawTileData to the main thread. In that case, don't use a transferrable.
  • Loading branch information
jfirebaugh committed Aug 30, 2016
1 parent d918226 commit 09ab2cf
Show file tree
Hide file tree
Showing 14 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bench/benchmarks/buffer.js
Expand Up @@ -157,7 +157,7 @@ function runSample(stylesheet, getGlyphs, getIcons, getTile, callback) {

getTile(url, function(err, response) {
if (err) throw err;
var data = new VT.VectorTile(new Protobuf(new Uint8Array(response)));
var data = new VT.VectorTile(new Protobuf(response));
workerTile.parse(data, layerFamilies, actor, function(err) {
if (err) return callback(err);
eachCallback();
Expand Down
2 changes: 1 addition & 1 deletion bin/point-in-polygon.js
Expand Up @@ -22,7 +22,7 @@ var data = fs.readFileSync(process.argv[2]);
zlib.inflate(data, function(err, data) {
if (err) throw err;

var tile = new VectorTile(new Protobuf(new Uint8Array(data)));
var tile = new VectorTile(new Protobuf(data));


var tree = rbush(9, ['.x1', '.y1', '.x2', '.y2']);
Expand Down
2 changes: 1 addition & 1 deletion bin/show.js
Expand Up @@ -29,7 +29,7 @@ var data = fs.readFileSync(process.argv[2]);
zlib.inflate(data, function(err, data) {
if (err) throw err;

var tile = new VectorTile(new Protobuf(new Uint8Array(data)));
var tile = new VectorTile(new Protobuf(data));

for (var layer_name in tile.layers) {
var layer = tile.layers[layer_name];
Expand Down
2 changes: 1 addition & 1 deletion bin/stats-type.js
Expand Up @@ -18,7 +18,7 @@ var data = fs.readFileSync(process.argv[2]);
zlib.inflate(data, function(err, data) {
if (err) throw err;

var tile = new VectorTile(new Protobuf(new Uint8Array(data)));
var tile = new VectorTile(new Protobuf(data));

var stats = {};
for (var layer_name in tile.layers) {
Expand Down
2 changes: 1 addition & 1 deletion bin/stats.js
Expand Up @@ -19,7 +19,7 @@ var data = fs.readFileSync(process.argv[2]);
zlib.inflate(data, function(err, data) {
if (err) throw err;

var tile = new VectorTile(new Protobuf(new Uint8Array(data)));
var tile = new VectorTile(new Protobuf(data));

var omit = ['osm_id', 'name', 'name_en', 'name_de', 'name_es', 'name_fr', 'maki', 'website', 'address', 'reflen', 'len', 'area'];

Expand Down
2 changes: 1 addition & 1 deletion js/data/feature_index.js
Expand Up @@ -98,7 +98,7 @@ function translateDistance(translate) {
// Finds features in this tile at a particular position.
FeatureIndex.prototype.query = function(args, styleLayers) {
if (!this.vtLayers) {
this.vtLayers = new vt.VectorTile(new Protobuf(new Uint8Array(this.rawTileData))).layers;
this.vtLayers = new vt.VectorTile(new Protobuf(this.rawTileData)).layers;
this.sourceLayerCoder = new DictionaryCoder(this.vtLayers ? Object.keys(this.vtLayers).sort() : ['_geojsonTileLayer']);
}

Expand Down
2 changes: 1 addition & 1 deletion js/source/tile.js
Expand Up @@ -156,7 +156,7 @@ Tile.prototype = {
if (!this.rawTileData) return;

if (!this.vtLayers) {
this.vtLayers = new vt.VectorTile(new Protobuf(new Uint8Array(this.rawTileData))).layers;
this.vtLayers = new vt.VectorTile(new Protobuf(this.rawTileData)).layers;
}

var layer = this.vtLayers._geojsonTileLayer || this.vtLayers[params.sourceLayer];
Expand Down
6 changes: 4 additions & 2 deletions js/source/vector_tile_worker_source.js
Expand Up @@ -61,9 +61,11 @@ VectorTileWorkerSource.prototype = {
tile.data = data.tile;
tile.parse(tile.data, this.styleLayers.getLayerFamilies(), this.actor, function (err, result, transferrables) {
if (err) return callback(err);

// Not transferring rawTileData because the worker needs to retain its copy.
callback(null,
util.extend({rawTileData: data.rawTileData}, result),
transferrables.concat(data.rawTileData));
transferrables);
});

this.loaded[source] = this.loaded[source] || {};
Expand Down Expand Up @@ -127,7 +129,7 @@ VectorTileWorkerSource.prototype = {
return function abort () { xhr.abort(); };
function done(err, data) {
if (err) { return callback(err); }
var tile = new vt.VectorTile(new Protobuf(new Uint8Array(data)));
var tile = new vt.VectorTile(new Protobuf(data));
callback(err, { tile: tile, rawTileData: data });
}
},
Expand Down
2 changes: 1 addition & 1 deletion js/symbol/glyph_source.js
Expand Up @@ -106,7 +106,7 @@ GlyphSource.prototype.loadRange = function(fontstack, range, callback) {
var url = glyphUrl(fontstack, rangeName, this.url);

getArrayBuffer(url, function(err, data) {
var glyphs = !err && new Glyphs(new Protobuf(new Uint8Array(data)));
var glyphs = !err && new Glyphs(new Protobuf(data));
for (var i = 0; i < loading[range].length; i++) {
loading[range][i](err, range, glyphs);
}
Expand Down
2 changes: 1 addition & 1 deletion test/js/data/fill_bucket.test.js
Expand Up @@ -11,7 +11,7 @@ var path = require('path');
var StyleLayer = require('../../../js/style/style_layer');

// Load a fill feature from fixture tile.
var vt = new VectorTile(new Protobuf(new Uint8Array(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf')))));
var vt = new VectorTile(new Protobuf(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'))));
var feature = vt.layers.water.feature(0);

function createFeature(points) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/data/line_bucket.test.js
Expand Up @@ -10,7 +10,7 @@ var LineBucket = require('../../../js/data/bucket/line_bucket');
var StyleLayer = require('../../../js/style/style_layer');

// Load a line feature from fixture tile.
var vt = new VectorTile(new Protobuf(new Uint8Array(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf')))));
var vt = new VectorTile(new Protobuf(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'))));
var feature = vt.layers.road.feature(0);

test('LineBucket', function(t) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/data/load_geometry.test.js
Expand Up @@ -8,7 +8,7 @@ var VectorTile = require('vector-tile').VectorTile;
var loadGeometry = require('../../../js/data/load_geometry.js');

// Load a line feature from fixture tile.
var vt = new VectorTile(new Protobuf(new Uint8Array(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf')))));
var vt = new VectorTile(new Protobuf(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'))));

test('loadGeometry', function(t) {
var feature = vt.layers.road.feature(0);
Expand Down
2 changes: 1 addition & 1 deletion test/js/data/symbol_bucket.test.js
Expand Up @@ -16,7 +16,7 @@ var StyleLayer = require('../../../js/style/style_layer');
var util = require('../../../js/util/util');

// Load a point feature from fixture tile.
var vt = new VectorTile(new Protobuf(new Uint8Array(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf')))));
var vt = new VectorTile(new Protobuf(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'))));
var feature = vt.layers.place_label.feature(10);
var glyphs = JSON.parse(fs.readFileSync(path.join(__dirname, '/../../fixtures/fontstack-glyphs.json')));

Expand Down
2 changes: 1 addition & 1 deletion test/js/util/classify_rings.test.js
Expand Up @@ -8,7 +8,7 @@ var VectorTile = require('vector-tile').VectorTile;
var classifyRings = require('../../../js/util/classify_rings');

// Load a fill feature from fixture tile.
var vt = new VectorTile(new Protobuf(new Uint8Array(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf')))));
var vt = new VectorTile(new Protobuf(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'))));
var feature = vt.layers.water.feature(0);

test('classifyRings', function(assert) {
Expand Down

0 comments on commit 09ab2cf

Please sign in to comment.