garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/tile-pipeline-stage-costs.md

raw


topic: performance triggers: [refine_for_terrain_sampled, nearest_tunnel_within, BesideRoads, "surface cell stages", build_tunnels, unmeasured_profiles, road_levels.rs, tessellate_roads, road_stitch, cut_under_roads, stitch_some, print_tile_pipeline_time, perf_budgets.rs, "tile is slow", "map is slow", "streaming is slow", "vector tile performance", Buckets, SPANS_PER_TRI, MIN_BUCKET_M, covered_weight, RoadMask, dilate_mask] updated: 2026-11-21

What one tile costs, stage by stage — and where the instrument does not reach

docs/notes/tile-raster-cost.md ends by saying rasterisation dominates the tile pipeline by ~20× over parsing. That stopped being true in September 2026 and nobody noticed for a month, because the thing that overtook it was not measured by anything.

The measurements

Release build, crates/geo/tests/fixtures/groningen_z14.mvt, this container, 2026-09-21, via print_tile_pipeline_time:

stage2026-08-242026-09-21, beforeafter
tessellate_roads3.1 ms103.1 ms38.4 ms
cut_under_roadsnot timednot timed*47.5 ms, ×2 per cell
render 512² whole—68.9 ms31.4 ms
render 256² whole—50.2 ms29.0 ms
water_shore_field19.4 ms34.6 ms24.5 ms
tessellate_surfaces0.8 ms14.0 ms8.2 ms
parse + decode all layers14.7 ms15.6 ms15.4 ms
mip chain9.9 ms11.4 ms10.2 ms
build_tunnelsnot timednot timed3.0 ms
ocean_mask 256²0.3 ms0.3 ms0.3 ms

* cut_under_roads had no timing row at all until one was added on 2026-09-21, and by then the bounded cut had already landed; its true pre-September-21 cost is unmeasured and was higher than the 128.5 ms the first reading gave.

Summing the stages a dense cell actually runs — and cut_under_roads runs twice — that is ~478 ms of CPU before, ~198 ms after.

ocean_mask is the control, and it is now the only one. A wall-clock comparison on a shared box is worth nothing without a stage that nothing touched reading the same on both sides, and that row has read 0.30–0.32 ms across a month and across every change here. The 1/64 overzoom sub-tile used to serve the same purpose and no longer can: the raster banding changed it too (8.4 → 7.0 ms). Anyone quoting a timing from this hatch owes a control row beside it, and owes a check that the control is still untouched by whatever they changed.

tessellate_roads overtook the whole 512² rasteriser. That is the inversion the first note describes the other way round, and any reasoning that starts "the cost is scan conversion" is now wrong about a city tile.

Where it went, measured rather than guessed

Timing the phases inside tessellate_roads (perf::passes, recorded in the real function — not a reconstruction, which is how an earlier per-layer attribution came to sum to 282 ms against a measured 121 ms):

phaseshare
road_stitch::stitch (road sheet)91 %
build loop (ribbon + kerb_band)3.7 %
building_passages3.3 %
assign_road_ties1.4 %
street_nodes0.7 %

Three hypotheses died here and are worth recording so they are not re-proposed:

What it is: volume. 21,557 triangles reach the cut, from 3,496 street vertices — roughly six per source vertex, from round_corners fillets multiplied by kerb_band's three ribbons per road.

cut_under_roads is a different shape of expensive, and it is the one left

It is now the single largest item in the pipeline and it runs twice per cell (map_geometry, once for paving and once for the landuse fills). Its subjects are earcut'd ground fills, which arrive as a handful of triangles spanning whole city blocks:

road sheetcut_under_roads
candidates examined per query55412
clip contours per boolean4.217.2

Those 412 are mostly genuine — a block-sized subject really does overlap hundreds of road triangles — so a finer grid does not remove them.

Dicing the subjects was the obvious fix and it is refuted. Timing the phases inside the call gives i_overlay 66 % of it, and across the two call sites the solver costs ~1.43 µs per clip contour with near-zero fixed overhead — 4.2 contours cost 5.3 µs on the road sheet, 17.2 cost 23.9 µs here, and that line predicts both totals within 14 %. Cost therefore tracks the total number of contours inserted, and dicing a subject into sixteen pieces leaves that total alone: each blocker simply meets more, smaller subjects. Reducing it means regrouping blockers or subjects, which does change how a cut sheet is triangulated and so has to be argued against the no-hairline-cracks invariant on a real graphics card.

Two cheaper things were measured and are worth not repeating. Only 19 of 6,157 subject triangles are wholly buried by a single blocker, so hoisting the containment test to avoid wasted contour insertions saves four contours in total. And the bucket grid is not mis-sized here either: 9.6 cells per query at a 24 m edge.

What did work was threading it: the cut is order-independent by construction, so it runs on four threads and the stage went 128.5 → 47.5 ms with byte-identical output. The remaining 47.5 ms is still the largest single item in the pipeline.

