Map: draw the ground off the keystroke, and stop reprojecting the world

A pan cost 0.9-2.4 s. It was never the tiles - those already load in a
thread - it was render_map, which rasterizes the vector ground on the
paint path: 900-2342 ms for a view holding ~16k features.

Two changes, in that order.

The projection was doing the same trigonometry once per vertex. Every
feature_to_dot call recomputed Viewport.origin_world - a sin/log pair -
and a 2** for values constant across the whole frame, 107k times a frame.
The mapping is affine in tile-local coordinates, so tile_transform folds
it into three numbers and _draw_tile spells the arithmetic out inline: two
multiplies and two adds per vertex, no call at all. That alone is 2.4x
(z12 2342 -> 991 ms).

The rest is genuinely per-feature Python and will not shrink to keystroke
size, so the raster moved off the paint path. A paint now returns the best
picture available immediately and a background task draws the real one:
unchanged view serves the finished frame; a view that only gained a tile
keeps the old raster, which is still correctly aligned; a view that *moved*
draws the nodes alone on the new viewport, which is a few ms, so panning
tracks the keys and the streets catch up. The title says "drawing..." while
it does.

Exactly one raster runs at a time and it is always the newest requested -
a held arrow hands us a new viewport every repaint, and starting a second
of thread-bound work per keypress would queue frames that are already
stale on arrival and slow the keystrokes this exists to protect.

On the device: pan 1205 -> 89 ms, 1156 -> 130 ms, 2449 -> 91 ms; zoom
1161 -> 142 ms; a settled view 37 ms.

Also flips MESHTERM_FASTRENDER on by default (=0 still opts out), and asks
whether a loop is running *before* building a coroutine, so a static render
no longer leaves an un-awaited one behind.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Fable 5 committed 2026-08-09 01:58:08 -04:00
1 parent 57386fe879
commit e53e35523e
6 files changed
+270 -17

No files matched your search

+87
View File
@@ -1506,3 +1506,90 @@ def test_render_map_find_matches_still_label_white() -> None:
markers = [MapMarker("TARGET", 45.5, -73.6, key="d4" * 32)]
lines = render_map(vp, {}, markers, find="targ")
assert _glyph_color(lines, "TARGET") == (255, 255, 255)
# --- the ground raster runs off the paint path ---------------------------------------
def _async_map(monkeypatch, drawn: list):
"""A map screen whose rasters are captured instead of run, with a live event loop."""
from meshterm.ui import map_screen as ms
from meshterm.ui.map_render import MapMarker
monkeypatch.setattr(ms, "_loop_running", lambda: True)
started: list[tuple] = []
monkeypatch.setattr(
ms.asyncio, "ensure_future", lambda coro: (coro.close(), started.append(coro))[0]
)
screen = ms.MapScreen(
_StubSession(80, 24), [MapMarker("A", 45.5, -73.6)], _StubSource(), 14
)
drawn.append(started)
return screen
def test_map_paints_without_waiting_for_the_ground(monkeypatch) -> None: # noqa: ANN001
"""A pan must answer the keystroke now — rasterizing takes ~1 s on the PicoCalc."""
from meshterm.ui import map_screen as ms
calls: list[tuple] = []
real = ms.render_map
monkeypatch.setattr(ms, "render_map", lambda vp, tiles, m, **k: calls.append(tiles) or real(vp, tiles, m, **k))
started: list = []
screen = _async_map(monkeypatch, started)
screen.render_body(80)
# Whatever ran on the paint path drew no tiles: the ground is somebody else's job.
assert calls, "the paint drew nothing at all"
assert all(tiles == {} for tiles in calls), calls
assert screen._drawing is not None, "no background raster was scheduled"
def test_map_keeps_one_raster_in_flight_while_panning(monkeypatch) -> None: # noqa: ANN001
"""A held arrow key must not queue a second of thread work per keypress."""
started: list = []
screen = _async_map(monkeypatch, started)
screen.render_body(80)
scheduled = len(started[0])
for _ in range(8):
screen.handle("right")
screen.render_body(80)
assert len(started[0]) == scheduled, "a second raster started before the first finished"
assert screen._wanted is not None, "the newest view was not remembered for next"
def test_map_holds_an_aligned_frame_while_a_tile_lands(monkeypatch) -> None: # noqa: ANN001
"""A tile arriving must not blank streets that are still in the right place.
The view has not moved, so the finished raster is still correctly aligned — it is only
missing the newcomer's detail. Redrawing from nothing would flash the ground away for
the second it takes to draw the same picture again.
"""
started: list = []
screen = _async_map(monkeypatch, started)
screen.render_body(80)
# Pretend the scheduled raster finished.
screen._frame = ["ground"] * 20
screen._frame_key = screen._ground_key(screen._viewport)
screen._drawing = None
assert screen.render_body(80) == ["ground"] * 20
in_view = screen._viewport.tiles(14)[0]
screen._tiles[in_view] = _loaded_tile() # a tile lands, view unmoved
assert screen.render_body(80) == ["ground"] * 20, "dropped an aligned frame"
assert screen._drawing is not None, "did not redraw for the new tile"
def test_map_drops_a_stale_frame_once_the_view_moves(monkeypatch) -> None: # noqa: ANN001
"""Streets drawn for somewhere else are a lie about where you are looking."""
started: list = []
screen = _async_map(monkeypatch, started)
screen.render_body(80)
screen._frame = ["ground"] * 20
screen._frame_key = screen._ground_key(screen._viewport)
screen._drawing = None
screen.handle("right")
assert screen.render_body(80) != ["ground"] * 20, "kept a misaligned frame"
+4 -4
View File
@@ -283,14 +283,14 @@ def _fast_renderer(frames: list[str]) -> tuple["FastRenderer", _CapturingOutput]
return renderer, out
def test_fastrender_is_off_unless_asked_for(monkeypatch) -> None: # noqa: ANN001
"""The bypass is opt-in: an unset environment leaves prompt_toolkit in charge."""
def test_fastrender_is_on_with_an_escape_hatch(monkeypatch) -> None: # noqa: ANN001
"""The row writer is the default paint; only an explicit 0 hands it back to pt."""
from meshterm.ui.tui import fastrender
monkeypatch.delenv("MESHTERM_FASTRENDER", raising=False)
assert not fastrender.enabled()
monkeypatch.setenv("MESHTERM_FASTRENDER", "1")
assert fastrender.enabled()
monkeypatch.setenv("MESHTERM_FASTRENDER", "0")
assert not fastrender.enabled()
def test_fastrender_rewrites_only_the_rows_that_changed() -> None: