isard 2.1.2 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,16 @@ versions [Semantic Versioning](https://semver.org) (before 1.0.0, a minor versio
5
5
 
6
6
  ## [Unreleased]
7
7
 
8
+ ## [2.1.3] - 2026-11-01
9
+
10
+ With `isard-react-native` and `isard-map` (Python) 2.1.3.
11
+
12
+ ### Changed
13
+
14
+ - The basemap workers' decoded vector tiles lighter again: each layer's features' properties kept as indices into its
15
+ keys and values, a feature's properties' object made as they are read and kept until the worker's task ends (a tile
16
+ of Paris's middle some 3.5 MB of a worker's heap against 4.2, decoded in 4.6 ms against 5.4)
17
+
8
18
  ## [2.1.2] - 2026-11-01
9
19
 
10
20
  With `isard-react-native` and `isard-map` (Python) 2.1.2.
@@ -9,6 +9,7 @@ export type Ring = ArrayLike<number>;
9
9
  export interface TileFeature {
10
10
  /** 1 point, 2 line, 3 polygon */
11
11
  readonly type: 1 | 2 | 3;
12
+ /** Its properties (a decoded tile's: an object made from its layer's keys and values when read, kept for the task) */
12
13
  readonly properties: Record<string, Value>;
13
14
  /** Point lists (a decoded tile's: views made as they are read, of one buffer for its whole layer) */
14
15
  readonly geometry: readonly Ring[];
@@ -19,8 +20,8 @@ export interface TileLayer {
19
20
  /** The heap it takes, about (`decodedBytes`) */
20
21
  bytes: number;
21
22
  }
22
- /** The heap a decoded tile takes, about (bytes): its features with their properties, their coordinates (measured on a
23
- * city's tile in Bun's JavaScriptCore, Safari's engine) */
23
+ /** The heap a decoded tile takes, about (bytes): its features, their tags and coordinates, its keys and values
24
+ * (measured on a city's tile in Bun's JavaScriptCore, Safari's engine) */
24
25
  export declare const decodedBytes: (tile: Map<string, TileLayer>) => number;
25
26
  /** Decodes a tile; only the layers in `wanted` (all if undefined) */
26
27
  export declare const decode: (bytes: Uint8Array, wanted?: Set<string>) => Map<string, TileLayer>;
@@ -4,30 +4,50 @@
4
4
  * outer ones clockwise in tile coordinates, y down, holes the other way round, as the specification says).
5
5
  */
6
6
  /**
7
- * A decoded feature: its point lists as two indices into its layer's `Shapes`, their views made each time they are
8
- * read. A city's tile holds some hundred thousand points in thirty thousand lists: as arrays (or views kept), some 6 to
9
- * 9 MB of a worker's heap a tile, which its full collections go through again and again; in one buffer, under 1 MB
7
+ * A decoded feature: its index in its layer's `Store`, its point lists' views made each time they are read, its
8
+ * properties' object once a task. A city's tile holds some hundred thousand points in thirty thousand lists and a hundred thousand
9
+ * properties: as arrays and objects, which a worker's full collections go through again and again, some 15 MB of its
10
+ * heap a tile; in buffers, under 3 MB
10
11
  */
