Skip to content

The vertex-buffer pipeline

These pages explain how the thesis encodes the geometry of a MapLibre Tile (MLT). Every layer of a tile stores the coordinates of its vertices in a VertexBuffer. Stock MLT writes it as the difference between consecutive vertices, packed by a generic integer codec. The pipeline replaces this with a chain of small, reversible stages. Each stage exploits one property of map geometry (features close together, right-angled buildings, straight roads, shared walls and junctions), and every stage is built to feed the general-purpose compressor (gzip, Brotli or zstd) that the tile is delivered with.

Gains against stock MLT v2

The pipeline runs inside the MLT v2 encoder (mlt-core, Rust) as its vertex encoder. Everything else in the tile (topology, ids, property columns) is written by the stock encoder. Stock picks the encoding of each of those streams by its raw size; the rows marked gzip stream choice pick among the same encodings by gzip size instead, so the format does not change. The sizes are whole tiles as a client receives them: the Munich corpus, 358 tiles, every layer, gzip level 6, summed over all tiles.

encoder raw bytes gzip bytes gzip vs stock gzip vs MVT
MVT (the source tiles) 31.580.127 21.090.045 +25,64%
stock MLT v2 18.487.134 16.786.524 -20,41%
stock MLT v2 with its own feature sorting 18.317.963 16.643.440 -0,85% -21,08%
stock MLT v2, gzip stream choice 20.721.218 16.083.379 -4,19% -23,74%
pipeline, plain delta + escape planes on every layer 19.810.846 16.279.891 -3,02% -22,81%
pipeline, per-layer default composition 19.255.816 15.843.951 -5,62% -24,87%
pipeline, tile oracle without feature reorder 18.998.820 15.384.854 -8,35% -27,05%
pipeline, tile oracle 18.936.228 15.319.927 -8,74% -27,36%
pipeline, tile oracle, gzip stream choice 20.559.369 15.180.223 -9,57% -28,02%

The MVT row is the source tileset (munich-plantiler.mbtiles, Planetiler), every tile decompressed and gzipped again at level 6 with Python's zlib; this reproduces the stored tiles to the byte. Every MLT row is converted from these tiles and holds the same features. Under gzip the MVT tiles are 1,39 times the size of the best row.

The tile oracle with gzip stream choice saves 1,61 MB of 16,79 MB. Four things stand out:

  • The pipeline's tiles are larger before compression and smaller after it. The stages make the bytes easy to compress, not small on their own.
  • Choosing the stock encodings by gzip size alone makes stock tiles 4,19% smaller. Against that stock row, which uses the same criterion, the pipeline's own gain is -5,62%. On top of the tile oracle the gzip stream choice adds 0,83 points.
  • Of the 8,74 points with the stock stream choice, 3,02 come from writing the vertices in a gzip-friendly form at all. The predictors, frames, references and stream layouts add 5,3 points, the feature reorder 0,4.
  • All 358 tiles of both tile oracles decode, and every feature keeps its shape (the stock gzip stream choice tiles were not decoded). What may change is what a renderer does not draw differently: feature order, the start vertex of a ring, the order of points in a MultiPoint, and the direction of some line parts (which matters only for direction-dependent symbols such as one-way arrows).

The gain sits where the geometry has structure. Each layer compressed on its own (tile oracle with gzip stream choice):

layer stock MLT v2 tile oracle change
building 4.287.027 3.460.555 -19,28%
transportation 3.608.695 3.236.256 -10,32%
poi 3.035.607 3.001.050 -1,14%
landcover 1.748.145 1.512.349 -13,49%
housenumber 1.239.693 1.268.414 +2,32%
transportation_name 892.802 832.662 -6,74%
landuse 876.416 796.522 -9,12%
all 16 layers 17.064.891 15.433.214 -9,56%

(Per layer and the two tile oracle rows without gzip stream choice above: measured with the earlier index of the shared references, codes 7, 25 and 12; the raw slot index of rings and lines, codes 26, 27 and 28, moves the whole tile by +0,05%.)

Buildings gain most from corner guesses, wall frames and shared walls, landcover from shared borders, roads from continuation and junctions. Point layers have little shape to exploit: poi ends 1,14% below stock, housenumber 2,32% above.

Decoding cost

The smaller tiles cost decode time. Whole tiles, the same 358 tiles, parse and decode of every layer, decompression not timed, single-threaded, per tile the minimum of 10 runs:

encoder decode ms × stock
stock MLT v2 120 1,00
pipeline, per-layer default composition 290 2,42
pipeline, tile oracle, gzip stream choice 262 2,19

