Map: the tile palette resolved once per feature, not once per tile

mark_rgb was reached ~30 000 times a frame and Feature.get ~32 000, both
answering the same handful of questions: a tile's colours are a handful
of constants and a handful of feature classes, shared by tens of
thousands of features. The memo behind mark_rgb makes each call a dict
hit, but at that count a dict hit behind a function call is real time on
a Cortex-A7.

Resolve the fixed colours once per tile and memoize the per-class styles
(roads, waterways) into small local dicts; read tags directly rather than
through the accessor. Only the ground raster's catch-up, not the
keystroke — that moved off the paint path already.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 5 committed 2026-08-09 05:42:33 -04:00
1 parent 42743bded2
commit c9aa9a410d
2 files changed
+90 -20

No files matched your search

+43 -20
View File
@@ -154,6 +154,10 @@ DRAWN_LAYERS: frozenset[str] = frozenset(
"place", "water_name")
)
#: Marks "this feature class has not been styled yet" in :func:`_draw_tile`'s per-tile style
#: memos, where ``None`` is a real answer meaning "a class we deliberately don't draw".
_UNRESOLVED = object()
@dataclass(slots=True)
class _Label:
@@ -394,23 +398,33 @@ def _draw_tile(frame: _Frame, layers: list[Layer], z: int, x: int, y: int) -> No
bx, by, step = t
return [(bx + lx * step, by + ly * step) for lx, ly in ring]
# A tile's palette is a handful of colours shared by tens of thousands of features, so
# every one of them is resolved once here rather than per feature. mark_rgb memoizes,
# but at ~30 000 calls a frame even a dict hit behind a function call is real time on
# the device — and the styles below are constants, so the answer never varies within a
# tile. Per-class styles (roads, waterways, places) memoize into the dicts alongside.
water_rgb, water_prio = mark_rgb(_WATER_FILL[0]), _WATER_FILL[1]
green_rgb, green_prio = mark_rgb(_GREEN_FILL[0]), _GREEN_FILL[1]
boundary_rgb = mark_rgb("#6d5f88")
road_styles: dict[Optional[str], tuple[tuple[int, int, int], int]] = {}
water_styles: dict[Optional[str], Optional[tuple[tuple[int, int, int], int]]] = {}
# Fills first (water, green space) so lines and labels sit on top.
for name in _FILL_LAYERS:
layer = by_name.get(name)
if layer is None:
continue
is_water = name == "water"
for feat in layer.features:
if feat.geom_type != GEOM_POLYGON:
continue
if name == "water":
color, prio = _WATER_FILL
elif str(feat.get("class") or feat.get("subclass")) in _GREEN_CLASSES:
color, prio = _GREEN_FILL
if is_water:
rgb, prio = water_rgb, water_prio
elif str(feat.tags.get("class") or feat.tags.get("subclass")) in _GREEN_CLASSES:
rgb, prio = green_rgb, green_prio
else:
continue
vp.fill_polygon(
[project(r, layer.extent) for r in feat.rings], mark_rgb(color), prio
)
vp.fill_polygon([project(r, layer.extent) for r in feat.rings], rgb, prio)
# Buildings, as a stippled texture under the streets.
building = by_name.get("building")
@@ -453,13 +467,18 @@ def _draw_tile(frame: _Frame, layers: list[Layer], z: int, x: int, y: int) -> No
waterway = by_name.get("waterway")
if waterway is not None:
for feat in waterway.features:
style = _WATERWAY_STYLE.get(str(feat.get("class")))
cls = feat.tags.get("class")
style = water_styles.get(cls, _UNRESOLVED)
if style is _UNRESOLVED:
named = _WATERWAY_STYLE.get(str(cls))
style = None if named is None else (mark_rgb(named[0]), named[1])
water_styles[cls] = style
if style is None or feat.geom_type != GEOM_LINE:
continue
color, prio = style
rgb, prio = style
for ring in feat.rings:
if len(ring) >= 2:
vp.draw_line(project(ring, waterway.extent), mark_rgb(color), prio)
vp.draw_line(project(ring, waterway.extent), rgb, prio)
if feat.name:
_add_line_label(frame, feat.rings, waterway.extent, z, x, y, feat.name, _WATER_LABEL)
@@ -469,12 +488,15 @@ def _draw_tile(frame: _Frame, layers: list[Layer], z: int, x: int, y: int) -> No
for feat in transportation.features:
if feat.geom_type != GEOM_LINE:
continue
cls = str(feat.get("class") or "")
if cls in ("rail", "transit"):
color, prio = _RAIL
else:
color, prio = _ROAD_STYLE.get(cls, _ROAD_DEFAULT)
rgb = mark_rgb(color)
cls = feat.tags.get("class")
style = road_styles.get(cls)
if style is None:
name = str(cls or "")
named = _RAIL if name in ("rail", "transit") else _ROAD_STYLE.get(
name, _ROAD_DEFAULT
)
style = road_styles[cls] = (mark_rgb(named[0]), named[1])
rgb, prio = style
for ring in feat.rings:
if len(ring) >= 2:
vp.draw_line(project(ring, transportation.extent), rgb, prio)
@@ -486,13 +508,13 @@ def _draw_tile(frame: _Frame, layers: list[Layer], z: int, x: int, y: int) -> No
if feat.geom_type != GEOM_LINE:
continue
try:
if int(feat.get("admin_level", 99)) > 6:
if int(feat.tags.get("admin_level", 99)) > 6:
continue
except (TypeError, ValueError):
continue
for ring in feat.rings:
if len(ring) >= 2:
vp.draw_line(project(ring, boundary.extent), mark_rgb("#6d5f88"), 14)
vp.draw_line(project(ring, boundary.extent), boundary_rgb, 14)
# Street-name labels (only kick in at high zoom via the label's min_zoom gate).
tname = by_name.get("transportation_name")
@@ -511,7 +533,7 @@ def _draw_tile(frame: _Frame, layers: list[Layer], z: int, x: int, y: int) -> No
for feat in place.features:
if not feat.name or not feat.rings or not feat.rings[0]:
continue
style = _PLACE_STYLE.get(str(feat.get("class")))
style = _PLACE_STYLE.get(str(feat.tags.get("class")))
if style is None:
continue
color, bold, rank, min_zoom = style
@@ -525,11 +547,12 @@ def _draw_tile(frame: _Frame, layers: list[Layer], z: int, x: int, y: int) -> No
water_name = by_name.get("water_name")
if water_name is not None:
color, bold, rank = _WATER_LABEL
label_rgb = mark_rgb(color)
for feat in water_name.features:
if feat.name and feat.rings and feat.rings[0]:
lx, ly = feat.rings[0][0]
dx, dy = frame.viewport.feature_to_dot(x, y, z, water_name.extent, lx, ly)
frame.labels.append(_Label(rank, dx, dy, feat.name, mark_rgb(color), bold, 8))
frame.labels.append(_Label(rank, dx, dy, feat.name, label_rgb, bold, 8))
def _clip_to_canvas(
+47
View File
@@ -1742,3 +1742,50 @@ async def test_map_pans_on_the_ground_its_last_raster_left(monkeypatch) -> None:
screen.handle("right")
screen.render_body(80)
assert handed and handed[-1] is screen._ghost, "the pan painted on an empty canvas"
def test_each_road_class_keeps_its_own_colour_through_the_per_tile_style_memo() -> None:
"""The tile's palette is resolved once per class, not once per feature.
``mark_rgb`` was reached ~30 000 times a frame and ``Feature.get`` ~32 000 — both
answering the same handful of questions. Hoisting them behind a per-tile memo is only
sound while every class still resolves to exactly the colour it did: a motorway must
not inherit the footway's grey because it drew second.
"""
from meshterm.core.mvt import GEOM_LINE, Feature, Layer
from meshterm.ui.map_render import (
_RAIL,
_ROAD_DEFAULT,
_ROAD_STYLE,
_draw_tile,
_Frame,
)
from meshterm.ui.mapcanvas import MapCanvas
from meshterm.ui.theme import mark_rgb
classes = ["motorway", "footway", "residential", "rail", "no_such_class", None]
features = [
Feature(
geom_type=GEOM_LINE,
# One short horizontal run per class, each on its own row of the tile.
rings=[[(200, 500 + i * 500), (3900, 500 + i * 500)]],
tags={} if cls is None else {"class": cls},
)
for i, cls in enumerate(classes)
]
# Wide enough that the whole tile lands on the canvas, so every class gets drawn.
vp = Viewport(45.5019, -73.5674, 14, 512, 512)
canvas = MapCanvas(vp.dot_w // 2, vp.dot_h // 4)
frame = _Frame(canvas=canvas, viewport=vp)
_draw_tile(
frame, [Layer(name="transportation", extent=4096, features=features)],
14, 4843, 5861,
)
painted = {colour for row in canvas._color for colour in row if colour}
for cls in classes:
if cls == "rail":
expected = _RAIL
else:
expected = _ROAD_STYLE.get(cls or "", _ROAD_DEFAULT)
assert mark_rgb(expected[0]) in painted, f"{cls} lost its own colour"