Tile decode: don't pay for the layers the map never draws
Profiling on the Lyra found vector-tile decode dominating everything else in the app by about two orders of magnitude: a median cached tile took 1236 ms of pure CPU, the largest 3.4 s, against 16 ms of SD read for a whole viewport. Decoded tiles are memoised for the session, so this is a first-pan cost -- but it runs on a GIL-bound worker thread, so it starves the render loop for those seconds rather than running beside it. Roughly 40% of that work was thrown away. A planet tile ships building, housenumber, poi and mountain_peak; _draw_tile looks up ten layer names and none of them is those. So decode_tile now takes the set of layers the caller actually wants, reads each layer's identity with a cheap header walk, and decodes the features only for the ones asked for. Unwanted layers are still returned, named and empty -- which matters, because the tile source decides whether a cache entry is real by whether the bytes parsed into layers, and a tile whose every layer we skip must not read as blank and get its perfectly good cache entry pruned and re-downloaded every session. That test moves off "has features" and onto "parsed into layers", which is sound because the other two ways a tile isn't real -- empty and corrupt -- decode to nothing and raise respectively. Only the decode narrows; whole tiles are still cached, so drawing more later costs a re-decode and never a re-download. Measured on the device across the 46 real cached tiles: 183 s -> 51 s, 3.6x, with the drawn layers byte-identical on every one. The median tile is 1259 -> 353 ms, the largest 3545 -> 759 ms. _Reader.skip stopped slicing out the bytes it steps over while it was here, which is most of what makes the header walk cheap. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
649a55b558
commit
4e7bcacc37
5 files changed
+176
-19
No files matched your search
@@ -120,6 +120,22 @@ _PLACE_STYLE: dict[str, tuple[str, bool, int, int]] = {
|
||||
_WATER_LABEL = ("#7dd3fc", False, 2)
|
||||
_STREET_LABEL = ("#9aa0aa", False, 7)
|
||||
|
||||
#: Polygon layers drawn as fills, in painting order (later wins the shared cell).
|
||||
_FILL_LAYERS = ("water", "landcover", "landuse", "park")
|
||||
|
||||
#: Every basemap layer :func:`_draw_tile` looks at — and so the only geometry the map
|
||||
#: has any use for. A planet tile also ships ``building``, ``housenumber``, ``poi`` and
|
||||
#: ``mountain_peak``, which together are a large share of its features and none of its
|
||||
#: pixels: decoding them cost ~40% of every tile until this set was handed to
|
||||
#: :func:`~meshterm.core.mvt.decode_tile` (measured on the Lyra — a 153 KB tile went
|
||||
#: 1236 ms → 281 ms). Passed to the tile source at construction
|
||||
#: (:attr:`meshterm.context.AppContext.basemap_source`); the on-disk cache still holds
|
||||
#: whole tiles, so widening this set costs a re-decode, never a re-download.
|
||||
DRAWN_LAYERS: frozenset[str] = frozenset(
|
||||
_FILL_LAYERS
|
||||
+ ("waterway", "transportation", "boundary", "transportation_name", "place", "water_name")
|
||||
)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class _Label:
|
||||
@@ -199,7 +215,7 @@ def _draw_tile(frame: _Frame, layers: list[Layer], z: int, x: int, y: int) -> No
|
||||
]
|
||||
|
||||
# Fills first (water, green space) so lines and labels sit on top.
|
||||
for name in ("water", "landcover", "landuse", "park"):
|
||||
for name in _FILL_LAYERS:
|
||||
layer = by_name.get(name)
|
||||
if layer is None:
|
||||
continue
|
||||
|
||||
Reference in new issue
Block a user