And at street level it is the whole load (2026-10-20). A sampling profile of the web-release build filling the street scenario on deck-1 (nine cells with surveyed paving, gap fills and the street objects) puts road_stitch::cut_ribbon_inner at 62 % of all worker time, i_overlay's CrossSolver::cross and tree_split at 46 % between them, and the terrain refinement at 7 %. A surface cell there is ~21 s of worker CPU; the fixture tile's 47.5 ms is not what a street-level cell costs.

What it was: cuts cutting the output of cuts. Recording every stitch_some input of one street-level fill and replaying them (the profile cannot attribute the cut to a caller: it runs on scoped threads, whose stacks start at their own closure) showed the road sheet's own cut costing 1.4 s of 277 and the sheet-against-sheet cuts the rest. Each cut re-triangulates what it leaves into fans of slivers, and the next took those as subjects or blockers: in one gap-fill cut 107,480 of a sheet's 142,499 blocker triangles were under 0.05 m², each solve meeting up to 11,587 of them. A smaller gap-fill quad did not help (8 m quads still met ~2,600). What did: a cut only removes overlaps, so the union of the sheets is unchanged by it, and the fills below the paving are now cut once against the roads and the paving as laid (paving::cut_ground_under_roads_and_paving), and the gap fill is asked of the laid sheets. i_overlay is held to its list solver (road_stitch::new_overlay; Auto's tree, chosen past 4,000 segments, was slower). The recorded set went 56.8 → 18.1 s optimised; street, city and Earth settle roughly twice as fast (render-performance-survey.md). The old two-pass cut had also left ~30 m² of Built under the paving on the fixture.

Still open: cut_into_ground cuts the crops against the already-cut sheets above them, the same cascade on farmland; and the paving itself is dense (300 k triangles a cell as a blocker), so every fill triangle laid under it still meets thousands.

build_tunnels grew tenfold, and it is the road-level solve (2026-10-30)

Same hatch, mac-1 (Apple M5 Pro VM), release: build_tunnels read 30.8 ms on the fixture against the 3.0 ms above — the most expensive row in the hatch, ahead of cut_under_roads (20.7 ms). Since September it plans every structure on the solved road network (tunnels::unmeasured_profiles → road_levels::solve + structures), and that is 26 of the 31 ms:

phase, per callms
solve: the outlier median (Net::within per measured node, ~28 k calls)10.1
solve: Net::build6.3
structures: Net::build again, over the same lines6.3
solve: the four grade envelopes2.8
everything else in both1.4

The median's search now runs in reused dense buffers instead of a HashMap and heap per call: build_tunnels 30.8 → 22.9 ms, ocean_mask 0.13 ms on both sides, and solved heights bit-identical over 31,607 points with noisy measured inputs. Still open: unmeasured_profiles builds the same network twice (solve, then structures); sharing one saves another ~6 ms a cell, and atlas's grading (pipeline/terrain_grade.rs) calls both as well.

Batching the bank field's building fills — tried, measured, reverted

water_shore_field's second pass fills all 8,552 building footprints into the bank mask one fill_path at a time, which is exactly the shape the paint loop was fixed for in August (118,905 allocations → 1,137, at ~4 µs of per-call tiny-skia setup per feature). Batching them into shared paths was built and measured: 34.6 ms → 29.9 ms.

It was reverted, because an equivalence test against the per-feature rasterisation failed and kept failing:

Three texels of 65,536 on the fixture, always in the direction of erasing built ground. Small, but this mask feeds the reflection term, and 4.7 ms on a 30 ms stage does not buy a mask that quietly loses buildings. The test that caught it was written because the existing bank-field test only asserts that some texel reads zero and some reads the far value — both still true of a mask with holes punched in it.

The same construction is live in the paint loop (flush_batch fills buildings Winding), whose comment records a max channel delta of 8/255 against the per-feature raster and attributes it to anti-aliased edges. That attribution is worth re-checking against the coincident-wall case above before it is relied on.

What the parallelism rests on, and what it costs

Three stages are threaded now and the argument is not the same in each.

road_stitch is exact. A ribbon is cut against the uncut geometry of its superiors, never a running union, so no result feeds another — the same fact the module doc gives for why walk order cannot matter. Two splits exist because the callers are shaped differently: the roads are 1,214 ribbons of ~18 triangles (split across ribbons), the fill sheets are four ribbons of thousands (split inside one). stitch_is_the_same_cut_split_across_threads compares both against a forced-serial run byte for byte.

The shore field's second pass is exact for a narrower reason: the y-sweeps carry a running minimum between rows and are left serial, while the parabola envelope reads and writes one row with row-local scratch.

The raster is not exact, and that is the one to know about. Bands change the magnitudes coordinates are computed at, so f32 rounds a few edge samples differently: up to 8/255 on isolated anti-aliased pixels, which is the figure flush_batch already records for the paint batching. Bleed rows remove the part that is clipping (2,443 differing bytes → 312) and no amount removes the rest (256 rows of bleed still leaves 43). The band count is a constant rather than the core count, so at least the basemap does not vary between machines.

Two instrument consequences, both live traps. perf::paths and perf::passes are thread-local, so a banded render records most of its work where the measuring code cannot see it — every budget and hatch that reads them goes through render_mvt_serial instead. And the 1/64 overzoom row stopped being a usable control the moment banding touched it.

Threading this crate breaks the web build, and the native suite cannot see it

Adding std::thread::scope behind #[cfg(not(target_arch = "wasm32"))] broke wasm32 twice in one change, both times invisibly to 1,647 passing tests.

A dead branch still has to resolve. cfg strips items before name resolution and knows nothing about reachability, so calling a cfg(not(wasm32)) function from inside an if let whose condition is bound to None on wasm is still error[E0425]. Put the call behind the cfg, not just the value it branches on — or give the serial path a name both arms can reach.

And this crate builds -D unused, so anything left unreachable when the threading is stripped — a thread-count ceiling, a merge helper, a band count — is a hard error on wasm and nothing at all natively.

Run the lane before believing a threading change. It is nightly with specific flags, and a plain cargo check --target wasm32 is worse than useless here: -p cartopolis_geo alone resolves a different feature set and dies inside uuid, which reads exactly like your bug and is not.

rustup target add wasm32-unknown-unknown
RUSTUP_TOOLCHAIN=nightly \
RUSTFLAGS="--cfg=web_sys_unstable_apis -C target-feature=+atomics,+bulk-memory,+mutable-globals,+simd128" \
cargo check --locked -p cartopolis --no-default-features --features audio \
    --target wasm32-unknown-unknown

The target is not installed in the agent container by default and rustup target add fails as the agent user (the download cache directory is missing); it installs as root through the host, ssh host "docker exec -u root viberfox-agent …", with RUSTUP_HOME=/usr/local/rustup.

Work that is still on the main thread

Tile-stage timings above are worker time. These are not, and no hatch here measures them:

Two vectorisation candidates, both now measured as hot: dilate_mask over 4.2 M bools is a bitset problem (shifts and ORs, 525 KB instead of 4.2 MB), and road_grade::flatten is 24 four-neighbour relaxation passes over ~1.9 M samples. road_stitch's candidate filter is already ~8 ns per test and tiny-skia vectorises internally; neither is worth hand-tuning.

A GPU compute path is not obviously safe here and the reason is in docs/notes/device-loss-strands-the-swapchain.md: every per-frame cap in the streaming path exists because upload and mesh churn has lost the device on an Intel integrated GPU and on an Adreno 840, a lost device is terminal in this client, and a readback would contend with the same worker pool that already starved the screenshot path once. road_stitch being order-independent by construction makes CPU parallelism the cheaper first move.

The instrument is the actual lesson

A hundredfold regression reached main with every test green, because:

cut_under_roads and build_tunnels have rows now. A new stage on the per-cell worker owes itself a row in that hatch in the same commit, or it is invisible in exactly the way these were.

Two main-thread costs that are not in any of this

Tile-stage timings are worker time and show up as streaming latency. These were main-thread and show up as stutter, which no hatch here measures:

A street-level cell is its ground lookups (2026-11-21)

The tile stages above are milliseconds. A whole map_geometry cell at street level is seconds, and almost none of it is in them. Measured on mac-2 (dev build, so read the shares, not the digits), Grote Markt at 40 m, warm cache, RUST_LOG=cartopolis::systems::map::map_geometry=debug for the per-cell surface cell stages line:

Two changes, both exact (the same vertices and triangles, the same heights to the bit):

beforeafter
surfaces busy, 9 cells105.4 s62.5 s
slowest cell's ground26.0 s7.1 s
lookups a cell3.2–5.5 M2.0–3.1 M
settled_s50.343.8

What is left is one lookup per crease-tested edge midpoint, ~2 million a cell, most of them on the paving's 3 m edges and most answered "flat". Making the long-edge rule adaptive the same way (split an edge over 12.5 m only where the ground bends under it) was tried and refuted on a screen: the surfaces stacked in the ground ladder's 10 mm window follow the ground on their own triangles, so a 15 cm tolerance put the verge through the Oude Ebbingestraat's paving. It took surfaces 105 → 70 s and only 15 % of the vertices; it would need a tolerance under the ladder's window to be safe, and at that the AHN noise splits nearly everything.

The vertices are not the refinement's. Of ~1.9 million vertices a cell, the refinement adds ~0.35 million; the rest arrive built. By group over the nine cells: furniture 6.0 M (mostly the parked cars, one per registered bay, up to 400 vertices each), paving 3.3 M, canopies and trunks 3.3 M, built ground 1.5 M, kerbs 1.3 M. That is the lever for the cells' ~90 MB of GPU memory each, not the terrain.

Running any of it

cargo test -p cartopolis_geo --release print_tile_pipeline_time -- --ignored --nocapture
cargo test -p cartopolis_geo print_tile_pipeline_cost -- --ignored --nocapture

--release is not optional for the timings: the crate is at optimisation level 1 in the dev profile and the hatch prints a warning saying so. Read min against median on every row — a wide gap means the box was busy and the run should be retaken. There is no perf and no valgrind in this container and perf_event_paranoid is 4, so sampling profilers are not an option; phase timing via perf::passes and the allocation counters are what there is.