The middle of three runs; the machine was not idle, so the milliseconds move between sessions and only the ratios compare (the three runs: 2,42 to 2,52 and 2,18 to 2,25). The index into raw slots instead of distinct positions took 7% off the tile oracle's decode time for the ring references (865 bytes) and another 3% for the line references (6.470 bytes) (shared references). Measured before that change: by zoom the ratio is lowest at z10 to z12 (1,8 to 2,5 × stock) and highest at z14 (2,6 to 2,9 ×). The tile oracle decodes faster than the per-layer default; it writes 34,7% of the layers with plain DELTA. What the ring frame adds on its own is on the frames page. The thesis target is compressed size per tile, so this cost is reported, not optimised.

mlt convert ../data/MVT/munich-plantiler.mbtiles stock.mbtiles --mlt-version 2
mlt-oracle decode-time stock.mbtiles default.mbtiles oracle.mbtiles --by-zoom

Full results, by zoom and by layer: mlt-v2-rust-port.md.

Across cities

The same encoder on four cities: the tile oracle with gzip stream choice (the 42 compositions, codes 26, 27 and 28) against stock MLT v2 and the source MVT tiles. Munich is the corpus above (munich-plantiler, Planetiler 0.10.2); Tokyo, Chicago and São Paulo are 25 km extracts of the same schema. Every tile gzip level 6, summed over the tileset:

city tiles MVT stock MLT v2 tile oracle vs stock v2 vs MVT decode × stock v2
Munich 358 21.090.045 16.854.860 15.180.223 -9,94% -28,02% 2,22 (2,21 to 2,23)
Tokyo 278 51.562.148 42.376.064 37.164.623 -12,30% -27,92% 2,20 (2,15 to 2,20)
Chicago 338 24.994.575 21.853.388 19.322.918 -11,58% -22,69% 1,84 (1,79 to 1,88)
São Paulo 214 32.326.807 27.705.767 23.946.072 -13,57% -25,93% 4,98 (4,60 to 5,06)
all four 1.188 129.973.575 108.790.079 95.613.836 -12,11% -26,44%
  • Stock v2 here is mlt convert --mlt-version 2 --vertex-pipeline off (16.854.860 B on Munich); the tables above take stock from mlt-oracle compare (16.786.524 B). Both are the stock encoder; why they differ by 0,41% was not looked into, so the Munich figures of the two tables do not mix.
  • MVT is the source tileset, every tile decompressed and gzipped again at level 6 (Python's zlib); this reproduces the stored tiles to the byte in all four.
  • The gain against stock is smallest in Munich and largest in São Paulo; against MVT it is smallest in Chicago, where stock MLT already gains least on MVT (-12,57%, against -20,08% in Munich).
  • Decode: whole tiles, parse and decode of every layer, decompression not timed, single-threaded, per tile the minimum of 10 runs; the middle of three runs, the range in brackets. São Paulo is the outlier: its z14 tiles decode at 5,75 × stock (z12 1,89 ×, z13 1,78 ×); not yet looked into.
  • Every oracle tileset was converted with --verify, and mlt-oracle diff against the MVT input reports the same tiles, layers and features for all four. Conversion took 5 min (Munich), 17,5 min (Tokyo), 8,3 min (Chicago) and 11 min (São Paulo).
mlt convert ../data/MVT/<city>.mbtiles stock.mbtiles --mlt-version 2 --gzip --vertex-pipeline off
mlt convert ../data/MVT/<city>.mbtiles oracle.mbtiles --mlt-version 2 --gzip --vertex-pipeline oracle --verify
mlt-oracle decode-time stock.mbtiles oracle.mbtiles

How the encoding builds on itself

Each stage takes the output of the stage before it and makes it easier for the next one. The topology streams, which stock MLT writes and the decoder reads first, run alongside: the stages derive everything structural from them, so none of it is written into the vertex payload.

flowchart TD
    layer["MVT layer<br/>features with geometry<br/>and properties"]
    s1["Reorder (S1)<br/>close features together,<br/>rings start near<br/>the previous vertex"]
    topo["Topology streams<br/>types, parts, rings,<br/>vertex counts"]
    vb["Vertex buffer<br/>absolute x, y<br/>of every vertex"]
    s2["Prediction (S2)<br/>guess each vertex<br/>from decoded ones,<br/>keep the residual"]
    s2f["Frames (S2)<br/>rotate the residual into<br/>the wall direction"]
    s2r["Shared references (S2)<br/>point back at vertices<br/>an earlier ring or line<br/>already holds"]
    s3["Segmentation (S3)<br/>one stream<br/>per vertex class"]
    s4["Zigzag (S4)<br/>signed to unsigned"]
    s6["Byte width (S6)<br/>escape bytes"]
    s7["Byte planes (S7)<br/>low bytes first,<br/>then high bytes"]
    payload["Payload of one buffer<br/>2-byte header + streams"]
    s8["Backend (S8)<br/>all layers of the tile,<br/>compressed once"]

    layer --> s1
    s1 --> topo
    s1 --> vb
    vb -->|"same vertices,<br/>shorter jumps"| s2
    s2 -->|"small signed residuals"| s2f
    s2f -->|"one component near zero"| s2r
    s2r -->|"residuals and reference bits"| s3
    s3 -->|"homogeneous streams"| s4
    s4 -->|"small unsigned values"| s6
    s6 -->|"mostly single bytes"| s7
    s7 --> payload
    payload --> s8
    topo -.->|"runs and vertex classes,<br/>never written"| s2
    topo -.->|"stream lengths,<br/>never written"| s3

The same build-up in numbers, on whole MLT v2 tiles (gzip, summed over the 358 tiles), one decision added per step:

step gzip bytes vs stock this step
stock MLT v2 16.786.524
every layer: plain delta + zigzag + escape planes 16.279.891 -3,02% -3,02%
per layer the best of the 14 compositions without a feature reorder (tile oracle) 15.384.854 -8,35% -5,50%
+ the 28 compositions with a feature reorder 15.319.927 -8,74% -0,42%
+ gzip stream choice for the other streams 15.172.888 -9,61% -0,96%

(The build-up was measured with the earlier index of the shared references, codes 7, 25 and 12; with the raw slot index, codes 26, 27 and 28, the last row is 15.180.223 B.) The codec's own stages (prediction, frames, references, class streams) are the largest step. The reorder shortens the jumps between features, but it permutes the property columns too and often costs more there than it saves on the geometry, so the tile oracle uses it on 27,4% of the layers only (compositions).

mlt-oracle compare --mbtiles ../data/MVT/munich-plantiler.mbtiles --by-zoom --by-layer

How a composition is chosen

Every stage has a few options, and a composition is one choice per stage. The decoder reads all choices but the reorder from the two header bytes of each buffer, and the reorder needs nothing undone, so how a composition was chosen never matters to the decoder. Today there are three ways to choose:

  • the per-layer default, one fixed composition per MVT layer, fitted once on the corpus;
  • the tile oracle, which tries 42 compositions on every layer of every tile and keeps the combination that makes the whole gzipped tile smallest;
  • one fixed composition for every layer.

The compositions page explains each, with the search loop of the tile oracle.

Rules every stage follows

  • Nothing derivable is written. The decoder reads the topology before the vertex buffer, and from it derives the runs (one per ring or line part), the class of every vertex and the length of every stream. Only the stream lengths of a buffer with shared references are ever written.
  • Lossless. Every composition round-trips every buffer, checked by the harness and by the test suite. The only freedoms used are the ones a renderer cannot see: feature order, ring start, MultiPoint order, and the direction of line parts reversed by the shared line references.
  • Self-describing. Every choice except the reorder is in the two header bytes, so a buffer decodes from its bytes and the topology alone.

One buffer, end to end

A rectangular building, first in its buffer (so the previous vertex is the origin), with the composition: no reorder, QUAD_CORNER, class streams, zigzag, escape width, byte planes.

vertices        p0=(100,200)  p1=(130,200)  p2=(130,240)  p3=(100,240)
class           START         INTERIOR      INTERIOR      CORNER
guess           (0,0)         p0            p1            p0 + p2 - p1 = (100,240)
residual        (100,200)     (30,0)        (0,40)        (0,0)

streams         START: 100 200 | INTERIOR: 30 0 0 40 | CORNER: 0 0
zigzag          START: 200 400 | INTERIOR: 60 0 0 80 | CORNER: 0 0
bytes           START:    04 C8 FF 91      descriptor (1-byte values, escape, 1-byte exceptions), 200, marker, 400-255
                INTERIOR: 00 3C 00 00 50   descriptor (1-byte values), values
                CORNER:   00 00 00
header          49 03
payload         49 03 04 C8 FF 91 00 3C 00 00 50 00 00 00   (14 bytes)

The decoder reads the header, derives from the topology that the ring has four vertices (classes START INTERIOR INTERIOR CORNER, so 2 + 4 + 2 values), reads the three streams, undoes zigzag and walks the vertices, adding each residual to the same guess.

The building from the example: its four vertices fall into three classes, each class is one stream, and the streams become 14 bytes behind the 2-byte header.

The pages

  1. Compositions: what a composition is and how one is chosen.
  2. Reorder: emit close features next to each other.
  3. Prediction: guess each vertex from the ones before it.
  4. Frames: write residuals in the direction of the wall.
  5. Shared references: point back at repeated vertices.
  6. Segmentation: one stream per kind of residual.
  7. Zigzag and byte width: from residuals to bytes.
  8. Byte planes: group the bytes of fixed-width values.
  9. Backend: the compressor and how sizes are measured.

The stage numbers S1 to S8 are the order in which the stages were built and measured. S5, dividing a stream by the greatest common divisor of its values, fired on 0,02% of the values and was removed; the later numbers were kept. The byte-level format is in bitstream.md, the code in research-rust/mlt-pipeline/src/.