diff --git a/src/toad/application_lifetime.py b/src/toad/application_lifetime.py index 49e735afd..f78779b72 100644 --- a/src/toad/application_lifetime.py +++ b/src/toad/application_lifetime.py @@ -147,8 +147,17 @@ async def start(self) -> None: ApplicationLifetime._startup_hook = ApplicationLifetime._startup_hook.notify(self) await self.restore_workspace() self.started() + self.prepare_view_settings() self.settle_heap() + def prepare_view_settings(self) -> None: + """The class setup every view's configuration needs, done once the first frame and + the restored tabs are up, before the heap settles (so it is frozen with them), and + not on the reader's first tap of a view.""" + from toad.object_configuration import ObjectConfiguration + + ObjectConfiguration.prepare_settings_type(self.app.saved_settings) + @staticmethod def settle_heap() -> None: """What is alive once the workspace is back (modules, classes, declared families, the diff --git a/src/toad/object_configuration.py b/src/toad/object_configuration.py index 8c65e3846..c4a1e8743 100644 --- a/src/toad/object_configuration.py +++ b/src/toad/object_configuration.py @@ -65,8 +65,7 @@ def __init__(self, identity: TabIdentity, global_settings: SavedSettings, scope = str(ScopeKey.of("view", identity.mode_name)) if ObjectStateRegistry.get_by_scope(scope) is not None: raise ValueError(f"Settings already open for {identity}") - lazy_type = LazyDataclassFactory.make_lazy_simple(type(defaults.object_instance)) - super().__init__(lazy_type(), scope_id=scope, parent_state=defaults) + super().__init__(self.lazy_settings_type(global_settings)(), scope_id=scope, parent_state=defaults) if self.path.exists(): document = FieldCodec.decode(ObjectSettingsDocument, FieldCodec.parse(self.path.read_text("utf-8"))) if document.identity != identity: @@ -81,6 +80,20 @@ def __init__(self, identity: TabIdentity, global_settings: SavedSettings, self.on_resolved_changed(self.apply_changes) self.on_state_changed(self.save_changes) + @staticmethod + def lazy_settings_type(global_settings: SavedSettings) -> type: + """The settings tree as a view holds it, every field following the app's settings + until the view sets it: made once per settings class.""" + return LazyDataclassFactory.make_lazy_simple(type(global_settings.state.object_instance)) + + @classmethod + def prepare_settings_type(cls, global_settings: SavedSettings) -> None: + """Set up, once, what every view's configuration is built from: its lazy settings + class, each nested one, and their annotations, which the first instance validates + against. That is class setup (190 ms on a phone's speed), and unprepared it was + paid by the first view the reader opened.""" + cls.lazy_settings_type(global_settings)() + def field(self, path: str) -> ObjectSettingField: return ObjectSettingField(self, self.parameter_structure.path(path)) diff --git a/src/toad/qt/history.py b/src/toad/qt/history.py index b8e4ad159..d28ed9c8d 100644 --- a/src/toad/qt/history.py +++ b/src/toad/qt/history.py @@ -419,11 +419,15 @@ def paint(self, painter, option, index) -> None: # type: ignore[override] @dataclass(frozen=True) class PreparedOnFill(RowFormatting): - """A row's formatting prepared only when a document is filled from it: a measurement that - answers from its cache never fills, so it never pays for the preparation.""" + """A row's formatting prepared only when a measurement needs it: one that answers from its + cache never resolves it, so it never pays for the preparation. What it resolves to may be + the row's pending stand-in, which the measurement estimates instead of laying out.""" prepare: Callable[[], RowFormatting] + def resolved(self) -> RowFormatting: + return self.prepare().resolved() + def fill(self, document: QTextDocument) -> None: self.prepare().fill(document) diff --git a/tools/qt_drive.py b/tools/qt_drive.py index bac065d26..784514eec 100644 --- a/tools/qt_drive.py +++ b/tools/qt_drive.py @@ -185,27 +185,36 @@ async def open_named(self, name: str) -> None: channel it belongs to, and any one opens it.""" await self.listed() if name.startswith("#"): - await self.channel_listed(name) + await self.channel_row(name) await self.expand(name) - await self.open(self.channel_row(name), name) + await self.open(await self.channel_row(name), name) else: - pending, rows = list(self.app.sidebar.items()), [] - while pending: - item = pending.pop() - rows.append(item) - pending.extend(item.child(index) for index in range(item.childCount())) - await self.open(next(row for row in rows if isinstance(row, ConversationRow) - and row.target.name == name), name) + await self.open(await self.found(lambda row: isinstance(row, ConversationRow) + and row.target.name == name, name), name) await asyncio.sleep(2.0) - async def channel_listed(self, name: str) -> None: - """The sidebar lists this channel: the welcome's first rows can come before it.""" - await until(lambda: any(isinstance(row, ConversationRow) and row.target.name == name - for row in self.app.sidebar.items()), name) + async def found(self, wanted, label: str, rows=None, tenths: int = 200): + """The first sidebar row that is `wanted`, waited for: the sidebar is a live list, + filled from observations that can arrive after the step that reads it (the welcome's + first rows come before the channels). It is read again every tenth of a second + through the loop the scenario's steps run on, until the deadline, and then the drive + fails naming the missing row and every row listed. `rows` reads the rows to search, + every row nested under every channel unless it is given.""" + read = rows or (lambda: self.rows(self.app.sidebar.items())) + present: list = [] + for _ in range(tenths): + present = list(read()) + for row in present: + if wanted(row): + return row + await asyncio.sleep(0.1) + listed = ", ".join(f"{row.drawn[1] or row.key!r}" for row in present) or "nothing" + raise LookupError(f"the sidebar lists no {label} after {tenths / 10:.1f} s; it lists: {listed}") - def channel_row(self, name: str) -> ConversationRow: - return next(row for row in self.app.sidebar.items() - if isinstance(row, ConversationRow) and row.target.name == name) + async def channel_row(self, name: str) -> ConversationRow: + """The channel's top-level row, once the sidebar lists it.""" + return await self.found(lambda row: isinstance(row, ConversationRow) and row.target.name == name, + f"channel {name}", rows=self.app.sidebar.items) def rows(self, items): """Every sidebar row, threads nested under their channels included.""" @@ -222,13 +231,16 @@ def thread_rows(self) -> list[ConversationRow]: found.setdefault(row.key, row) return list(found.values()) - def thread_row(self) -> ConversationRow: + async def thread_row(self) -> ConversationRow: """The first thread nested under a channel, as the Textual sidebar nests them.""" - for channel in self.app.sidebar.items(): - if isinstance(channel, ConversationRow) and channel.childCount(): - self.app.sidebar.expandItem(channel) - return channel.child(0) - raise LookupError("no channel lists a thread") + channel = await self.found(lambda row: isinstance(row, ConversationRow) and row.childCount() > 0, + "channel with a thread under it", rows=self.app.sidebar.items) + self.app.sidebar.expandItem(channel) + return channel.child(0) + + async def listed_thread(self, key: str) -> ConversationRow: + """The thread row for `key`, once the sidebar lists it.""" + return await self.found(lambda row: row.key == key, f"thread {key}", rows=self.thread_rows) async def live_native_thread_rows(self) -> list[ConversationRow]: """The listed threads a running native runtime owns, as Core presents each owner @@ -261,9 +273,8 @@ async def run(self) -> None: await self.listed() app = self.app await self.expand("#comms") - tops = list(app.sidebar.items()) - comms = next(row for row in tops if isinstance(row, ConversationRow) and row.target.name == "#comms") - peer = self.thread_row() + comms = await self.channel_row("#comms") + peer = await self.thread_row() app.open_row(comms) await until(lambda: self.page is not app.workspace_sessions.home and self.page.messages.model_rows.rowCount(), "#comms") self.report("1-comms") @@ -292,6 +303,7 @@ class ActivityScenario(Scenario): async def run(self) -> None: await self.listed() app = self.app + await self.found(lambda row: True, "thread row", rows=self.thread_rows) rows = app.coordination_access.observation.observed().sidebar.threads.rows children = self.thread_rows() working = [row for row in children if rows[row.key].status.is_working()] @@ -377,7 +389,7 @@ async def run(self) -> None: # both frontends' prompts read. app.saved_settings.set(SETTINGS.ui.key_mode, KeyMode.member_named(mode)) # The welcome answers before the sidebar has drawn its thread rows. - await until(self.thread_rows, "sidebar thread rows") + await self.found(lambda row: True, "thread row", rows=self.thread_rows) rows = app.coordination_access.observation.observed().sidebar.threads.rows children = self.thread_rows() print("working:", [row.key for row in children if rows[row.key].status.is_working()], flush=True) @@ -445,7 +457,7 @@ async def run(self) -> None: app = self.app app.resize(int(os.environ.get("WIDTH", self.width)), 915) wanted = os.environ["PEER"] - peer = next(row for row in self.thread_rows() if row.key == wanted) + peer = await self.listed_thread(wanted) home = app.workspace_sessions.home app.open_row(peer) await until(lambda: self.page is not home and getattr(self.page, "composer", None) is not None, "composer") @@ -472,7 +484,7 @@ async def run(self) -> None: app = self.app app.resize(int(os.environ.get("WIDTH", self.width)), 915) wanted = os.environ["PEER"] - peer = next(row for row in self.thread_rows() if row.key == wanted) + peer = await self.listed_thread(wanted) home = app.workspace_sessions.home app.open_row(peer) await until(lambda: self.page is not home and getattr(self.page, "composer", None) is not None, "composer") @@ -646,8 +658,7 @@ async def run(self) -> None: await self.listed() app = self.app await self.expand("#comms") - comms = next(row for row in app.sidebar.items() - if isinstance(row, ConversationRow) and row.target.name == "#comms") + comms = await self.channel_row("#comms") app.open_row(comms) await until(lambda: self.page is not app.workspace_sessions.home and self.page.messages.model_rows.rowCount(), "#comms rows") @@ -819,6 +830,7 @@ async def slow_read(request): reader.read = slow_read try: + await self.found(lambda row: True, "thread row", rows=self.thread_rows) threads = self.thread_rows() app.open_row(next((row for row in threads if row.target.name == "scratch-live-pipeline"), threads[0])) await until(lambda: isinstance(self.page, QtOpeningSessionView), "opening view", tenths=50) @@ -832,7 +844,7 @@ async def slow_read(request): await asyncio.sleep(1.0) self.report("2-opened", waiting=app.float_layer.isVisible()) # 2. #comms' first page: its wait floats over the history until the first rows are drawn. - app.open_row(self.channel_row("#comms")) + app.open_row(await self.channel_row("#comms")) seen: list[tuple[bool, str]] = [] for _ in range(60): await asyncio.sleep(0.05) @@ -1019,7 +1031,7 @@ async def run(self) -> None: await self.listed() app = self.app await self.expand("Threads") - await self.open(self.channel_row("#any"), "#any") + await self.open(await self.channel_row("#any"), "#any") composer = self.page.composer self.report("1-any", placeholder=composer.editor.placeholderText(), activity=[line.text() for line in self.page.activity]) @@ -1150,7 +1162,7 @@ async def run(self) -> None: await self.listed() app = self.app await self.expand("Threads") - await self.open(self.channel_row("#any"), "#any") + await self.open(await self.channel_row("#any"), "#any") composer = self.page.composer await self.type("/") await until(lambda: composer.offers.offers, "command offers", tenths=300) @@ -1164,7 +1176,7 @@ async def run(self) -> None: await until(lambda: any("current target" in notice for notice in app.float_layer.shown_notices()), "refused", tenths=100) self.report("3-refused", notices=app.float_layer.shown_notices(), shown=app.float_layer.isVisible()) - await self.open(self.channel_row("#comms"), "#comms") + await self.open(await self.channel_row("#comms"), "#comms") await self.send("/rename-tag") await until(self.forms, "form", tenths=150) form = self.forms()[0] @@ -1239,7 +1251,7 @@ async def run(self) -> None: app = self.app await self.listed() await self.expand("#any") - await self.open(self.thread_rows()[0], "first thread") + await self.open(await self.found(lambda row: True, "thread row", rows=self.thread_rows), "first thread") await self.open_named("#comms") app.show_sidebar() await asyncio.sleep(0.5) @@ -1295,7 +1307,7 @@ async def run(self) -> None: await app.select_session(app.tab_order.home) app.show_sidebar() await self.expand("#any") - row = next(row for row in self.thread_rows() if row.key == restored_thread.key.name) + row = await self.listed_thread(restored_thread.key.name) tree = row.treeWidget() tree.scrollToItem(row) await asyncio.sleep(0.5) @@ -1359,8 +1371,7 @@ async def run(self) -> None: app.resize(self.width, 915) await self.settle() await self.expand("#comms", "Threads") - tops = list(app.sidebar.items()) - comms = next(row for row in tops if isinstance(row, ConversationRow) and row.target.name == "#comms") + comms = await self.channel_row("#comms") home = app.workspace_sessions.home app.open_row(comms) await self.opened(home) @@ -1407,7 +1418,7 @@ async def run(self) -> None: # A DM: its own bar heading and its own, independent view settings. The # sidebar may have re-rendered after the pane toggles, so re-fetch the # peer row from the current tree instead of reusing the stale handle. - peer = self.thread_row() + peer = await self.thread_row() app.open_row(peer) await self.opened(comms_view) dm_view = app.workspace_sessions.selected @@ -1528,8 +1539,7 @@ def timed(slide, amount): print(" opening slide frame gaps ms:", [round(gap * 1000, 1) for gap in gaps], "max", round(max(gaps, default=0) * 1000, 1), flush=True) await self.expand("#comms") - tops = list(app.sidebar.items()) - comms = next(row for row in tops if isinstance(row, ConversationRow) and row.target.name == "#comms") + comms = await self.channel_row("#comms") app.open_row(comms) await until(lambda: app.tabs.count() == 1, "#comms tab") await asyncio.sleep(0.5) @@ -1549,7 +1559,7 @@ def widened(growth, amount): grew.append(app.tabs.tabRect(app.tabs.count() - 1).width()) started.append(time.perf_counter()) TabGrowth.apply_amount = widened - app.open_row(self.thread_row()) + app.open_row(await self.thread_row()) await until(lambda: app.tabs.count() == 2, "second tab") await until(lambda: not app.tabs.growing, "tab grown", tenths=50) TabGrowth.apply_amount = growing @@ -1674,10 +1684,10 @@ async def run(self) -> None: app.resize(self.width, self.height) await asyncio.sleep(0.5) app.show_sidebar() - await self.channel_listed("#comms") + await self.channel_row("#comms") await self.expand("#comms") self.report("1-welcome", **self.geometry()) - comms = self.channel_row("#comms") + comms = await self.channel_row("#comms") home = app.workspace_sessions.home app.open_row(comms) await until(lambda: self.page is not home and self.page.content_ready.is_set(), "#comms") @@ -1698,7 +1708,7 @@ async def run(self) -> None: override.reset() # Step 7 reads the thread's configure form, which Core offers only a live native owner. native = await self.live_native_thread_rows() - peer = native[0] if native else self.thread_row() + peer = native[0] if native else await self.thread_row() before = self.page app.open_row(peer) await until(lambda: self.page is not before, "thread") @@ -1783,8 +1793,8 @@ async def run(self) -> None: await asyncio.sleep(1.0) tree = app.sidebar name = os.environ.get("TOAD_WAVE_THREAD") - row = self.thread_row() if not name else next( - line for line in self.rows(tree.items()) if isinstance(line, ConversationRow) and line.key == name) + row = await self.thread_row() if not name else await self.found( + lambda line: isinstance(line, ConversationRow) and line.key == name, f"thread {name}") tree.scrollToItem(row) await asyncio.sleep(0.5) cells = int(os.environ.get("TOAD_WAVE_CELLS", "0")) @@ -1851,12 +1861,11 @@ async def run(self) -> None: app.resize(self.width, 915) target = os.environ.get("TARGET", "#scratch") if target.startswith("#"): - await self.channel_listed(target) + await self.channel_row(target) await self.expand(target) - app.open_row(self.channel_row(target)) + app.open_row(await self.channel_row(target)) else: - await until(lambda: any(row.key == target for row in self.thread_rows()), target) - app.open_row(next(row for row in self.thread_rows() if row.key == target)) + app.open_row(await self.listed_thread(target)) for tick in range(120): page = self.page rows = page.messages.model_rows.rowCount() if hasattr(page, "messages") else None @@ -2096,6 +2105,38 @@ def wrapper(*args, **kwargs): timed(qt_history.QtHistoryWindow, name, f"window.{name}") timed(qt_history.HistoryRows, "show", "rows.show") timed(virtual_rows.RowDocumentDelegate, "_row_prepared", "delegate._row_prepared") + if os.environ.get("FRAME_TIME_OPEN"): + # What opening a page costs, by owner (tools only). + from objectstate.lazy_factory import LazyDataclassFactory + from pyqt_reactive.widgets import virtual_rows + from toad import object_configuration + from toad.qt import history as qt_history + def timed_open(owner, attribute, label): + original = owner.__dict__[attribute] + wrap = type(original) if isinstance(original, (staticmethod, classmethod)) else None + function = original.__func__ if wrap else original + def wrapper(*args, **kwargs): + started = time.perf_counter() + try: + return function(*args, **kwargs) + finally: + timings.setdefault(label, []).append((time.perf_counter() - started) * 1000) + setattr(owner, attribute, wrap(wrapper) if wrap else wrapper) + timed_open(object_configuration.ObjectConfiguration, "__init__", "open.view_configuration") + timed_open(LazyDataclassFactory, "make_lazy_simple", "open.lazy_type") + # A settings write, and its two registry-wide parts. + from objectstate import ObjectStateRegistry + from toad.preferences import SavedSettings + timed_open(SavedSettings, "set", "write.settings_set") + timed_open(ObjectStateRegistry, "_record_snapshot_internal", "write.snapshot") + timed_open(ObjectStateRegistry, "invalidate_by_type_and_scope", "write.invalidate") + delegate = virtual_rows.RowDocumentDelegate + for name in ("sizeHint", "row_document", "measured_height", "_row_prepared", "_rows_prepared", + "_update_viewport"): + if name in delegate.__dict__: + timed_open(delegate, name, f"rows.{name}") + for name in ("rows_changed", "show_pages"): + timed_open(qt_history.QtHistoryWindow, name, f"window.{name}") self.timings = timings def write_timings(self, probe) -> None: @@ -2218,6 +2259,9 @@ async def action(self, probe, name: str, act) -> None: await asyncio.sleep(self.settle) probe.sink.write(f"action {name}: trigger_returned_ms={took:.0f}\n") probe.report(time.perf_counter()) + # The owners' calls this action made, then a fresh count for the next one. + self.write_timings(probe) + self.timings.clear() async def run(self) -> None: app = self.app @@ -2227,23 +2271,24 @@ async def run(self) -> None: probe.REPORT_SECONDS = 1e9 # one summary per action, written by `action` self.install_timings() await self.listed() - await until(lambda: any(isinstance(row, ConversationRow) and row.target.name == "#comms" - for row in app.sidebar.items()), "#comms row") + await self.channel_row("#comms") app.show_sidebar() await asyncio.sleep(2.0) home = app.workspace_sessions.home - await self.action(probe, "open #comms (tap its row, cold)", lambda: self.tap_row(self.channel_row("#comms"))) + comms = await self.channel_row("#comms") + await self.action(probe, "open #comms (tap its row, cold)", lambda: self.tap_row(comms)) await until(lambda: self.page is not home, "#comms open") if any(isinstance(row, ConversationRow) and row.target.name == "#scratch" for row in app.sidebar.items()): before = self.page + scratch = await self.channel_row("#scratch") await self.action(probe, "open #scratch (a second channel, cold)", - lambda: self.tap_row(self.channel_row("#scratch"))) + lambda: self.tap_row(scratch)) await until(lambda: self.page is not before, "#scratch open") - group = next(row for row in app.sidebar.items() - if isinstance(row, ConversationRow) and row.childCount() and not row.isExpanded()) + group = await self.found(lambda row: isinstance(row, ConversationRow) and row.childCount() + and not row.isExpanded(), "closed channel group", rows=app.sidebar.items) await self.action(probe, f"expand channel group {group.key}", lambda: app.sidebar.expandItem(group)) - thread = next(row for row in self.rows(app.sidebar.items()) - if isinstance(row, ConversationRow) and row.key == "scratch-live-pipeline") + thread = await self.found(lambda row: isinstance(row, ConversationRow) and row.key == "scratch-live-pipeline", + "thread scratch-live-pipeline") before = self.page await self.action(probe, "open thread scratch-live-pipeline (tap its row, cold)", lambda: self.tap_row(thread)) await until(lambda: self.page is not before, "thread open") @@ -2284,8 +2329,8 @@ async def run(self) -> None: app = self.app app.resize(self.width, 915) await self.expand("Threads") - row = next(row for row in self.rows(app.sidebar.items()) - if isinstance(row, ConversationRow) and row.target.name == self.peer) + row = await self.found(lambda row: isinstance(row, ConversationRow) and row.target.name == self.peer, + f"thread {self.peer}") home = app.workspace_sessions.home app.open_row(row) await until(lambda: self.page is not home and self.bar() is not None, "dm") @@ -2555,14 +2600,13 @@ async def run(self) -> None: app.resize(412, 640) await asyncio.sleep(0.5) app.show_sidebar() - await until(lambda: any(isinstance(row, ConversationRow) and row.target.name == "#comms" - for row in app.sidebar.items()), "#comms listed", tenths=200) + await self.channel_row("#comms") await self.expand("#comms", "#any") await self.exercise("1-welcome-sidebar") # A tap on a sidebar row still opens it: the scroller holds the press back, then # delivers it when the finger lifts without dragging. tree = app.sidebar - comms = self.channel_row("#comms") + comms = await self.channel_row("#comms") tree.scrollToItem(comms) await asyncio.sleep(0.3) home = app.workspace_sessions.home @@ -2664,6 +2708,24 @@ async def drive(scenario: type[Scenario], connection: CoreConnection, out: Path) await app.close_client() +def time_settings_writes() -> None: + """FRAME_TIME_WRITES: each settings write's time, with the registered states and how many + a write left changed for its snapshot (tools only).""" + from objectstate import ObjectStateRegistry + from toad.preferences import SavedSettings + write = SavedSettings.set + + def timed(self, path, value) -> None: + states = len(ObjectStateRegistry._states) + started = time.perf_counter() + try: + write(self, path, value) + finally: + took = (time.perf_counter() - started) * 1000 + print(f" settings write {path}: {took:.1f} ms, {states} states", flush=True) + SavedSettings.set = timed + + def main(arguments: list[str]) -> None: own = arguments[1:] connection = select_connection(own) if len(own) == 4 else None @@ -2671,6 +2733,8 @@ def main(arguments: list[str]) -> None: names = "\n".join(f" {member.declared_name:10} {member.summary}" for member in Scenario.members_with(Scenario)) raise SystemExit(f"usage: {arguments[0]} SCENARIO OUTDIR (--comms-profile PROFILE | --comms-root ROOT)\n{names}") scenario = Scenario.member_named(own[0]) + if os.environ.get("FRAME_TIME_WRITES"): + time_settings_writes() application = FrameProbe.application(arguments[:1]) install_glyph_fonts(application) # the entry's splash registers them; this drive has none FingerScrolling.install(application, scenario.finger_gesture())