diff --git a/meshterm/core/device_config.py b/meshterm/core/device_config.py index 511e1b4..aa3ae6c 100644 --- a/meshterm/core/device_config.py +++ b/meshterm/core/device_config.py @@ -183,7 +183,12 @@ def format_value(spec: SettingSpec, value: Any) -> str: # --- snapshot ---------------------------------------------------------------- -async def build_snapshot(device: Device) -> dict: +async def build_snapshot( + device: Device, + *, + self_info: Optional[dict] = None, + path_hash_mode: Optional[int] = None, +) -> dict: """Read a device's full current configuration into one dict. Merges ``SELF_INFO`` with the separately-read tuning parameters and path-hash mode. @@ -192,20 +197,28 @@ async def build_snapshot(device: Device) -> dict: Args: device: A connected device. + self_info: An already-read ``SELF_INFO`` dict to reuse instead of asking the + radio again — a menu caller passes the devstate session cache here, saving + a round-trip on every screen open. Omit to read live (a refresh after a + restore or reset must not trust any cache). + path_hash_mode: An already-read path-hash mode to reuse, on the same terms. Returns: A dict keyed like ``SELF_INFO`` plus ``rx_delay``, ``airtime_factor``, ``path_hash_mode``, ``autoadd_config`` and ``flood_scope``. """ - snapshot = dict(await device.get_self_info()) + snapshot = dict(self_info if self_info is not None else await device.get_self_info()) try: snapshot.update(await device.get_tuning()) except Exception: # noqa: BLE001 - optional read; absence is acceptable pass - try: - snapshot["path_hash_mode"] = await device.get_path_hash_mode() - except Exception: # noqa: BLE001 - optional read; absence is acceptable - pass + if path_hash_mode is not None: + snapshot["path_hash_mode"] = path_hash_mode + else: + try: + snapshot["path_hash_mode"] = await device.get_path_hash_mode() + except Exception: # noqa: BLE001 - optional read; absence is acceptable + pass try: snapshot["autoadd_config"] = await device.get_autoadd_config() except Exception: # noqa: BLE001 - optional read; absence is acceptable diff --git a/meshterm/core/watch_store.py b/meshterm/core/watch_store.py index 2348cef..45a464e 100644 --- a/meshterm/core/watch_store.py +++ b/meshterm/core/watch_store.py @@ -248,18 +248,29 @@ class WatchStore: self.flush(only_if_due=True) def mark_silent(self, key: str, when: Optional[datetime] = None) -> None: - """Latch a node's active silence alarm so it fires once per quiet spell.""" + """Latch a node's active silence alarm so it fires once per quiet spell. + + Packet/sweep-driven (like :meth:`note_heard`), so the write is throttled — the + full-state save is synchronous on the event loop, and the sentinel flushes any + pending change on stop. + """ entry = self.state.watched.get(key) if entry is not None: entry.silent_since = when or utcnow() - self._save() + self._dirty = True + self.flush(only_if_due=True) def clear_silent(self, key: str) -> None: - """Re-arm a node's silence alarm after it has been heard again.""" + """Re-arm a node's silence alarm after it has been heard again. + + Packet-driven, so throttled like :meth:`mark_silent` — a recovery burst after an + outage must not land one full-state disk write per packet. + """ entry = self.state.watched.get(key) if entry is not None and entry.silent_since is not None: entry.silent_since = None - self._save() + self._dirty = True + self.flush(only_if_due=True) # --- the new-node baseline --------------------------------------------------------- @@ -289,7 +300,13 @@ class WatchStore: def add_alert( self, kind: str, label: str, message: str, *, when: Optional[datetime] = None ) -> Alert: - """Append an alert to the log (capped) and persist immediately. + """Append an alert to the log (capped) and persist, throttled. + + Alerts fire from the packet path (a new mesh region can announce a never-seen + node per packet for a while), and the save serializes the whole state + synchronously on the event loop — so this batches through the same throttled + flush as :meth:`note_heard`. The in-memory log (and the header badge it feeds) + updates immediately either way; the sentinel flushes any pending write on stop. Args: kind: The rule that tripped (``silence``/``recovered``/``snr``/``new-node``). @@ -309,7 +326,8 @@ class WatchStore: state.alerts.append(alert) if len(state.alerts) > ALERT_CAP: del state.alerts[: len(state.alerts) - ALERT_CAP] - self._save() + self._dirty = True + self.flush(only_if_due=True) return alert def alerts(self) -> list[Alert]: diff --git a/meshterm/persistence/db.py b/meshterm/persistence/db.py index cf7a506..fd7bc44 100644 --- a/meshterm/persistence/db.py +++ b/meshterm/persistence/db.py @@ -164,6 +164,11 @@ CREATE INDEX IF NOT EXISTS idx_neighbour_reports_pair ON neighbour_reports(repea CREATE INDEX IF NOT EXISTS idx_discovered_paths_cat ON discovered_paths(category, width_bytes); CREATE INDEX IF NOT EXISTS idx_observations_run ON observations(run_id); CREATE INDEX IF NOT EXISTS idx_observations_node ON observations(node); +-- The reception history is queried by time window from every direction — the monitor's +-- session seed, the dashboard's recent slice, the Time Machine's hour range, every +-- ``since=`` filter — and observed_at stores UTC ISO-8601, which orders lexically, so a +-- plain index turns each of those from a full-table scan into a range walk. +CREATE INDEX IF NOT EXISTS idx_observations_observed_at ON observations(observed_at); CREATE INDEX IF NOT EXISTS idx_messages_peer ON messages(peer); """ diff --git a/meshterm/persistence/repository.py b/meshterm/persistence/repository.py index 33cecd0..164da84 100644 --- a/meshterm/persistence/repository.py +++ b/meshterm/persistence/repository.py @@ -8,6 +8,7 @@ from __future__ import annotations import json import sqlite3 +import statistics from dataclasses import dataclass from datetime import datetime, timedelta from pathlib import Path @@ -1397,9 +1398,9 @@ class Repository: ) -> list[tuple[str, Optional[str], datetime]]: """When each node first ever appeared in the history, newest arrivals first. - The Time Machine's "new arrivals" feed: a single ordered scan folds out each - node's earliest observation and its most recent advertised name (``packet`` - rows excluded — no reliable identity). + The Time Machine's "new arrivals" feed: one grouped scan yields each node's + earliest observation, labelled by its most recent advertised name (via + :meth:`node_names`; ``packet`` rows excluded — no reliable identity). Args: since: Only nodes whose *first* appearance is at or after this time. @@ -1407,26 +1408,34 @@ class Repository: Returns: ``(node, latest_name, first_heard)`` triples, most recent arrival first. """ - rows = self._conn.execute( + # One streaming pass, unsorted: ``observed_at`` is UTC ISO-8601 and compares + # correctly as a string, so each node's earliest stamp and latest name are + # tracked by string comparison, and only the ~one winning stamp per node is + # parsed — instead of ordering the whole history and walking it row-object by + # row-object as this used to. + firsts: dict[str, str] = {} + names: dict[str, tuple[str, str]] = {} + for row in self._conn.execute( "SELECT node, name, observed_at FROM observations " - "WHERE node IS NOT NULL AND kind != 'packet' ORDER BY observed_at" - ).fetchall() - firsts: dict[str, datetime] = {} - names: dict[str, str] = {} - for row in rows: - node = row["node"] - if node not in firsts: - try: - firsts[node] = datetime.fromisoformat(row["observed_at"]) - except (TypeError, ValueError): - continue + "WHERE node IS NOT NULL AND kind != 'packet'" + ): + node, iso = row["node"], row["observed_at"] + earliest = firsts.get(node) + if earliest is None or iso < earliest: + firsts[node] = iso if row["name"]: - names[node] = row["name"] - arrivals = [ - (node, names.get(node), first) - for node, first in firsts.items() - if since is None or first >= since - ] + named = names.get(node) + if named is None or iso >= named[0]: + names[node] = (iso, row["name"]) + arrivals: list[tuple[str, Optional[str], datetime]] = [] + for node, iso in firsts.items(): + try: + first = datetime.fromisoformat(iso) + except (TypeError, ValueError): + continue + if since is None or first >= since: + named = names.get(node) + arrivals.append((node, named[1] if named else None, first)) arrivals.sort(key=lambda t: t[2], reverse=True) return arrivals @@ -1491,10 +1500,15 @@ class Repository: Returns: Latest non-empty name keyed by stored node id (the 12-hex key prefix). """ + # SQLite's bare-column-with-MAX guarantee: grouped with ``MAX(observed_at)``, the + # ungrouped ``name`` is taken from the row that supplied the maximum — the latest + # name per node in one aggregate scan, no Python walk over the whole history. + # ``NOT INDEXED``, because the planner otherwise walks ``idx_observations_node`` + # row by row (a random-access fetch per entry — measurably slower than the scan). rows = self._conn.execute( - "SELECT node, name FROM observations " + "SELECT node, name, MAX(observed_at) FROM observations NOT INDEXED " "WHERE node IS NOT NULL AND name IS NOT NULL AND name != '' " - "AND kind != 'packet' ORDER BY observed_at" + "AND kind != 'packet' GROUP BY node" ).fetchall() return {row["node"]: row["name"] for row in rows} @@ -1522,24 +1536,55 @@ class Repository: if since is not None: sql += " AND observed_at >= ?" params.append(since.isoformat()) - rows = self._conn.execute(sql, params).fetchall() - grouped: dict[Optional[str], list[Observation]] = {} - for row in rows: - obs = Observation( - node=row["node"], - public_key=row["public_key"], - name=row["name"], - node_type=row["node_type"], - snr=row["snr"], - rssi=row["rssi"], - lat=row["lat"], - lon=row["lon"], - observed_at=datetime.fromisoformat(row["observed_at"]), + # One streaming pass, aggregating in place. This is a whole-history scan on the + # open path of half the screens (Contacts, the map, a trace's target list), so it + # never materializes per-row Observation objects or parses per-row timestamps — + # ``observed_at`` is UTC ISO-8601, which compares correctly as a *string*, so each + # "most recent X" is tracked by string comparison and only the one winning stamp + # per node is parsed at the end. ``>=`` on every comparison keeps the old + # sort-then-walk-backwards tie behaviour: among equal stamps, the later row wins. + stats: dict[Optional[str], list] = {} + for row in self._conn.execute(sql, params): + iso = row["observed_at"] + snr = row["snr"] + s = stats.get(row["node"]) + if s is None: + # [count, snrs, last_iso, last_rssi, name, name_iso, type, type_iso, + # key, key_iso, lat, lon, loc_iso] + stats[row["node"]] = s = [ + 0, [], "", None, None, "", None, "", None, "", None, None, "" + ] + s[0] += 1 + if snr is not None: + s[1].append(snr) + if iso >= s[2]: + s[2], s[3] = iso, row["rssi"] + if row["name"] and iso >= s[5]: + s[4], s[5] = row["name"], iso + if row["node_type"] is not None and iso >= s[7]: + s[6], s[7] = row["node_type"], iso + if row["public_key"] and iso >= s[9]: + s[8], s[9] = row["public_key"], iso + if row["lat"] is not None and row["lon"] is not None and iso >= s[12]: + s[10], s[11], s[12] = row["lat"], row["lon"], iso + + nodes = [ + HeardNode( + node=node, + name=s[4], + count=s[0], + median_snr=statistics.median(s[1]) if s[1] else None, + best_snr=max(s[1]) if s[1] else None, + last_rssi=s[3], + last_seen=datetime.fromisoformat(s[2]), + lat=s[10], + lon=s[11], + node_type=s[6], + public_key=s[8], ) - grouped.setdefault(row["node"], []).append(obs) - - nodes = [HeardNode.from_observations(node, obs) for node, obs in grouped.items()] + for node, s in stats.items() + ] for heard in nodes: # backfill the type registry from history (PicoCalc colour) register_node_type(heard.public_key or heard.node, heard.node_type) return sorted(nodes, key=lambda n: n.last_seen, reverse=True) diff --git a/meshterm/services/monitor_service.py b/meshterm/services/monitor_service.py index c2f395d..35da80e 100644 --- a/meshterm/services/monitor_service.py +++ b/meshterm/services/monitor_service.py @@ -69,6 +69,13 @@ class MonitorService: # indicator answers "is the mesh alive?", not "any mail?". Pruned as it rolls, # so it never holds more than the six-hour window plus one closing bucket. self._activity: dict[int, int] = {} + # The last histogram/flags tuples, keyed by what they derive from — the current + # minute bucket and (for the histogram) the packet-arrival stamp below. The header + # rebuilds both on every repaint of every screen; between packets and minute + # rolls that was 720 dict lookups a frame for identical tuples. + self._activity_stamp = 0 + self._histogram_cache: tuple[int, int, tuple[int, ...]] = (-1, -1, ()) + self._flags_cache: tuple[int, tuple[bool, ...]] = (-1, ()) # Tallies by packet class (advert/telemetry/packet/message/ack): seeded from # stored history below, then fed live by the kind-unfiltered subscription — # the dashboard's traffic panel, persistent across sessions. @@ -139,7 +146,14 @@ class MonitorService: :data:`ACTIVITY_BUCKETS` bucket counts. """ bucket = int(time.time() // ACTIVITY_BUCKET_S) - return tuple(self._activity.get(bucket - i, 0) for i in range(ACTIVITY_BUCKETS)) + stamp = self._activity_stamp + cached_bucket, cached_stamp, histogram = self._histogram_cache + if bucket != cached_bucket or stamp != cached_stamp: + histogram = tuple( + self._activity.get(bucket - i, 0) for i in range(ACTIVITY_BUCKETS) + ) + self._histogram_cache = (bucket, stamp, histogram) + return histogram def activity_session_flags(self) -> tuple[bool, ...]: """Whether each histogram bucket holds this session's own traffic. @@ -153,7 +167,11 @@ class MonitorService: :data:`ACTIVITY_BUCKETS` flags, newest first. """ bucket = int(time.time() // ACTIVITY_BUCKET_S) - return tuple(bucket - i >= self._start_bucket for i in range(ACTIVITY_BUCKETS)) + cached_bucket, flags = self._flags_cache + if bucket != cached_bucket: + flags = tuple(bucket - i >= self._start_bucket for i in range(ACTIVITY_BUCKETS)) + self._flags_cache = (bucket, flags) + return flags def kind_counts(self) -> dict[str, int]: """Tallies by packet class: advert/telemetry/packet buckets, message, ack. @@ -172,6 +190,7 @@ class MonitorService: """Land one packet in the current activity bucket (and prune scrolled-off ones).""" bucket = int(time.time() // ACTIVITY_BUCKET_S) self._activity[bucket] = self._activity.get(bucket, 0) + 1 + self._activity_stamp += 1 # invalidates the memoized histogram tuple obs = event.observation kind = obs.kind if obs is not None else event.kind.value if obs is not None and kind == "packet": diff --git a/meshterm/services/topology.py b/meshterm/services/topology.py index b224cee..b0b0773 100644 --- a/meshterm/services/topology.py +++ b/meshterm/services/topology.py @@ -74,6 +74,9 @@ _CORROBORATION_MARGIN = 3 _HEX_DIGITS = frozenset("0123456789abcdef") +#: Distinguishes "memoized as None" from "not memoized" in the identity caches. +_MISS = object() + def _is_hex(value: str) -> bool: """Whether ``value`` is non-empty, even-length hex (a plausible path hash).""" @@ -292,6 +295,18 @@ class MeshTopology: key = (c.public_key or c.key_prefix or "").lower().removeprefix("0x") if key: self._known.append((key, key[:12], c.name)) + # ``_known`` never changes after construction, and a build feeds the same few + # dozen distinct hashes through canonical() thousands of times (every hop of + # every stored packet path), so both identity lookups memoize per instance. + self._canonical_memo: dict[str, Optional[str]] = {} + self._name_memo: dict[str, Optional[str]] = {} + # Route search results per (target, k), valid for one graph shape: every link + # mutation bumps the version, so a memoized Yen's run can never outlive the + # evidence it ranked. Callers ask for the same target twice per screen open + # (suggested() and scenarios() both run the search), and the search is the + # single priciest part of a topology-backed open. + self._graph_version = 0 + self._routes_memo: dict[tuple[str, int, int], list[tuple[str, ...]]] = {} # --- identity ---------------------------------------------------------------- @@ -313,24 +328,32 @@ class MeshTopology: """ if not hop: return None + cached = self._canonical_memo.get(hop, _MISS) + if cached is not _MISS: + return cached needle = hop.lower().removeprefix("0x") if not _is_hex(needle): - return None - matches = { - canonical - for key, canonical, _name in self._known - if key.startswith(needle) or needle.startswith(key[:12]) - } - if len(matches) == 1: - return next(iter(matches)) - return needle[:12] + result: Optional[str] = None + else: + matches = { + canonical + for key, canonical, _name in self._known + if key.startswith(needle) or needle.startswith(key[:12]) + } + result = next(iter(matches)) if len(matches) == 1 else needle[:12] + self._canonical_memo[hop] = result + return result def display_name(self, node: str) -> Optional[str]: """The contact name for a canonical id, or ``None`` when unknown.""" - for key, canonical, name in self._known: - if canonical == node: - return name - return None + cached = self._name_memo.get(node, _MISS) + if cached is not _MISS: + return cached + result = next( + (name for _key, canonical, name in self._known if canonical == node), None + ) + self._name_memo[node] = result + return result # --- construction ------------------------------------------------------------ @@ -357,6 +380,7 @@ class MeshTopology: source: Evidence class tag (``trace`` / ``route`` / ``packet`` / ``neighbour``). """ + self._graph_version += 1 # link evidence moved; memoized route searches expire for i in range(len(nodes) - 1): a, b = nodes[i], nodes[i + 1] if not a or not b or a == b: @@ -543,6 +567,7 @@ class MeshTopology: def _merge_node(self, src: str, dst: str) -> None: """Relabel every link touching ``src`` onto ``dst``, folding shared links together.""" + self._graph_version += 1 # the graph is being reshaped; memoized searches expire for key in list(self._links): if src not in key: continue @@ -759,8 +784,14 @@ class MeshTopology: Returns: The intermediate-hop tuples of the found routes (endpoints stripped), - cheapest total-cost first. + cheapest total-cost first. Memoized per graph version (see + ``_routes_memo``) — callers treat the list as read-only, which they do + (both consumers only iterate it). """ + memo_key = (target, k, self._graph_version) + memoized = self._routes_memo.get(memo_key) + if memoized is not None: + return memoized neighbors: dict[str, list[tuple[str, float]]] = {} for (a, b), link in self._links.items(): cost = 1.0 / max(link.strength(self._now), 1e-6) + _HOP_PENALTY @@ -806,6 +837,7 @@ class MeshTopology: first = shortest(self.self_id, set(), set()) if first is None or len(first[1]) - 2 > _MAX_SCENARIO_HOPS: + self._routes_memo[memo_key] = [] return [] accepted: list[list[str]] = [first[1]] # Spur candidates, a min-heap by total cost (counter tiebreaker so equal-cost @@ -843,7 +875,9 @@ class MeshTopology: break _, _, best_path = heapq.heappop(candidates) accepted.append(best_path) - return [tuple(path[1:-1]) for path in accepted] + routes = [tuple(path[1:-1]) for path in accepted] + self._routes_memo[memo_key] = routes + return routes def build_topology( diff --git a/meshterm/services/trace_runner.py b/meshterm/services/trace_runner.py index c48a69a..84f450d 100644 --- a/meshterm/services/trace_runner.py +++ b/meshterm/services/trace_runner.py @@ -61,18 +61,29 @@ def make_node_resolver( if name and ident: entries.append((name, "", ident)) + # Memoized per label: the render paths ask for the same few hop hashes on every + # repaint (per hop, per row, per frame), and ``entries`` is immutable for this + # closure's lifetime, so each distinct label is scanned exactly once. + memo: dict[str, Optional[str]] = {} + def resolve(label: Optional[str]) -> Optional[str]: if not label: return label + if label in memo: + return memo[label] needle = label.lower().removeprefix("0x") + result = label for name, pub, prefix in entries: # The hash is a prefix of the node's key; match either against the full # public key or the (possibly shorter) stored prefix, in either direction. if pub and pub.startswith(needle): - return name + result = name + break if prefix and (prefix.startswith(needle) or needle.startswith(prefix)): - return name - return label + result = name + break + memo[label] = result + return result return resolve @@ -100,14 +111,21 @@ def make_node_type_resolver( if ident and c.node_type is not None: entries.append((ident, c.node_type)) + memo: dict[str, Optional[int]] = {} # per label, as in make_node_resolver + def type_of(label: Optional[str]) -> Optional[int]: if not label: return None + if label in memo: + return memo[label] needle = label.lower().removeprefix("0x") + result = None for ident, node_type in entries: if ident.startswith(needle) or needle.startswith(ident): - return node_type - return None + result = node_type + break + memo[label] = result + return result return type_of @@ -134,14 +152,17 @@ def make_key_resolver(contacts: Optional[list[Contact]]) -> NodeResolver: if (pub := (c.public_key or "").lower().removeprefix("0x")) ] + memo: dict[str, str] = {} # per label, as in make_node_resolver + def resolve(label: Optional[str]) -> Optional[str]: if not label: return label + if label in memo: + return memo[label] needle = label.lower().removeprefix("0x") - for pub in keys: - if pub.startswith(needle): - return pub - return label + result = next((pub for pub in keys if pub.startswith(needle)), label) + memo[label] = result + return result return resolve diff --git a/meshterm/tools/info.py b/meshterm/tools/info.py index 628dc91..bd342a1 100644 --- a/meshterm/tools/info.py +++ b/meshterm/tools/info.py @@ -41,11 +41,10 @@ class InfoTool(Tool): Returns: A :class:`ToolResult` summarizing the device name. """ - from ..core.device_config import build_snapshot - from ..ui.config_editor import config_table + from ..ui.config_editor import cached_snapshot, config_table device = await ctx.device() - snapshot = await build_snapshot(device) + snapshot = await cached_snapshot(ctx, device) custom = await device.get_custom_vars() # A live status panel (role, firmware, battery, clock, radio/packet statistics) diff --git a/meshterm/tools/tx_optimize.py b/meshterm/tools/tx_optimize.py index 0b65fa5..003e2d8 100644 --- a/meshterm/tools/tx_optimize.py +++ b/meshterm/tools/tx_optimize.py @@ -128,7 +128,7 @@ class TxOptimizeTool(Tool): from ..ui.tx_screen import open_tx_optimize device = await ctx.device() - contacts = await device.get_contacts() + contacts = await ctx.devstate.contacts() # the session cache; no re-read admin_node = next( (c for c in contacts if c.name == str(params["admin"])), None ) diff --git a/meshterm/ui/channels.py b/meshterm/ui/channels.py index 348fb4d..d973a0d 100644 --- a/meshterm/ui/channels.py +++ b/meshterm/ui/channels.py @@ -37,6 +37,7 @@ from __future__ import annotations import asyncio import time from dataclasses import dataclass +from functools import lru_cache from typing import TYPE_CHECKING, Awaitable, Callable, Optional from rich.console import Group @@ -408,9 +409,11 @@ class _LiveStats: the manager sits open updates that channel's counts, age, and sparkline in place — but the repository is re-queried at most once per ``ttl`` seconds rather than once per row per repaint, so a full slot table stays cheap at the session's ~1 Hz repaint. + The TTL sits *above* that repaint period on purpose: at exactly the tick rate every + idle frame would still land one repository query — the throttle would gate nothing. """ - def __init__(self, ctx: "AppContext", *, ttl: float = 1.0) -> None: + def __init__(self, ctx: "AppContext", *, ttl: float = 3.0) -> None: """Bind to a context; the first read populates the cache.""" self._ctx = ctx self._ttl = ttl @@ -465,6 +468,7 @@ _BADGE_WIDTH = 5 _COUNT_WIDTH = 5 #: Width of the right-aligned last-message-age lane (fits ``never``-length ages). _AGE_WIDTH = 5 +@lru_cache(maxsize=32) def _activity_sparkline(histogram: "tuple[int, ...]", peak: float) -> Text: """The channel's braille activity sparkline over the trailing two hours, now at the right. @@ -474,6 +478,10 @@ def _activity_sparkline(histogram: "tuple[int, ...]", peak: float) -> Text: :meth:`_LiveStats.peak`) — so the whole activity column shares one scale and the rows' bars are comparable at a glance. The histogram runs deeper than is drawn; the tail past the drawn buckets shapes ``peak`` but isn't charted. + + Memoized on its (hashable) inputs: the row callables rebuild every repaint, but the + histogram snapshot only moves once per :class:`_LiveStats` TTL, so between refreshes + every slot's sparkline is a cache hit. Callers treat the returned Text as read-only. """ return activity_sparkline(histogram, ACTIVITY_DRAWN_BUCKETS, peak=peak) diff --git a/meshterm/ui/chat.py b/meshterm/ui/chat.py index 5652df1..3842a1e 100644 --- a/meshterm/ui/chat.py +++ b/meshterm/ui/chat.py @@ -1021,9 +1021,10 @@ async def _make_paths_presenter( session = ctx.ui.session contacts = await ctx.devstate.contacts() - resolve = trace_runner.make_node_resolver(contacts, ctx.repo.node_names()) + stored_names = ctx.repo.node_names() + resolve = trace_runner.make_node_resolver(contacts, stored_names) type_of = trace_runner.make_node_type_resolver(contacts) - key_of = trace_runner.make_name_key_resolver(contacts, ctx.repo.node_names()) + key_of = trace_runner.make_name_key_resolver(contacts, stored_names) prefix_bytes = await _routing_prefix_bytes(ctx) self_name: Optional[str] = None try: diff --git a/meshterm/ui/config_editor.py b/meshterm/ui/config_editor.py index ad258a8..0c8620c 100644 --- a/meshterm/ui/config_editor.py +++ b/meshterm/ui/config_editor.py @@ -99,6 +99,28 @@ _REBOOT_DROP_TIMEOUT_S = 10.0 _REBOOT_DROP_POLL_S = 0.25 +async def cached_snapshot(ctx: "AppContext", device: "Device") -> dict: + """A config snapshot for a *screen open*, reusing the devstate session cache. + + The two facts devstate already holds — ``SELF_INFO`` and the path-hash mode — are + the slowest part of :func:`~meshterm.core.device_config.build_snapshot` to re-ask + the radio for, and every config apply invalidates them (see + ``tools/config.py``), so an open can trust the cache. The refresh after a restore, + key change, or factory reset keeps calling ``build_snapshot(device)`` raw: the + device state genuinely changed under us there. + """ + self_info: Optional[dict] = None + path_hash_mode: Optional[int] = None + try: + self_info = await ctx.devstate.self_info() + path_hash_mode = await ctx.devstate.path_hash_mode() + except Exception: # noqa: BLE001 - fall back to the raw reads below + pass + return await build_snapshot( + device, self_info=self_info, path_hash_mode=path_hash_mode + ) + + async def edit_config(ctx: "AppContext") -> Optional[list[tuple]]: """Run the interactive editor and return the staged operations to perform. @@ -112,7 +134,7 @@ async def edit_config(ctx: "AppContext") -> Optional[list[tuple]]: from .tui import CANCEL, SelectScreen device = await ctx.device() - snapshot = await build_snapshot(device) + snapshot = await cached_snapshot(ctx, device) custom = await device.get_custom_vars() # The background-advert cadences are app-side settings (MeshTerm sends the adverts, # not the firmware), but they're staged and applied exactly like device values so @@ -652,7 +674,7 @@ async def device_actions(ctx: "AppContext") -> None: from .tui import CANCEL, SelectScreen device = await ctx.device() - snapshot = await build_snapshot(device) + snapshot = await cached_snapshot(ctx, device) # Same persistent-backdrop pattern as the editor: the menu stays pushed while each # action's prompts float over it as modal popups (see edit_config). @@ -749,7 +771,7 @@ async def send_advert(ctx: "AppContext") -> None: ctx: Shared application context (provides the connected device and UI surface). """ device = await ctx.device() - snapshot = dict(await device.get_self_info()) + snapshot = dict(await ctx.devstate.self_info()) # the session cache; no re-read # No emoji in this floating dialog's title: a terminal that paints an emoji a cell # narrower than Rich measures it leaves the content-sized popup's title border short, # bleeding the backdrop through the frame. The menu row keeps its 📡 icon (drawn in the diff --git a/meshterm/ui/dashboard_screen.py b/meshterm/ui/dashboard_screen.py index e1cb1de..b9ae093 100644 --- a/meshterm/ui/dashboard_screen.py +++ b/meshterm/ui/dashboard_screen.py @@ -171,6 +171,12 @@ class DashboardScreen(Screen): #: dynamic rows): ``stats`` from get_stats, ``battery`` from get_battery. self.stats: dict = {} self.battery: dict = {} + # The one-pass window aggregates (see _digest_window), refreshed per repaint. + self._win_nodes: set[str] = set() + self._win_repeaters: set[str] = set() + self._win_counts: Counter = Counter() + self._win_snrs: list[float] = [] + self._win_rssis: list[float] = [] # --- live window ----------------------------------------------------------------- @@ -212,6 +218,7 @@ class DashboardScreen(Screen): def render_body(self, width: int) -> list[str]: """Render the overview sections as one scrolling body.""" self._prune() + self._digest_window() body: list[RenderableType] = [ *self._activity_section(width), Text(), @@ -223,6 +230,37 @@ class DashboardScreen(Screen): self._scroll_total = max(1, len(lines)) return lines + def _digest_window(self) -> None: + """Fold the observation window into the per-frame aggregates, in one pass. + + The pulse line, the busiest-transmitter row, and the RF section each need a + different slice of the same window; scanning the (up to 4000-entry) deque once + per consumer added up to five full passes on every repaint of a busy mesh. + One pass here fills them all; the section builders read the results. + """ + nodes: set[str] = set() + repeaters: set[str] = set() + counts: Counter = Counter() + snrs: list[float] = [] + rssis: list[float] = [] + for o in self._window: + if o.node and o.node_type == NODE_TYPE_REPEATER: + repeaters.add(o.node) + if o.kind == "packet": + continue + if o.node: + nodes.add(o.node) + counts[o.node] += 1 + if o.snr is not None: + snrs.append(o.snr) + if o.rssi is not None: + rssis.append(o.rssi) + self._win_nodes = nodes + self._win_repeaters = repeaters + self._win_counts = counts + self._win_snrs = snrs + self._win_rssis = rssis + # -- activity -- def _activity_section(self, width: int) -> list[RenderableType]: @@ -296,12 +334,8 @@ class DashboardScreen(Screen): recent = sum(shown[:15]) / max(1, min(15, len(shown))) overall = sum(shown) / max(1, len(shown)) span = _span_label(len(shown)) - nodes = {o.node for o in self._window if o.node and o.kind != "packet"} - repeaters = { - o.node - for o in self._window - if o.node and o.node_type == NODE_TYPE_REPEATER - } + nodes = self._win_nodes # the one-pass window digest (see _digest_window) + repeaters = self._win_repeaters def compose(heard: bool) -> Text: line = Text() @@ -335,9 +369,7 @@ class DashboardScreen(Screen): def _busiest(self) -> Optional[tuple[str, int]]: """The window's most-heard attributable node, or ``None`` in silence.""" - counts = Counter( - o.node for o in self._window if o.node and o.kind != "packet" - ) + counts = self._win_counts # the one-pass window digest (see _digest_window) if not counts: return None node, count = counts.most_common(1)[0] @@ -387,8 +419,8 @@ class DashboardScreen(Screen): heading.append(" · reception over 2 h · radio live", style="muted") rows: list[tuple[str, Text]] = [] - snrs = [o.snr for o in self._window if o.snr is not None and o.kind != "packet"] - rssis = [o.rssi for o in self._window if o.rssi is not None and o.kind != "packet"] + snrs = self._win_snrs # the one-pass window digest (see _digest_window) + rssis = self._win_rssis if snrs: med = median(snrs) line = Text(f"{med:+.1f} dB median ", style=snr_style(med)) diff --git a/meshterm/ui/livefeed_screen.py b/meshterm/ui/livefeed_screen.py index 6c57c03..11fe43f 100644 --- a/meshterm/ui/livefeed_screen.py +++ b/meshterm/ui/livefeed_screen.py @@ -232,11 +232,12 @@ class LiveFeedScreen(Screen): when=utcnow(), kind="ack", where=event.ack.code or "delivery confirmed", ) - if entry is not None: - self._feed.appendleft(entry) - # Keep the highlight on the same packet as new rows push it down. - if self._selected is not None: - self._selected = min(self._selected + 1, len(self._feed) - 1) + if entry is None: + return # nothing new on screen — don't buy a full repaint for it + self._feed.appendleft(entry) + # Keep the highlight on the same packet as new rows push it down. + if self._selected is not None: + self._selected = min(self._selected + 1, len(self._feed) - 1) self._session.invalidate() # --- input ----------------------------------------------------------------------- @@ -739,12 +740,17 @@ async def open_livefeed(ctx: "AppContext") -> None: contacts = [] # Contacts first, every name the recorder ever overheard as the fallback — the # app-wide rule that a node we can name never renders as a bare hash. - resolve = trace_runner.make_node_resolver(contacts, ctx.repo.node_names()) + stored_names = ctx.repo.node_names() + resolve = trace_runner.make_node_resolver(contacts, stored_names) type_of = trace_runner.make_node_type_resolver(contacts) - key_of = trace_runner.make_name_key_resolver(contacts, ctx.repo.node_names()) + key_of = trace_runner.make_name_key_resolver(contacts, stored_names) prefix_bytes = await _routing_prefix_bytes(ctx) - seed = ctx.repo.recent_observations(since=utcnow() - OBSERVATION_WINDOW) + # Only the newest screenful is kept (the deque caps at _FEED_CAP), so only that + # many are worth hydrating. + seed = ctx.repo.recent_observations( + since=utcnow() - OBSERVATION_WINDOW, limit=_FEED_CAP + ) screen = LiveFeedScreen( session=session, resolve=resolve, diff --git a/meshterm/ui/node_detail_screen.py b/meshterm/ui/node_detail_screen.py index 0317c82..cf210ed 100644 --- a/meshterm/ui/node_detail_screen.py +++ b/meshterm/ui/node_detail_screen.py @@ -359,6 +359,8 @@ class NodeDetailScreen(Screen): #: it moves onto a route row and holds while it rests on an action row, so the graph #: keeps showing the last pick. self._route_sel = 0 + #: The composed route fan for one (width, rows, selection) — see _routes_stage. + self._stage_memo: Optional[tuple[tuple, list[str]]] = None self._cursor: Optional[int] = None #: The route list's window state: the first visible row, the rows the last fit #: carried (the PgUp/PgDn stride), and whether any rows are hidden (gates the @@ -712,6 +714,16 @@ class NodeDetailScreen(Screen): if not rv.routes or rv.glyph_of is None: return render_lines(Text(rv.note or "no route observed yet", style="muted"), width) sel = self._route_sel if 0 <= self._route_sel < len(rv.routes) else 0 + # The routes view is a snapshot (the opener built it once), so the whole fan — + # a multi-pass graph layout — is a pure function of the width, the row budget, + # and which route is emphasized. One slot suffices: the key only moves on a + # selection change or a resize, and then the previous layout is dead anyway. + caption_lines = 1 + (1 if rv.legend else 0) + list_need = min(sum(len(block) for block in route_blocks), _LIST_MIN_LINES) + max_rows = max(_GRAPH_MIN_ROWS, min(_PATH_MAX_ROWS, budget - caption_lines - list_need)) + stage_key = (width, max_rows, sel) + if self._stage_memo is not None and self._stage_memo[0] == stage_key: + return self._stage_memo[1] # Priority is fixed by evidence order (route 0, the best-evidence route, is always the # spine) so the fan's geometry never moves as the selection changes — only emphasis # (which route is drawn white and on top) and the label muting below follow the pick. @@ -738,9 +750,6 @@ class NodeDetailScreen(Screen): def label_rgb_of(node: str): return base_rgb(node) if node in on_route else _GREY - caption_lines = 1 + (1 if rv.legend else 0) - list_need = min(sum(len(block) for block in route_blocks), _LIST_MIN_LINES) - max_rows = max(_GRAPH_MIN_ROWS, min(_PATH_MAX_ROWS, budget - caption_lines - list_need)) lines = list( render_path_graph( layers, @@ -755,6 +764,7 @@ class NodeDetailScreen(Screen): lines.extend(render_lines(caption, width, no_wrap=True)) if rv.legend: lines.extend(render_lines(node_type_legend(), width, no_wrap=True)) + self._stage_memo = (stage_key, lines) return lines def _route_row_lines(self, route: _Route, selected: bool, width: int) -> list[str]: diff --git a/meshterm/ui/packet_viewer.py b/meshterm/ui/packet_viewer.py index a0fe166..30c848e 100644 --- a/meshterm/ui/packet_viewer.py +++ b/meshterm/ui/packet_viewer.py @@ -30,6 +30,7 @@ dialog, matching what they do on every other screen; Esc closes it. from __future__ import annotations +from collections import OrderedDict from dataclasses import dataclass from datetime import datetime from typing import Callable, Optional, Sequence @@ -39,9 +40,12 @@ from rich.table import Table from rich.text import Text from ..core.channels import decrypt_channel_text, identify_channel -from ..core.models import NODE_TYPE_LABELS, Observation +from ..core.models import NODE_TYPE_LABELS, Observation, utcnow from ..services.trace_runner import NodeResolver from .map_render import _SELF, _UNKNOWN + +#: Composed packet cards the viewer memoizes (a few pages either side of the view). +_BODY_CACHE_MAX = 8 from .pathgraph import PathLayer, render_path_graph, revisited_hops from .pathline import PathLine, path_line from .theme import glyph, name_style, snr_style @@ -354,6 +358,8 @@ class PacketViewer(Screen): super().__init__() self._entries = list(entries) self._index = max(0, min(index, len(self._entries) - 1)) + # Composed cards per (entry, width, age-minute) — see render_body. + self._body_cache: "OrderedDict[tuple, list[str]]" = OrderedDict() self._resolve = resolve self._prefix_bytes = prefix_bytes self._self_name = self_name @@ -464,6 +470,18 @@ class PacketViewer(Screen): # same packet, so the title's ``n/total`` and the reachable range stay current. self._sync() entry = self._entries[self._index] + # An entry is immutable once captured, so its whole card — rows, decrypted + # payload, route-graph layout — is a pure function of the entry, the width, + # and the minute its "heard" age reads (the one row that rolls with time). + # Memoize on exactly that: the repaint tick re-lays the graph only when a + # minute boundary actually moves the visible text. + age_minute = int(max(0.0, (utcnow() - entry.when).total_seconds()) // 60) + key = (id(entry), width, age_minute) + cached = self._body_cache.get(key) + if cached is not None: + self._body_cache.move_to_end(key) + self._scroll_total = max(1, len(cached)) + return cached head, tail = self._head_rows(entry), self._tail_rows(entry) label_w = self._label_width(head + tail, width) @@ -479,6 +497,9 @@ class PacketViewer(Screen): lines.append("") lines.extend(self._grid_lines(tail, width, label_w)) self._scroll_total = max(1, len(lines)) + self._body_cache[key] = lines + if len(self._body_cache) > _BODY_CACHE_MAX: + self._body_cache.popitem(last=False) return lines @staticmethod diff --git a/meshterm/ui/records_screen.py b/meshterm/ui/records_screen.py index 8dfc81b..699c31c 100644 --- a/meshterm/ui/records_screen.py +++ b/meshterm/ui/records_screen.py @@ -210,6 +210,8 @@ class RecordDialog(Screen): self._actions = ("trace", "delete", "back") self._index = 0 self._cursor: Optional[int] = None + # The composed card above the action rows, per width (see render_body). + self._upper_cache: Optional[tuple[int, list[str]]] = None # The card opens at the top, reading down; the arrows drive (and follow) the action # cursor, while PgUp/PgDn/Home/End scroll the body free of it (see cursor_line). self._follow = False @@ -430,6 +432,43 @@ class RecordDialog(Screen): def render_body(self, width: int) -> list[str]: """Stats lanes, the route graph, the full route, provenance, then the actions.""" + # Everything above the action rows — the stat lanes, the area drawing, the + # route graph and the provenance — is a pure function of the frozen record + # and the width, so it is composed once per width; only the cursor-bearing + # action rows below re-render per repaint. + cached = self._upper_cache + if cached is not None and cached[0] == width: + lines = list(cached[1]) + else: + lines = self._upper_lines(width) + self._upper_cache = (width, list(lines)) + + lines.append("") + self._cursor = None + for i, key in enumerate(self._actions): + if key == "back": + lines.append("") + selected = i == self._index + row = Text("❯ " if selected else " ", style="brand" if selected else "") + if key == "trace": + row.append("👣 ", style="accent") + row.append("Trace this path — reopen in Trace path") + elif key == "delete": + row.append("🗑 ", style="err") + row.append("Delete record…") + else: + row.append("Back") + if selected: + row.style = "brand" + self._cursor = len(lines) + row.no_wrap = True + row.truncate(width, overflow="ellipsis") + lines.append(render_to_ansi(row, width)) + self._scroll_total = max(1, len(lines)) + return lines + + def _upper_lines(self, width: int) -> list[str]: + """Compose the record card above the action rows (memoized per width).""" record = self._record lines: list[str] = [] @@ -472,29 +511,6 @@ class RecordDialog(Screen): when = Text(stamp) when.append(f" · MeshTerm {record.app_version}", style="muted") lines.append(render_to_ansi(self._lane("recorded", when), width, no_wrap=True)) - - lines.append("") - self._cursor = None - for i, key in enumerate(self._actions): - if key == "back": - lines.append("") - selected = i == self._index - row = Text("❯ " if selected else " ", style="brand" if selected else "") - if key == "trace": - row.append("👣 ", style="accent") - row.append("Trace this path — reopen in Trace path") - elif key == "delete": - row.append("🗑 ", style="err") - row.append("Delete record…") - else: - row.append("Back") - if selected: - row.style = "brand" - self._cursor = len(lines) - row.no_wrap = True - row.truncate(width, overflow="ellipsis") - lines.append(render_to_ansi(row, width)) - self._scroll_total = max(1, len(lines)) return lines @@ -561,8 +577,16 @@ async def open_records(ctx: "AppContext") -> dict: ) else None node_entries.append((ident, pos, contact.node_type)) + # Memoized: the boards ask for the same route nodes over and over (every hop of + # every record, records sharing hops), and the entry list is fixed for this open — + # so each distinct id pays the prefix scan once. + geo_memo: dict[str, tuple[Optional[tuple[float, float]], Optional[int]]] = {} + def node_geo(node_id: str) -> tuple[Optional[tuple[float, float]], Optional[int]]: """A route node's best-known position and type across the heard/contact entries.""" + cached = geo_memo.get(node_id) + if cached is not None: + return cached needle = node_id.lower().removeprefix("0x") pos: Optional[tuple[float, float]] = None ntype: Optional[int] = None @@ -575,6 +599,7 @@ async def open_records(ctx: "AppContext") -> dict: ntype = etype if pos is not None and ntype is not None: break + geo_memo[node_id] = (pos, ntype) return pos, ntype def walk_drawing( @@ -743,6 +768,26 @@ async def open_records(ctx: "AppContext") -> dict: ): ctx.repo.delete_discoveries(category.id) + def record_row_title( + rank: int, category: Category, record: DiscoveredPath, + show_width: bool, score_w: int, + ): + """A width-aware row title, memoized per width — the record is frozen, so the + path-widget assembly and middle-elide only ever run once per render width + instead of once per record per repaint.""" + memo: dict[int, Text] = {} + + def title(width: int) -> Text: + cached = memo.get(width) + if cached is None: + cached = memo[width] = browser_row( + rank, category, record, + show_width=show_width, score_w=score_w, width=width, + ) + return cached + + return title + while True: items: list = [] for category in CATEGORIES: @@ -759,12 +804,9 @@ async def open_records(ctx: "AppContext") -> dict: items.append(Choice( # Width-aware (see Choice.title): the row re-fits its walk to each # render width, middle-eliding rather than dying at the right edge. - title=( - lambda width, rank=rank, category=category, record=record, - show_width=show_width, score_w=score_w: browser_row( - rank, category, record, - show_width=show_width, score_w=score_w, width=width, - ) + title=record_row_title( + rank, category, record, + show_width=show_width, score_w=score_w, ), value=("open", category, rank, record), )) diff --git a/meshterm/ui/timemachine_screen.py b/meshterm/ui/timemachine_screen.py index b9ada3c..727099e 100644 --- a/meshterm/ui/timemachine_screen.py +++ b/meshterm/ui/timemachine_screen.py @@ -829,10 +829,14 @@ def _mesh_sections( # every wider window keeps the calendar-day columns. Both feed the same bar and # tick machinery — only the bucket resolution and the axis labels differ. hourly = since is not None and window is not None and window <= timedelta(days=1) + # The all-days series feeds the ledger at the bottom too — fetched once here (it is + # one of the costlier scans), and only on demand in the hourly case. + all_days: Optional[list[tuple[str, int, int]]] = None if hourly: series = _fill_hours(ctx.repo.hourly_series(since), since, now) else: - series = _fill_days(ctx.repo.daily_activity(), since, now) + all_days = ctx.repo.daily_activity() + series = _fill_days(all_days, since, now) if not series: return [ Text(), @@ -895,6 +899,9 @@ def _mesh_sections( ) arrivals = ctx.repo.first_seen(since=since) + # One aggregation serves both the arrivals' key lane and the ledger's node count + # below (every heard node *is* a first-seen node, so the counts agree). + heard = ctx.repo.heard_nodes() out.append(Text()) out.append(_heading("Arrivals", "nodes heard for the first time ever")) if not arrivals: @@ -909,7 +916,7 @@ def _mesh_sections( # and each key expands to its fullest known form the way the picker's lane # does: the key captured with an observation (shows offline), else the # device's contact list, else the stored 12-hex prefix. - stored_keys = {n.node: n.public_key for n in ctx.repo.heard_nodes() if n.node} + stored_keys = {n.node: n.public_key for n in heard if n.node} listed = [ ( stored_keys.get(node) or resolve_key(node) or node, @@ -949,8 +956,9 @@ def _mesh_sections( line.append(f" ({format_ago(secs)})", style=_recency_style(secs)) out.append(line) - all_days = ctx.repo.daily_activity() - total_nodes = len(ctx.repo.first_seen()) + if all_days is None: + all_days = ctx.repo.daily_activity() + total_nodes = sum(1 for n in heard if n.node) busiest = max(all_days, key=lambda d: d[1]) out.append(Text()) out.append(_heading("Ledger", "everything ever recorded")) diff --git a/meshterm/ui/trace_screen.py b/meshterm/ui/trace_screen.py index 5e25810..3237217 100644 --- a/meshterm/ui/trace_screen.py +++ b/meshterm/ui/trace_screen.py @@ -419,6 +419,14 @@ class TraceScreen(Screen): #: Traces aggregated on screen — the current route's run. Adopting a #: different path clears it (old numbers describe the old route). self._traces: list[TraceResult] = [] + #: Bumped on every mutation of ``_traces`` — the key the per-frame memos + #: below expire on (a bare ``len`` would miss a clear-then-refill). + self._traces_rev = 0 + # The aggregate stats and the windowed results block, each valid for one + # traces revision (see render_body / _tail_lines): a repaint that changed + # nothing re-reads them instead of re-aggregating every stored trace. + self._stats_memo: Optional[tuple[int, TraceStats]] = None + self._tail_memo: Optional[tuple[tuple, list[str]]] = None #: Every trace this screen ever ran, across path changes — the session #: count the owner reports, immune to the per-route clears above. self._total_traces = 0 @@ -538,6 +546,7 @@ class TraceScreen(Screen): earlier label on the same board. """ self._traces.append(result) + self._traces_rev += 1 self._total_traces += 1 for label in placed: self._run_placed[label.split(" — ")[0]] = label @@ -692,6 +701,7 @@ class TraceScreen(Screen): # session restarts as clean as a fresh screen. self._path_spec = spec.strip() self._traces.clear() + self._traces_rev += 1 self._status = "" finally: self._dialog_open = False @@ -725,6 +735,7 @@ class TraceScreen(Screen): return self._path_spec = reversed_spec self._traces.clear() + self._traces_rev += 1 self._status = "" self._session.invalidate() @@ -737,7 +748,11 @@ class TraceScreen(Screen): highlighted row's body line is known exactly — that is what :meth:`cursor_line` pins while the user is navigating. """ - stats = TraceStats.from_traces(self._target, self._traces) + if self._stats_memo is not None and self._stats_memo[0] == self._traces_rev: + stats = self._stats_memo[1] + else: + stats = TraceStats.from_traces(self._target, self._traces) + self._stats_memo = (self._traces_rev, stats) current = next((t for t in reversed(self._traces) if t.success), None) # Both path lanes break at hop boundaries under their own value column (the # app-wide labelled-row rule): a long walk never folds back to column zero, @@ -781,7 +796,19 @@ class TraceScreen(Screen): def _tail_lines( self, stats: TraceStats, current: Optional[TraceResult], width: int ) -> list[str]: - """The windowed results block: per-hop medians, then the trace log.""" + """The windowed results block: per-hop medians, then the trace log. + + Memoized per traces revision while idle — the block re-renders every stored + trace, and between completions nothing in it moves. A *running* trace skips + the memo outright: its log row carries the live spinner glyph. + """ + key = (self._traces_rev, self._status, width) + if ( + not self._running + and self._tail_memo is not None + and self._tail_memo[0] == key + ): + return self._tail_memo[1] tail: list[RenderableType] = [] if stats.hop_snrs: hash_bytes = current.path_hash_bytes if current is not None else None @@ -790,9 +817,10 @@ class TraceScreen(Screen): if self._running or self._status or self._traces: tail += [Text(), Text("Traces", style="accent")] tail.append(self._trace_log()) - if not tail: - return [] - return render_lines(Group(*tail), width) + lines = render_lines(Group(*tail), width) if tail else [] + if not self._running: + self._tail_memo = (key, lines) + return lines def cursor_line(self) -> Optional[int]: """The highlighted action row while ↑/↓ are in use; free scrolling otherwise. @@ -1429,8 +1457,12 @@ async def _open_session( # destination — a direct attempt, honest about being one. device_route: Optional[tuple[str, ...]] = None if target_contact is not None and target_contact.route_hops is not None: - topo0 = fresh_topology() - device_route = tuple(topo0.canonical(h) or h for h in target_contact.route_hops) + # Canonicalizing hop hashes needs only the contact index, so an *empty* + # graph does it — not the full evidence build (stored traces, packet paths, + # neighbour tables) fresh_topology() runs, which this branch would then + # discard unread. + ident = MeshTopology(device_hash or "local", contacts) + device_route = tuple(ident.canonical(h) or h for h in target_contact.route_hops) auto_hops: Optional[tuple[str, ...]] = None auto_source = "" if target_hash is not None: diff --git a/meshterm/ui/tui/emoji_width.py b/meshterm/ui/tui/emoji_width.py index 47753b6..af0e41d 100644 --- a/meshterm/ui/tui/emoji_width.py +++ b/meshterm/ui/tui/emoji_width.py @@ -416,9 +416,14 @@ def _install_terminal_widths(narrow: frozenset[str], wide: frozenset[str]) -> No import prompt_toolkit.utils as ptu import rich.cells as cells + from .render import _ANSI_CACHE + cells.cached_cell_len.cache_clear() cells._cell_len = _make_cell_len(narrow, wide) ptu._CHAR_SIZES_CACHE = _make_pt_cache(narrow, wide) + # The measurement just changed under every renderable, so anything rasterized before + # calibration (nothing, in the normal boot order — but never trust that) is stale. + _ANSI_CACHE.clear() __all__ = ["calibrate"] diff --git a/meshterm/ui/tui/frame.py b/meshterm/ui/tui/frame.py index 5d89be1..4490c9a 100644 --- a/meshterm/ui/tui/frame.py +++ b/meshterm/ui/tui/frame.py @@ -7,14 +7,15 @@ header and footer so the whole view fits the terminal exactly (never overflowing from __future__ import annotations -from typing import Sequence +from collections import OrderedDict +from typing import Optional, Sequence from rich.cells import cell_len from rich.console import Group, RenderableType from rich.panel import Panel from rich.text import Text -from ...platforms import get_platform +from ...platforms import Platform, get_platform, on_platform from ..theme import hint_style, title_style from .glow import apply_corner_glow from .render import render_lines @@ -131,39 +132,44 @@ def _breathing_room(body_len: int, budget: int) -> int: return 1 if body_len + 2 <= budget else 0 -def _panel(screen: Screen, inner_w: int, viewport: int, active: bool) -> Panel: - """Render a screen's body into a titled, scroll-aware panel. +def _panel_box( + title: str, visible: list[str], more_above: bool, more_below: bool, border: str +) -> Panel: + """Wrap already-sliced body lines in a titled, scroll-aware panel. Args: - screen: The screen to frame. - inner_w: Inner content width in columns. - viewport: Visible body height in rows. - active: Whether this is the focused (top) screen, brightening its border. + title: The screen's heading (empty for none). + visible: The viewport's ANSI lines, already sliced and padded to height. + more_above: Whether content continues above the slice. + more_below: Whether content continues below the slice. + border: Border style name (``"accent"`` for the focused screen). Returns: - A Rich :class:`Panel` of exactly ``viewport + 2`` rows. + A Rich :class:`Panel` of exactly ``len(visible) + 2`` rows. """ - # Record the viewport *before* the body renders, so a screen that windows a - # list inside itself (see :class:`~meshterm.ui.tui.screen.ListWindow`) can size - # its chrome to the frame it is about to be sliced into. - screen.note_viewport(viewport) - body_lines = screen.render_body(inner_w) - visible, more_above, more_below = _visible_slice(screen, body_lines, viewport) body = Text.from_ansi("\n".join(visible)) - border = "accent" if active else "muted" subtitle = None if more_above or more_below: arrow = ("↑" if more_above else " ") + ("↓" if more_below else " ") subtitle = f"[{hint_style(border)}]{arrow} more[/]" return Panel( body, - title=f"[{title_style(border)}]{screen.title}[/]" if screen.title else None, + title=f"[{title_style(border)}]{title}[/]" if title else None, subtitle=subtitle, border_style=border, padding=(0, 1), ) +#: The last framed base composition: ``(content key, rendered lines)``. Re-parsing the +#: sliced body (``Text.from_ansi``) and re-rendering it through the Panel is the priciest +#: part of a repaint (~8 ms on a full frame), and the idle tick recomposes an unchanged +#: screen every second — only the header above it moves. One slot suffices: there is only +#: ever one base screen per paint, and any content change (a keystroke, a scroll, new +#: rows) simply misses and re-renders. +_BASE_BOX_CACHE: Optional[tuple[tuple, list[str]]] = None + + def _title_bar(screen: Screen, cols: int, more_above: bool, more_below: bool) -> Text: """The borderless frame's one-row title bar: a centered title on a bold rule, clip arrows. @@ -231,6 +237,7 @@ def compose_base( Returns: An ANSI string of exactly ``rows`` lines, each within ``cols`` columns. """ + global _BASE_BOX_CACHE platform = get_platform() # The header is a single status line: crop it to one row so a narrow terminal never # wraps it onto a second line (which would push the panel down and misreport its height). @@ -241,8 +248,26 @@ def compose_base( ) if platform.frame_border: viewport = max(1, rows - header_h - 1 - 2) # minus footer(1) and panel border(2) - panel: RenderableType = _panel(base, cols - 4, viewport, active=True) - body = render_lines(Group(panel, footer), cols) + # Record the viewport *before* the body renders, so a screen that windows a + # list inside itself (see :class:`~meshterm.ui.tui.screen.ListWindow`) can size + # its chrome to the frame it is about to be sliced into. + base.note_viewport(viewport) + body_lines = base.render_body(cols - 4) + visible, more_above, more_below = _visible_slice(base, body_lines, viewport) + # The panel wrap is a pure function of what's between its borders: memoize it so + # the repaints that change nothing below the header (the 1 Hz tick) skip the + # ANSI re-parse and Panel re-render. A dynamic footer (the F-key lane, which can + # flip with the physical Shift key alone) has no place in the key, so it renders + # uncached — that combination doesn't arise: the lane belongs to the borderless + # platform below. + key = (cols, rows, base.title, footer_hint, more_above, more_below, *visible) + if footer_lane is None and _BASE_BOX_CACHE is not None and _BASE_BOX_CACHE[0] == key: + body = _BASE_BOX_CACHE[1] + else: + panel = _panel_box(base.title, visible, more_above, more_below, "accent") + body = render_lines(Group(panel, footer), cols) + if footer_lane is None: + _BASE_BOX_CACHE = (key, body) else: # Borderless chrome: a one-row title bar instead of the Panel's border and # padding — the body wins the full terminal width and one extra row. @@ -412,6 +437,16 @@ def _dialog_layout(screen: Screen, cols: int, rows: int) -> tuple[int, int, int, return max_w, vpad, viewport, body_lines +#: Memoized dialog compositions, keyed by everything the box is a function of. Dialogs +#: stack (a confirm over a picker over a menu), and each layer recomposes on every +#: repaint of the frame beneath it, so a single slot would thrash — a handful covers the +#: deepest realistic stack, LRU-evicted as dialogs change. +_DIALOG_CACHE: "OrderedDict[tuple, str]" = OrderedDict() + +#: Dialog compositions the memo keeps. +_DIALOG_CACHE_MAX = 12 + + def compose_dialog(screen: Screen, cols: int, rows: int) -> str: """Compose a centered dialog panel for a floating screen, bounded to the terminal. @@ -425,8 +460,16 @@ def compose_dialog(screen: Screen, cols: int, rows: int) -> str: """ max_w, vpad, viewport, body_lines = _dialog_layout(screen, cols, rows) visible, more_above, more_below = _visible_slice(screen, body_lines, viewport) - body = Text.from_ansi("\n".join(visible)) border = getattr(screen, "border_style", "accent") + key = ( + max_w, vpad, screen.title, screen.footer_hint, border, + more_above, more_below, *visible, + ) + cached = _DIALOG_CACHE.get(key) + if cached is not None: + _DIALOG_CACHE.move_to_end(key) + return cached + body = Text.from_ansi("\n".join(visible)) hint = hint_style(border) subtitle = f"[{hint}]{screen.footer_hint}[/]" if more_above or more_below: @@ -440,4 +483,20 @@ def compose_dialog(screen: Screen, cols: int, rows: int) -> str: padding=(vpad, 1), width=max_w, ) - return "\n".join(apply_corner_glow(render_lines(panel, max_w))) + out = "\n".join(apply_corner_glow(render_lines(panel, max_w))) + _DIALOG_CACHE[key] = out + if len(_DIALOG_CACHE) > _DIALOG_CACHE_MAX: + _DIALOG_CACHE.popitem(last=False) + return out + + +@on_platform +def _bind(platform: Platform) -> None: + """Drop the composition memos on a platform switch — their output bakes the theme in. + + Registered at module bottom so the immediate first run (see + :func:`~meshterm.platforms.on_platform`) finds both caches already defined. + """ + global _BASE_BOX_CACHE + _BASE_BOX_CACHE = None + _DIALOG_CACHE.clear() diff --git a/meshterm/ui/tui/glow.py b/meshterm/ui/tui/glow.py index ef550d4..ab408e7 100644 --- a/meshterm/ui/tui/glow.py +++ b/meshterm/ui/tui/glow.py @@ -29,6 +29,7 @@ box-drawing glyphs are recoloured). from __future__ import annotations import re +from collections import OrderedDict from itertools import accumulate from rich.cells import cell_len @@ -291,6 +292,17 @@ class _Glower: self.targets.setdefault(row, []).append((idx, weight)) +#: Memoized glow output per input block. The pass is pure — the highlight is derived from +#: the lines alone — and the compositors call it with byte-identical input on every idle +#: repaint (the 1 Hz header tick recomposes an unchanged screen), so remembering a handful +#: of recent blocks turns the ~3 ms scan-and-splice into a dict hit. A few entries cover +#: the layers alive in one frame (the base panel and each stacked dialog). +_GLOW_CACHE: "OrderedDict[tuple[str, ...], list[str]]" = OrderedDict() + +#: Blocks the memo keeps: the base frame plus the deepest realistic dialog stack. +_GLOW_CACHE_MAX = 12 + + def apply_corner_glow(lines: list[str]) -> list[str]: """Light every frame in ``lines`` from its top-left corner. @@ -303,6 +315,17 @@ def apply_corner_glow(lines: list[str]) -> list[str]: lines: Composed ANSI lines (one per terminal row, no newlines). Returns: - The lines with the corner highlight applied. + The lines with the corner highlight applied. The result is memoized per input + block (see :data:`_GLOW_CACHE`) — callers treat it as read-only, which they all + do (every compositor only joins or concatenates it onward). """ - return _Glower(lines).apply() + key = tuple(lines) + cached = _GLOW_CACHE.get(key) + if cached is not None: + _GLOW_CACHE.move_to_end(key) + return cached + out = _Glower(lines).apply() + _GLOW_CACHE[key] = out + if len(_GLOW_CACHE) > _GLOW_CACHE_MAX: + _GLOW_CACHE.popitem(last=False) + return out diff --git a/meshterm/ui/tui/render.py b/meshterm/ui/tui/render.py index d5b1a20..09bff06 100644 --- a/meshterm/ui/tui/render.py +++ b/meshterm/ui/tui/render.py @@ -11,7 +11,9 @@ automatically when the terminal is resized. from __future__ import annotations +from collections import OrderedDict from io import StringIO +from typing import Optional from rich.cells import cell_len from rich.console import Console, RenderableType @@ -34,12 +36,62 @@ _CONSOLES: dict[int, Console] = {} _COLOR_SYSTEM = "truecolor" +#: Memoized ANSI per (content, width) for the *describable* renderables — a ``str`` or a +#: :class:`Text`, whose rendering is a pure function of their plain text, spans, and flags. +#: The TUI recomposes the whole frame on every keystroke and on the idle tick, and the +#: list-family screens rasterize row by row through :func:`render_to_ansi`; between two +#: paints almost every row is byte-identical, so this cache turns the per-row console +#: render (~0.15 ms each) into a dict hit. Tables, panels and groups have no cheap content +#: key and skip the cache (their callers memoize at their own level where it matters). +_ANSI_CACHE: "OrderedDict[tuple, str]" = OrderedDict() + +#: Entries the ANSI cache holds before evicting least-recently-used ones. Sized for a few +#: screenfuls of distinct rows (a busy list, its filtered variants, a dialog over it) at +#: well under ~2 MB; small enough that even the PicoCalc carries it without noticing. +_ANSI_CACHE_MAX = 4096 + +#: Generation stamp folded into every cache key. A platform switch swaps the theme and +#: colour system baked into the render consoles, so it bumps the generation instead of +#: trusting eviction — a stale entry can then never be *read*, even mid-eviction. +_CACHE_GEN = 0 + + @on_platform def _bind(platform: Platform) -> None: """Re-bind the rasterizer's colour depth and drop stale consoles on a switch.""" - global _COLOR_SYSTEM + global _COLOR_SYSTEM, _CACHE_GEN _COLOR_SYSTEM = "truecolor" if platform.truecolor else "standard" _CONSOLES.clear() + _ANSI_CACHE.clear() + _CACHE_GEN += 1 + + +def _cache_key(renderable: RenderableType, width: int, no_wrap: bool) -> Optional[tuple]: + """A content key for a cacheable renderable, or ``None`` when it has no cheap one. + + A ``str`` *is* its own content (markup included). A :class:`Text` renders as a pure + function of its plain text, span list, base style, and wrap flags — everything the + key captures (span styles via ``str``, which :class:`~rich.style.Style` memoizes). + Anything else — a table, a group, a panel — would need a deep walk to describe, so it + reports ``None`` and renders uncached. + """ + if isinstance(renderable, str): + return ("s", renderable, width, no_wrap, _CACHE_GEN) + if isinstance(renderable, Text): + return ( + "t", + renderable.plain, + tuple((s.start, s.end, str(s.style)) for s in renderable.spans), + str(renderable.style), + renderable.no_wrap, + renderable.overflow, + renderable.justify, + renderable.tab_size, + width, + no_wrap, + _CACHE_GEN, + ) + return None def _console(width: int) -> Console: @@ -85,6 +137,12 @@ def render_to_ansi(renderable: RenderableType, width: int, *, no_wrap: bool = Fa The rendered output as an ANSI-escaped string, without a trailing newline. """ width = max(1, width) + key = _cache_key(renderable, width, no_wrap) + if key is not None: + cached = _ANSI_CACHE.get(key) + if cached is not None: + _ANSI_CACHE.move_to_end(key) + return cached console = _console(width) with console.capture() as capture: if no_wrap: @@ -95,7 +153,12 @@ def render_to_ansi(renderable: RenderableType, width: int, *, no_wrap: bool = Fa # one call is what makes the PicoCalc glyph contract hold app-wide (identity on the # regular platform). Widths were measured on the pre-fold text; the fold preserves # cell counts (wide emoji become glyph + pad), so the layout above survives it. - return fold_text(capture.get()) + out = fold_text(capture.get()) + if key is not None: + _ANSI_CACHE[key] = out + if len(_ANSI_CACHE) > _ANSI_CACHE_MAX: + _ANSI_CACHE.popitem(last=False) + return out def render_lines(renderable: RenderableType, width: int, *, no_wrap: bool = False) -> list[str]: diff --git a/meshterm/ui/walk_screen.py b/meshterm/ui/walk_screen.py index ea27cdd..1f7bcd0 100644 --- a/meshterm/ui/walk_screen.py +++ b/meshterm/ui/walk_screen.py @@ -245,6 +245,15 @@ class WalkScreen(Screen): #: The link list's window (the list scrolls, the screen doesn't); its #: settled capacity is the stride a PgUp/PgDn moves the highlight by. self._list = ListWindow() + # Graph-derived memos. The topology is snapshotted once when the screen opens + # (fresh evidence means reopening), so everything derived purely from it — + # neighbour lists, the node set, BFS depths, onward-link counts — is computed + # once per key instead of once per repaint. Nothing ever invalidates these: + # they live exactly as long as the frozen graph they describe. + self._links_of_memo: dict[str, list[tuple[str, Link]]] = {} + self._all_nodes_memo: Optional[set[str]] = None + self._hops_out_memo: Optional[dict[str, int]] = None + self._onward_memo: dict[str, dict[str, int]] = {} # --- state ------------------------------------------------------------------- @@ -259,7 +268,14 @@ class WalkScreen(Screen): return self._trail[-2] if len(self._trail) > 1 else None def _links_of(self, node: str) -> list[tuple[str, Link]]: - """``(other, link)`` for every link off ``node``, strongest evidence first.""" + """``(other, link)`` for every link off ``node``, strongest evidence first. + + Memoized per node over the frozen graph (the sort order is pinned at first + ask — strength decays over hours, far slower than a screen stays open). + """ + memoized = self._links_of_memo.get(node) + if memoized is not None: + return memoized now = utcnow() pairs = [ (link.b if link.a == node else link.a, link) @@ -267,15 +283,18 @@ class WalkScreen(Screen): if node in (link.a, link.b) ] pairs.sort(key=lambda pair: -pair[1].strength(now)) + self._links_of_memo[node] = pairs return pairs def _all_nodes(self) -> set[str]: """Every node the graph mentions, plus us (walkable even when alone).""" - nodes = {self._topo.self_id} - for link in self._topo.links(): - nodes.add(link.a) - nodes.add(link.b) - return nodes + if self._all_nodes_memo is None: + nodes = {self._topo.self_id} + for link in self._topo.links(): + nodes.add(link.a) + nodes.add(link.b) + self._all_nodes_memo = nodes + return self._all_nodes_memo def _hops_out(self) -> dict[str, int]: """BFS hop distance from our own node over the evidence links. @@ -283,6 +302,8 @@ class WalkScreen(Screen): Nodes with no path to us are absent — they are the islands, flagged as such wherever a distance would otherwise show. """ + if self._hops_out_memo is not None: + return self._hops_out_memo adjacency: dict[str, set[str]] = {} for link in self._topo.links(): adjacency.setdefault(link.a, set()).add(link.b) @@ -295,6 +316,7 @@ class WalkScreen(Screen): if neighbour not in depths: depths[neighbour] = depths[node] + 1 queue.append(neighbour) + self._hops_out_memo = depths return depths def _matches(self) -> list[str]: @@ -918,14 +940,24 @@ class WalkScreen(Screen): return name_w, key_w def _onward_counts(self, pairs: list[tuple[str, Link]]) -> dict[str, int]: - """How many links continue from each neighbour, the one back here excluded.""" + """How many links continue from each neighbour, the one back here excluded. + + Memoized per focus (the counts depend only on the frozen graph and whose + neighbours are being listed) — this was quadratic per repaint: every + neighbour re-walked a fresh copy of the whole link table. + """ + focus = self._focus + memoized = self._onward_memo.get(focus) + if memoized is not None: + return memoized counts: dict[str, int] = {} for other, _link in pairs: + # A link off the neighbour "continues onward" unless its far end is here: + # one endpoint is the neighbour itself, so only the far endpoint can be us. counts[other] = sum( - 1 - for link in self._topo.links() - if other in (link.a, link.b) and self._focus not in (link.a, link.b) + 1 for far, _l in self._links_of(other) if far != focus ) + self._onward_memo[focus] = counts return counts def _link_row( diff --git a/tests/test_dashboard.py b/tests/test_dashboard.py index e43491d..91d5c9f 100644 --- a/tests/test_dashboard.py +++ b/tests/test_dashboard.py @@ -217,6 +217,7 @@ def test_monitor_tallies_live_packets_by_payload_class(tmp_path: Path) -> None: monitor = MonitorService.__new__(MonitorService) monitor._activity = {} + monitor._activity_stamp = 0 monitor._kind_counts = {} monitor._count_packet( MeshEvent.observation_event(