11
12
  class Feature {
12
13
  type;
13
- properties;
14
- shapes;
15
- first;
16
- end;
17
- constructor(type, properties, shapes, first, end) {
14
+ store;
15
+ index;
16
+ constructor(type, store, index) {
18
17
  this.type = type;
19
- this.properties = properties;
20
- this.shapes = shapes;
21
- this.first = first;
22
- this.end = end;
18
+ this.store = store;
19
+ this.index = index;
23
20
  }
24
21
  get geometry() {
25
- const { values, ranges } = this.shapes;
22
+ const { values, ranges, spans } = this.store;
26
23
  const rings = [];
27
- for (let r = this.first; r < this.end; r += 2)
24
+ const end = spans[this.index * 4 + 1] ?? 0;
25
+ for (let r = spans[this.index * 4] ?? 0; r < end; r += 2)
28
26
  rings.push(values.subarray(ranges[r], ranges[r + 1]));
29
27
  return rings;
30
28
  }
29
+ get properties() {
30
+ const { store } = this;
31
+ if (!store.made) {
32
+ store.made = [];
33
+ setTimeout(() => {
34
+ store.made = undefined;
35
+ });
36
+ }
37
+ const made = store.made[this.index] ?? this.made();
38
+ store.made[this.index] = made;
39
+ return made;
40
+ }
41
+ made() {
42
+ const { tags, keys, properties, spans } = this.store;
43
+ const out = {};
44
+ const end = spans[this.index * 4 + 3] ?? 0;
45
+ // The tags were checked against the keys and values as they were decoded
46
+ for (let t = spans[this.index * 4 + 2] ?? 0; t < end; t += 2) {
47
+ out[keys[tags[t] ?? 0] ?? ''] = properties[tags[t + 1] ?? 0] ?? '';
48
+ }
49
+ return out;
50
+ }
31
51
  }
32
52
  /** Protobuf reader of a buffer range */
33
53
  class Pbf {
@@ -205,11 +225,13 @@ const geometry = (p, out) => {
205
225
  }
206
226
  close();
207
227
  };
208
- /** The heap a decoded tile takes, about (bytes): its features with their properties, their coordinates (measured on a
209
- * city's tile in Bun's JavaScriptCore, Safari's engine) */
228
+ /** The heap a decoded tile takes, about (bytes): its features, their tags and coordinates, its keys and values
229
+ * (measured on a city's tile in Bun's JavaScriptCore, Safari's engine) */
210
230
  export const decodedBytes = (tile) => [...tile.values()].reduce((sum, layer) => sum + layer.bytes, 0);
211
- /** A feature and its properties, about (bytes) */
212
- const FEATURE_BYTES = 270;
231
+ /** A feature, about (bytes) */
232
+ const FEATURE_BYTES = 70;
233
+ /** A key or a value (a string's characters counted apart), about (bytes) */
234
+ const VALUE_BYTES = 40;
213
235
  /** Decodes a tile; only the layers in `wanted` (all if undefined) */
214
236
  export const decode = (bytes, wanted) => {
215
237
  const layers = new Map();
@@ -250,22 +272,24 @@ export const decode = (bytes, wanted) => {
250
272
  }
251
273
  if (wanted && !wanted.has(name))
252
274
  continue;
253
- const decoded = [];
275
+ const types = [];
276
+ const spans = [];
254
277
  const coordinates = new Coordinates();
278
+ const tags = [];
255
279
  for (const f of raw) {
256
280
  let type = 1;
257
- const properties = {};
258
281
  const first = coordinates.ranges.length;
282
+ const tagsFirst = tags.length;
259
283
  while (f.more) {
260
284
  const t = f.varint();
261
285
  switch (t >> 3) {
262
286
  case 2: {
263
- const tags = f.sub();
264
- while (tags.more) {
265
- const k = keys[tags.varint()];
266
- const v = values[tags.varint()];
267
- if (k !== undefined && v !== undefined)
268
- properties[k] = v;
287
+ const pairs = f.sub();
288
+ while (pairs.more) {
289
+ const k = pairs.varint();
290
+ const v = pairs.varint();
291
+ if (k < keys.length && v < values.length)
292
+ tags.push(k, v);
269
293
  }
270
294
  break;
271
295
  }
@@ -279,11 +303,24 @@ export const decode = (bytes, wanted) => {
279
303
  f.skip(t & 7);
280
304
  }
281
305
  }
282
- decoded.push([type, properties, first, coordinates.ranges.length]);
306
+ types.push(type);
307
+ spans.push(first, coordinates.ranges.length, tagsFirst, tags.length);
283
308
  }
284
- const shapes = coordinates.shapes();
285
- const features = decoded.map(([type, properties, first, end]) => new Feature(type, properties, shapes, first, end));
286
- const bytes = features.length * FEATURE_BYTES + shapes.values.byteLength + shapes.ranges.byteLength;
309
+ const store = {
310
+ ...coordinates.shapes(),
311
+ tags: Math.max(keys.length, values.length) <= 0x10000 ? Uint16Array.from(tags) : Uint32Array.from(tags),
312
+ keys,
313
+ properties: values,
314
+ spans: Uint32Array.from(spans),
315
+ };
316
+ const features = types.map((type, i) => new Feature(type, store, i));
317
+ const strings = [...keys, ...values].reduce((n, v) => n + VALUE_BYTES + (typeof v === 'string' ? v.length : 0), 0);
318
+ const bytes = features.length * FEATURE_BYTES +
319
+ store.values.byteLength +
320
+ store.ranges.byteLength +
321
+ store.tags.byteLength +
322
+ store.spans.byteLength +
323
+ strings;
287
324
  layers.set(name, { extent, features, bytes });
288
325
  }
289
326
  return layers;
@@ -1 +1 @@
1
- {"version":3,"file":"mvt.js","sourceRoot":"","sources":["../../src/basemap/mvt.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAqBH;;;;GAIG;AACH,MAAM,OAAO;IAEA;IACA;IACQ;IACA;IACA;IALnB,YACW,IAAe,EACf,UAAiC,EACzB,MAAc,EACd,KAAa,EACb,GAAW;QAJnB,SAAI,GAAJ,IAAI,CAAW;QACf,eAAU,GAAV,UAAU,CAAuB;QACzB,WAAM,GAAN,MAAM,CAAQ;QACd,UAAK,GAAL,KAAK,CAAQ;QACb,QAAG,GAAH,GAAG,CAAQ;IAC3B,CAAC;IAEJ,IAAI,QAAQ;QACV,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;QACvC,MAAM,KAAK,GAAiB,EAAE,CAAC;QAE/B,KA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;oBACD,KAAK,CAAC;wBACJ,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAc,CAAC;wBACzD,MAAM;oBACR,KAAK,CAAC;wBACJ,QAAQ,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE,WAAW,CAAC,CAAC;wBAC/B,MAAM;oBACR;wBACE,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;gBAClB,CAAC;YACH,CAAC;YACD,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QACrE,CAAC;QAED,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC;QACpC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;QACpH,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,GAAG,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC;QAEpG,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["/**\n * Mapbox Vector Tiles (MVT 2.1) decoding: layers of features with their properties and geometry, in tile units\n * (`extent`, usually 4096 across). Points are lists of points, lines lists of lines, polygons lists of rings (the\n * outer ones clockwise in tile coordinates, y down, holes the other way round, as the specification says).\n */\n\nexport type Value = string | number | boolean;\n\n/** A point list: x0, y0, x1, y1… in tile units */\nexport type Ring = ArrayLike<number>;\n\nexport interface TileFeature {\n /** 1 point, 2 line, 3 polygon */\n readonly type: 1 | 2 | 3;\n readonly properties: Record<string, Value>;\n /** Point lists (a decoded tile's: views made as they are read, of one buffer for its whole layer) */\n readonly geometry: readonly Ring[];\n}\n\n/** A layer's coordinates, one feature's after the other, and where each point list starts and ends */\ninterface Shapes {\n values: Int32Array;\n ranges: Uint32Array;\n}\n\n/**\n * A decoded feature: its point lists as two indices into its layer's `Shapes`, their views made each time they are\n * read. A city's tile holds some hundred thousand points in thirty thousand lists: as arrays (or views kept), some 6 to\n * 9 MB of a worker's heap a tile, which its full collections go through again and again; in one buffer, under 1 MB\n */\nclass Feature implements TileFeature {\n constructor(\n readonly type: 1 | 2 | 3,\n readonly properties: Record<string, Value>,\n private readonly shapes: Shapes,\n private readonly first: number,\n private readonly end: number,\n ) {}\n\n get geometry(): Int32Array[] {\n const { values, ranges } = this.shapes;\n const rings: Int32Array[] = [];\n\n for (let r = this.first; r < this.end; r += 2) rings.push(values.subarray(ranges[r], ranges[r + 1]));\n\n return rings;\n }\n}\n\nexport interface TileLayer {\n extent: number;\n features: TileFeature[];\n /** The heap it takes, about (`decodedBytes`) */\n bytes: number;\n}\n\n/** Protobuf reader of a buffer range */\nclass Pbf {\n pos: number;\n constructor(\n private readonly buf: Uint8Array,\n start = 0,\n readonly end = buf.length,\n ) {\n this.pos = start;\n }\n /** An unsigned varint, approximated beyond 2^53; all its bytes are read (up to 10), so the next field is right */\n varint(): number {\n let value = 0;\n\n for (let shift = 0; shift < 70; shift += 7) {\n const b = this.buf[this.pos++] ?? 0;\n\n value += (b & 0x7f) * 2 ** shift;\n if (b < 0x80) break;\n }\n\n return value;\n }\n /** A signed 64-bit varint (two's complement: a negative value takes 10 bytes) */\n int64(): number {\n let lo = 0;\n let hi = 0;\n\n for (let i = 0; i < 10; i += 1) {\n const b = this.buf[this.pos++] ?? 0;\n const bits = b & 0x7f;\n\n // Bits 0-27 in the low word, bits 28-31 split, the rest in the high word\n if (i < 4) lo |= bits << (7 * i);\n else if (i === 4) {\n lo |= (bits & 0x0f) << 28;\n hi |= bits >> 4;\n } else hi |= bits << (7 * i - 32);\n if (b < 0x80) break;\n }\n\n // The high word signed: values from 2^63 up are negative\n return (hi | 0) * 2 ** 32 + (lo >>> 0);\n }\n sint(): number {\n const v = this.varint();\n\n return v % 2 === 1 ? -(v + 1) / 2 : v / 2;\n }\n /** A length-delimited field as a sub-reader */\n sub(): Pbf {\n const n = this.varint();\n // A length past the end (a truncated tile) stops at the end\n const end = Math.min(this.pos + n, this.end);\n const r = new Pbf(this.buf, this.pos, end);\n\n this.pos = end;\n\n return r;\n }\n string(): string {\n const n = this.varint();\n const end = Math.min(this.pos + n, this.end);\n const s = decoder.decode(this.buf.subarray(this.pos, end));\n\n this.pos = end;\n\n return s;\n }\n float(): number {\n const v = this.pos + 4 <= this.end ? this.view().getFloat32(this.pos, true) : 0;\n\n this.pos += 4;\n\n return v;\n }\n double(): number {\n const v = this.pos + 8 <= this.end ? this.view().getFloat64(this.pos, true) : 0;\n\n this.pos += 8;\n\n return v;\n }\n private view(): DataView {\n return new DataView(this.buf.buffer, this.buf.byteOffset, this.buf.byteLength);\n }\n skip(wire: number): void {\n if (wire === 0) this.varint();\n else if (wire === 1) this.pos += 8;\n else if (wire === 2) this.pos += this.varint();\n else if (wire === 5) this.pos += 4;\n }\n get more(): boolean {\n return this.pos < this.end;\n }\n}\n\nconst decoder = new TextDecoder();\n\nconst value = (p: Pbf): Value => {\n let v: Value = '';\n\n while (p.more) {\n const tag = p.varint();\n\n switch (tag >> 3) {\n case 1:\n v = p.string();\n break;\n case 2:\n v = p.float();\n break;\n case 3:\n v = p.double();\n break;\n case 4:\n v = p.int64();\n break;\n case 5:\n v = p.varint();\n break;\n case 6:\n v = p.sint();\n break;\n case 7:\n v = p.varint() !== 0;\n break;\n default:\n p.skip(tag & 7);\n }\n }\n\n return v;\n};\n\n/** The coordinates of a layer's features, one after the other, and where each point list starts and ends */\nclass Coordinates {\n values = new Int32Array(1024);\n length = 0;\n /** Per point list: its first value and its end */\n readonly ranges: number[] = [];\n\n push(x: number, y: number): void {\n if (this.length + 2 > this.values.length) {\n const grown = new Int32Array(this.values.length * 2);\n\n grown.set(this.values);\n this.values = grown;\n }\n this.values[this.length++] = x;\n this.values[this.length++] = y;\n }\n\n /** The values and ranges cut to their lengths (once all are pushed) */\n shapes(): Shapes {\n return { values: this.values.slice(0, this.length), ranges: Uint32Array.from(this.ranges) };\n }\n}\n\n/** Pushes the point lists of a geometry's commands (MoveTo, LineTo, ClosePath) */\nconst geometry = (p: Pbf, out: Coordinates): void => {\n let [x, y] = [0, 0];\n // The first value of the current point list (none before a MoveTo)\n let start = -1;\n const close = () => {\n if (start >= 0) out.ranges.push(start, out.length);\n };\n\n while (p.more) {\n const command = p.varint();\n const id = command & 7;\n const count = command >> 3;\n\n if (id === 7) {\n // ClosePath: back to the first point\n if (start >= 0 && out.length > start) out.push(out.values[start] ?? 0, out.values[start + 1] ?? 0);\n continue;\n }\n // A count past the end (a truncated geometry) stops there\n for (let i = 0; i < count && p.more; i += 1) {\n x += p.sint();\n y += p.sint();\n if (id === 1) {\n close();\n start = out.length;\n }\n if (start >= 0) out.push(x, y);\n }\n }\n close();\n};\n\n/** The heap a decoded tile takes, about (bytes): its features with their properties, their coordinates (measured on a\n * city's tile in Bun's JavaScriptCore, Safari's engine) */\nexport const decodedBytes = (tile: Map<string, TileLayer>): number =>\n [...tile.values()].reduce((sum, layer) => sum + layer.bytes, 0);\n/** A feature and its properties, about (bytes) */\nconst FEATURE_BYTES = 270;\n\n/** Decodes a tile; only the layers in `wanted` (all if undefined) */\nexport const decode = (bytes: Uint8Array, wanted?: Set<string>): Map<string, TileLayer> => {\n const layers = new Map<string, TileLayer>();\n const tile = new Pbf(bytes);\n\n while (tile.more) {\n const tag = tile.varint();\n\n if (tag >> 3 !== 3) {\n tile.skip(tag & 7);\n continue;\n }\n\n const l = tile.sub();\n let name = '';\n let extent = 4096;\n const keys: string[] = [];\n const values: Value[] = [];\n const raw: Pbf[] = [];\n\n while (l.more) {\n const t = l.varint();\n\n switch (t >> 3) {\n case 1:\n name = l.string();\n break;\n case 2:\n raw.push(l.sub());\n break;\n case 3:\n keys.push(l.string());\n break;\n case 4:\n values.push(value(l.sub()));\n break;\n case 5:\n extent = l.varint();\n break;\n default:\n l.skip(t & 7);\n }\n }\n if (wanted && !wanted.has(name)) continue;\n\n const decoded: [type: 1 | 2 | 3, properties: Record<string, Value>, first: number, end: number][] = [];\n const coordinates = new Coordinates();\n\n for (const f of raw) {\n let type: 1 | 2 | 3 = 1;\n const properties: Record<string, Value> = {};\n const first = coordinates.ranges.length;\n\n while (f.more) {\n const t = f.varint();\n\n switch (t >> 3) {\n case 2: {\n const tags = f.sub();\n\n while (tags.more) {\n const k = keys[tags.varint()];\n const v = values[tags.varint()];\n\n if (k !== undefined && v !== undefined) properties[k] = v;\n }\n break;\n }\n case 3:\n type = Math.min(3, Math.max(1, f.varint())) as 1 | 2 | 3;\n break;\n case 4:\n geometry(f.sub(), coordinates);\n break;\n default:\n f.skip(t & 7);\n }\n }\n decoded.push([type, properties, first, coordinates.ranges.length]);\n }\n\n const shapes = coordinates.shapes();\n const features = decoded.map(([type, properties, first, end]) => new Feature(type, properties, shapes, first, end));\n const bytes = features.length * FEATURE_BYTES + shapes.values.byteLength + shapes.ranges.byteLength;\n\n layers.set(name, { extent, features, bytes });\n }\n\n return layers;\n};\n"]}
1
+ {"version":3,"file":"mvt.js","sourceRoot":"","sources":["../../src/basemap/mvt.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAkCH;;;;;GAKG;AACH,MAAM,OAAO;IAEA;IACQ;IACA;IAHnB,YACW,IAAe,EACP,KAAY,EACZ,KAAa;QAFrB,SAAI,GAAJ,IAAI,CAAW;QACP,UAAK,GAAL,KAAK,CAAO;QACZ,UAAK,GAAL,KAAK,CAAQ;IAC7B,CAAC;IAE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F,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;QACrE,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC,CAAC,MAAM,CACzC,CAAC,CAAS,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,WAAW,GAAG,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAC1E,CAAC,CACF,CAAC;QACF,MAAM,KAAK,GACT,QAAQ,CAAC,MAAM,GAAG,aAAa;YAC/B,KAAK,CAAC,MAAM,CAAC,UAAU;YACvB,KAAK,CAAC,MAAM,CAAC,UAAU;YACvB,KAAK,CAAC,IAAI,CAAC,UAAU;YACrB,KAAK,CAAC,KAAK,CAAC,UAAU;YACtB,OAAO,CAAC;QAEV,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["/**\n * Mapbox Vector Tiles (MVT 2.1) decoding: layers of features with their properties and geometry, in tile units\n * (`extent`, usually 4096 across). Points are lists of points, lines lists of lines, polygons lists of rings (the\n * outer ones clockwise in tile coordinates, y down, holes the other way round, as the specification says).\n */\n\nexport type Value = string | number | boolean;\n\n/** A point list: x0, y0, x1, y1… in tile units */\nexport type Ring = ArrayLike<number>;\n\nexport interface TileFeature {\n /** 1 point, 2 line, 3 polygon */\n readonly type: 1 | 2 | 3;\n /** Its properties (a decoded tile's: an object made from its layer's keys and values when read, kept for the task) */\n readonly properties: Record<string, Value>;\n /** Point lists (a decoded tile's: views made as they are read, of one buffer for its whole layer) */\n readonly geometry: readonly Ring[];\n}\n\n/**\n * A layer's features in buffers: their coordinates one feature's after the other and where each point list starts and\n * ends; their tags one feature's after the other (a key's index then a value's) and the keys and values they index;\n * per feature, where its point lists and its tags start and end\n */\ninterface Store {\n values: Int32Array;\n ranges: Uint32Array;\n tags: Uint16Array | Uint32Array;\n keys: string[];\n properties: Value[];\n /** Per feature: its first point list and their end (in `ranges`), its first tag and their end (in `tags`) */\n spans: Uint32Array;\n /** The properties' objects made in the current task, per feature: the layers of a style read a feature's again and\n * again, the tile kept only as buffers between tasks */\n made?: Record<string, Value>[] | undefined;\n}\n\n/**\n * A decoded feature: its index in its layer's `Store`, its point lists' views made each time they are read, its\n * properties' object once a task. A city's tile holds some hundred thousand points in thirty thousand lists and a hundred thousand\n * properties: as arrays and objects, which a worker's full collections go through again and again, some 15 MB of its\n * heap a tile; in buffers, under 3 MB\n */\nclass Feature implements TileFeature {\n constructor(\n readonly type: 1 | 2 | 3,\n private readonly store: Store,\n private readonly index: number,\n ) {}\n\n get geometry(): Int32Array[] {\n const { values, ranges, spans } = this.store;\n const rings: Int32Array[] = [];\n const end = spans[this.index * 4 + 1] ?? 0;\n\n for (let r = spans[this.index * 4] ?? 0; r < end; r += 2) rings.push(values.subarray(ranges[r], ranges[r + 1]));\n\n return rings;\n }\n\n get properties(): Record<string, Value> {\n const { store } = this;\n\n if (!store.made) {\n store.made = [];\n setTimeout(() => {\n store.made = undefined;\n });\n }\n\n const made = store.made[this.index] ?? this.made();\n\n store.made[this.index] = made;\n\n return made;\n }\n\n private made(): Record<string, Value> {\n const { tags, keys, properties, spans } = this.store;\n const out: Record<string, Value> = {};\n const end = spans[this.index * 4 + 3] ?? 0;\n\n // The tags were checked against the keys and values as they were decoded\n for (let t = spans[this.index * 4 + 2] ?? 0; t < end; t += 2) {\n out[keys[tags[t] ?? 0] ?? ''] = properties[tags[t + 1] ?? 0] ?? '';\n }\n\n return out;\n }\n}\n\nexport interface TileLayer {\n extent: number;\n features: TileFeature[];\n /** The heap it takes, about (`decodedBytes`) */\n bytes: number;\n}\n\n/** Protobuf reader of a buffer range */\nclass Pbf {\n pos: number;\n constructor(\n private readonly buf: Uint8Array,\n start = 0,\n readonly end = buf.length,\n ) {\n this.pos = start;\n }\n /** An unsigned varint, approximated beyond 2^53; all its bytes are read (up to 10), so the next field is right */\n varint(): number {\n let value = 0;\n\n for (let shift = 0; shift < 70; shift += 7) {\n const b = this.buf[this.pos++] ?? 0;\n\n value += (b & 0x7f) * 2 ** shift;\n if (b < 0x80) break;\n }\n\n return value;\n }\n /** A signed 64-bit varint (two's complement: a negative value takes 10 bytes) */\n int64(): number {\n let lo = 0;\n let hi = 0;\n\n for (let i = 0; i < 10; i += 1) {\n const b = this.buf[this.pos++] ?? 0;\n const bits = b & 0x7f;\n\n // Bits 0-27 in the low word, bits 28-31 split, the rest in the high word\n if (i < 4) lo |= bits << (7 * i);\n else if (i === 4) {\n lo |= (bits & 0x0f) << 28;\n hi |= bits >> 4;\n } else hi |= bits << (7 * i - 32);\n if (b < 0x80) break;\n }\n\n // The high word signed: values from 2^63 up are negative\n return (hi | 0) * 2 ** 32 + (lo >>> 0);\n }\n sint(): number {\n const v = this.varint();\n\n return v % 2 === 1 ? -(v + 1) / 2 : v / 2;\n }\n /** A length-delimited field as a sub-reader */\n sub(): Pbf {\n const n = this.varint();\n // A length past the end (a truncated tile) stops at the end\n const end = Math.min(this.pos + n, this.end);\n const r = new Pbf(this.buf, this.pos, end);\n\n this.pos = end;\n\n return r;\n }\n string(): string {\n const n = this.varint();\n const end = Math.min(this.pos + n, this.end);\n const s = decoder.decode(this.buf.subarray(this.pos, end));\n\n this.pos = end;\n\n return s;\n }\n float(): number {\n const v = this.pos + 4 <= this.end ? this.view().getFloat32(this.pos, true) : 0;\n\n this.pos += 4;\n\n return v;\n }\n double(): number {\n const v = this.pos + 8 <= this.end ? this.view().getFloat64(this.pos, true) : 0;\n\n this.pos += 8;\n\n return v;\n }\n private view(): DataView {\n return new DataView(this.buf.buffer, this.buf.byteOffset, this.buf.byteLength);\n }\n skip(wire: number): void {\n if (wire === 0) this.varint();\n else if (wire === 1) this.pos += 8;\n else if (wire === 2) this.pos += this.varint();\n else if (wire === 5) this.pos += 4;\n }\n get more(): boolean {\n return this.pos < this.end;\n }\n}\n\nconst decoder = new TextDecoder();\n\nconst value = (p: Pbf): Value => {\n let v: Value = '';\n\n while (p.more) {\n const tag = p.varint();\n\n switch (tag >> 3) {\n case 1:\n v = p.string();\n break;\n case 2:\n v = p.float();\n break;\n case 3:\n v = p.double();\n break;\n case 4:\n v = p.int64();\n break;\n case 5:\n v = p.varint();\n break;\n case 6:\n v = p.sint();\n break;\n case 7:\n v = p.varint() !== 0;\n break;\n default:\n p.skip(tag & 7);\n }\n }\n\n return v;\n};\n\n/** The coordinates of a layer's features, one after the other, and where each point list starts and ends */\nclass Coordinates {\n values = new Int32Array(1024);\n length = 0;\n /** Per point list: its first value and its end */\n readonly ranges: number[] = [];\n\n push(x: number, y: number): void {\n if (this.length + 2 > this.values.length) {\n const grown = new Int32Array(this.values.length * 2);\n\n grown.set(this.values);\n this.values = grown;\n }\n this.values[this.length++] = x;\n this.values[this.length++] = y;\n }\n\n /** The values and ranges cut to their lengths (once all are pushed) */\n shapes(): Pick<Store, 'values' | 'ranges'> {\n return { values: this.values.slice(0, this.length), ranges: Uint32Array.from(this.ranges) };\n }\n}\n\n/** Pushes the point lists of a geometry's commands (MoveTo, LineTo, ClosePath) */\nconst geometry = (p: Pbf, out: Coordinates): void => {\n let [x, y] = [0, 0];\n // The first value of the current point list (none before a MoveTo)\n let start = -1;\n const close = () => {\n if (start >= 0) out.ranges.push(start, out.length);\n };\n\n while (p.more) {\n const command = p.varint();\n const id = command & 7;\n const count = command >> 3;\n\n if (id === 7) {\n // ClosePath: back to the first point\n if (start >= 0 && out.length > start) out.push(out.values[start] ?? 0, out.values[start + 1] ?? 0);\n continue;\n }\n // A count past the end (a truncated geometry) stops there\n for (let i = 0; i < count && p.more; i += 1) {\n x += p.sint();\n y += p.sint();\n if (id === 1) {\n close();\n start = out.length;\n }\n if (start >= 0) out.push(x, y);\n }\n }\n close();\n};\n\n/** The heap a decoded tile takes, about (bytes): its features, their tags and coordinates, its keys and values\n * (measured on a city's tile in Bun's JavaScriptCore, Safari's engine) */\nexport const decodedBytes = (tile: Map<string, TileLayer>): number =>\n [...tile.values()].reduce((sum, layer) => sum + layer.bytes, 0);\n/** A feature, about (bytes) */\nconst FEATURE_BYTES = 70;\n/** A key or a value (a string's characters counted apart), about (bytes) */\nconst VALUE_BYTES = 40;\n\n/** Decodes a tile; only the layers in `wanted` (all if undefined) */\nexport const decode = (bytes: Uint8Array, wanted?: Set<string>): Map<string, TileLayer> => {\n const layers = new Map<string, TileLayer>();\n const tile = new Pbf(bytes);\n\n while (tile.more) {\n const tag = tile.varint();\n\n if (tag >> 3 !== 3) {\n tile.skip(tag & 7);\n continue;\n }\n\n const l = tile.sub();\n let name = '';\n let extent = 4096;\n const keys: string[] = [];\n const values: Value[] = [];\n const raw: Pbf[] = [];\n\n while (l.more) {\n const t = l.varint();\n\n switch (t >> 3) {\n case 1:\n name = l.string();\n break;\n case 2:\n raw.push(l.sub());\n break;\n case 3:\n keys.push(l.string());\n break;\n case 4:\n values.push(value(l.sub()));\n break;\n case 5:\n extent = l.varint();\n break;\n default:\n l.skip(t & 7);\n }\n }\n if (wanted && !wanted.has(name)) continue;\n\n const types: (1 | 2 | 3)[] = [];\n const spans: number[] = [];\n const coordinates = new Coordinates();\n const tags: number[] = [];\n\n for (const f of raw) {\n let type: 1 | 2 | 3 = 1;\n const first = coordinates.ranges.length;\n const tagsFirst = tags.length;\n\n while (f.more) {\n const t = f.varint();\n\n switch (t >> 3) {\n case 2: {\n const pairs = f.sub();\n\n while (pairs.more) {\n const k = pairs.varint();\n const v = pairs.varint();\n\n if (k < keys.length && v < values.length) tags.push(k, v);\n }\n break;\n }\n case 3:\n type = Math.min(3, Math.max(1, f.varint())) as 1 | 2 | 3;\n break;\n case 4:\n geometry(f.sub(), coordinates);\n break;\n default:\n f.skip(t & 7);\n }\n }\n types.push(type);\n spans.push(first, coordinates.ranges.length, tagsFirst, tags.length);\n }\n\n const store: Store = {\n ...coordinates.shapes(),\n tags: Math.max(keys.length, values.length) <= 0x10000 ? Uint16Array.from(tags) : Uint32Array.from(tags),\n keys,\n properties: values,\n spans: Uint32Array.from(spans),\n };\n const features = types.map((type, i) => new Feature(type, store, i));\n const strings = [...keys, ...values].reduce(\n (n: number, v) => n + VALUE_BYTES + (typeof v === 'string' ? v.length : 0),\n 0,\n );\n const bytes =\n features.length * FEATURE_BYTES +\n store.values.byteLength +\n store.ranges.byteLength +\n store.tags.byteLength +\n store.spans.byteLength +\n strings;\n\n layers.set(name, { extent, features, bytes });\n }\n\n return layers;\n};\n"]}