Skip to content

Ariadne: the per-frame walk keeps its place on the heap, so any depth draws on a small stack - #1577

Merged
transfix merged 1 commit into
masterfrom
tfx/focused-tu-7a36aa
Nov 27, 2026
Merged

transfix merged 1 commit into
masterfrom
tfx/focused-tu-7a36aa

Conversation

@transfix

Copy link
Copy Markdown
Owner

Ariadne's per-frame walk no longer recurses: a document nested any depth draws in the same native stack. Asked for by Joe while SH1 (#1566) hit the stack edge ("validate the walk as stackless, as the DSL is, and make it iterative now").

Don't merge without Joe's OK.

What changes

  • The walk runs from a work stack on the heap (Runtime::Impl::walk_, stepped by run_walk/step_walk in src/cvc/ariadne/ariadne.cpp). emit, emit_node, emit_container, emit_children and the repeat expansion recursed once or more per widget level. Now each container pushes what it does after its children as a frame, in the order the recursive walk did it:
    • emit_node's window, group, menu, menubar, tabs and custom cases push a leave frame (leave_node) and return; what every widget does last is emit_node_tail, unchanged.
    • A repeat's instances (begin_repeat, Op::Instances) and a grid's cells (Op::Cells) are frames, with the outermost repeat's bookkeeping (emitting_repeat_, mount ticks), the per-frame instance cap and the instance's own copy.
    • emit_container's end (end_grid, the tracks/columns write-back, the scope and function pops) is Op::Container; a context menu's end is Op::ContextMenu.
    • The menubar's tail (the loaded documents' menus, the Windows menu, the tray), emit_app_menus, emit_tray and its overflow menu go on the walk as continuation (Then) frames.
    • run_walk drops what a throwing step left and pops the scopes, functions and repeat it held, as the recursive walk's guards did.
  • The per-tree helpers that ran inside render() and recursed are loops over an explicit stack: plan_windows_menu (and its first_bar), drop_mount_ticks, the isolation verdict's program and hole walks, substitute_index_in.
  • Widget copies and frees its tree a node at a time (inc/cvc/ariadne/widget.h): a new WidgetNode base holds every field but the two subtrees (children, context_items), and Widget's copy constructor and destructor go through a work list. Without it, a repeat over a deep template (copied each frame) and freeing a deep document still recursed. No field changed name or meaning; context_menu/context_menu_id moved into WidgetNode.
  • One native nesting is left: a registered widget fn's ctx.emit draws its child where the fn asks, between its own calls, so it runs a walk of its own: a native level per custom widget whose fn draws a custom widget. (The only registered fn in the tree, cvcGL's labeled, draws two texts.)
  • Comments in CMake/CvcHostStack.cmake and the 40-deep loader test now say loading recurses, drawing doesn't.

Depth before and after

Windows 11, MSVC 14.44 Release, each number the deepest that passed, bisected, on a boost::thread with that stack (ariadne_deep_nesting_test with CVC_ARI_DEEP_STACK_KIB/CVC_ARI_DEEP_DEPTH; master is 327a791 with the same test file).

The walk (a counting backend; plain groups, then every container kind, then menus and tray menus):

stack master: plain groups master: every kind master: menus this PR
64 KiB 0 — — 100000 (plain groups, menus and tray menus, repeats; FTXUI and tvision too)
256 KiB 10 11 9 every test at its default depth (3000–20000 levels)
1 MiB 51 52 52
2 MiB 106 — —
8 MiB 435 440 449

About 19 KB of stack per widget level before (emit_node's frame alone is 14.6 KB in Release, 23.5 KB in an MSVC Debug compile). After, a step of the walk takes a fixed amount of stack: every test passes on 64 KiB but FtxuiDrawsNestedContainers (below), and 100000 widget levels draw there.

A 40-level include: document (loaded on the 8 MiB main thread) drew on 256 KiB only up to 9 levels on master; it draws on 64 KiB now.

The loader is unchanged and still recurses (yaml-cpp's parser does, and so does parse_widget):

stack include: levels inline group levels
256 KiB 0 (nothing loads) 0
1 MiB 4 8
2 MiB 9 18
8 MiB 41 82

About 100 KB of stack per widget level: parse_widget_impl's frame is 95,168 bytes (__chkstk 0x173C0), a slot for every Widget temporary in its branches, and it is on the recursive path once a level. So the 41/42 edge ari snapshot hit on master was the loader, not the walk. Shrinking that frame and a loader nesting limit with a clear error, sized for the smallest shipped stack, are the follow-up planned in transfix/workstreams#4259; the probes (DISABLED_ProbeLoad*) are there to re-measure it.

Shipped stacks: the wasm builds link -sSTACK_SIZE=2097152 (ari's browser build, arigl, arigl-web, cyprty, the examples) or 8 MiB (ariglx), not Emscripten's 64 KB default; Windows hosts and tests 8 MiB (CvcHostStack.cmake); Linux 8 MiB.

Tests

src/cvc/tests/ariadne_deep_nesting_test.cpp (new), modelled on state_exec_deep_data_test's on_small_stack (256 KiB, CVC_ARI_DEEP_STACK_KIB overrides): every body draws on the small stack, two frames, and checks the deepest thing was drawn and every begin_*/push_id was closed.

  • WidgetCopiesAndFreesADeepTreeOnASmallStack (20000 levels and a context menu)
  • PlainGroups (10000), EveryContainerKind (3000: a group with an id, pointer handler, right-click menu and disabled scope; a grid cell; a tab bar; a mount with functions; a window; a horizontal row), MenusAndTrayMenus (5000-deep submenus in the bar, in a tray icon in the overflow menu and in a shown one), ContextMenusInContextMenus (2000), RepeatsOverADeepTemplate (5000, nested repeats as grid cells)
  • FTXUI: FtxuiDrawsPlainGroups, FtxuiDrawsMenusAndContextMenus, FtxuiDrawsNestedContainers (100 nested grids, item groups and context areas); tvision: TvisionDrawsPlainGroups
  • Loaded documents: ADocumentNestedThroughIncludesDraws (40 levels, as the loader test; counting backend and FTXUI), ADocumentNestedThroughLoadsDraws (31 nested load: mounts, the loader's cap, each with its own init:)
  • DISABLED_ProbeLoadIncludes, DISABLED_ProbeLoadInlineGroups, DISABLED_ProbeDrawIncludes measure the loader and the old walk.

Two limits these found that are not the walk's:

  • FTXUI lays out nested boxes recursively itself (the backend builds them with its own frame stack; the recursion is in FTXUI's layout of the element tree): about 400 bytes a box in Release, 627 nested boxes on 256 KiB, 93 on 64 KiB. Plain groups make no box, so deep documents mostly don't nest boxes. FtxuiDrawsNestedContainers stays at 100 (ten times what the old walk drew on 256 KiB) for Debug lanes' headroom. A fix would go in cvcpkg's FTXUI recipe or flatten boxes in ftxui_backend.cpp; not here.
  • A context menu's key (instance_key) and an unnamed item's id spell out its place in the tree, so each costs more the deeper it is; the context-menu tests use 2000 levels for that. The existing key scheme, not the stack.

Verified (Windows, MSVC Release, this box's .deps build with FTXUI and tvision)

  • ariadne_runtime_test 368, ariadne_loader_test 272, ariadne_host_test 66, ariadne_ftxui_test 126: all pass, unchanged.
  • ariadne_deep_nesting_test: 12/12 at 256 KiB; at 64 KiB 11/12 (FtxuiDrawsNestedContainers exceeds FTXUI's own recursion there, above).
  • Same backend calls as master: a scratch harness (not committed) recorded every Backend call for the 49 .ari documents under examples/ and 7 built ones (grids with repeats and hidden cells, row splits and tables, menus with the Windows menu merged, collapsed menubars, tray icons with menus and the overflow menu, context menus on windows, groups and the root, tabs, item groups, disabled scopes, mounts and repeated mounts with menubars as applications, nested repeats, backdrops, autofocus), three frames each, with menus open, closed, and a backend with no tray: 168 traces, byte-identical between master and this branch.
  • ari snapshot (FTXUI) of every example document: identical output on master and this branch (the 9 the terminal host draws; the rest are refused for gl.scene by both).
  • ctest -R ^ari_: the ari command tests built here pass.
  • Frame time, the scratch harness with a no-op backend, best of 7 runs, two runs each (µs a frame, this branch / master): a feed of 200 repeated cards 343–351 / 348–352; apps_demo.ari 25.8–27.0 / 24.1–24.5; json_feed.ari 25.0–26.5 / 24.8–24.9; fem_gallery.ari 10.1–10.5 / 9.4–9.7; built documents with a menubar, applications and a tray 33.7–40.9 / 28.1–33.7. So repeats are at parity (the first cut was 40% slower there, from the instance copy; it's now made in place), and walks with menus and trays pay about 1–7 µs a frame for their frames and continuations. The cvcGL frame benchmark (cvcgl_ariadne_frame) wasn't run here.
  • The walk's Debug stack: an MSVC Debug compile of ariadne.cpp has emit_node at 23.5 KB, step_walk 5.2 KB, render 8 KB, so a step stays well inside 256 KiB on the Debug lanes too (GCC's Debug-with-coverage frames weren't measured).

Not verified here: cvcGL's ImGui goldens (not built on this box; CI runs them), Linux and macOS (CI), wasm (no emscripten build here; its frames weren't measured).

Conflicts

#1566 (SH1) edits the same walk: it adds case Kind::Mounts calling emit_mounts (which loops over its mounts and calls emit() per mount), changes case Kind::MenuItemAction, and adds a Kind::Mounts case to plan_windows_menu's walk. Whichever merges second rebases: on this branch emit_mounts should push its wrappers as frames (a Then per mount, or a Children-like frame), since emit() from inside a step runs a nested walk; plan_windows_menu's Kind::Mounts case goes in the loop's switch as before.

… draws on a small stack

The walk (emit, emit_node, emit_container, emit_children and the repeat expansion) recursed once
or more per widget level, about 19 KB of stack a level in MSVC Release: 10 levels on a 256 KiB
stack, 435 on 8 MiB. It now runs from a work stack on the heap (Runtime::Impl::walk_, stepped by
run_walk/step_walk). Each container pushes what it does after its children (its end_*s, pop_ids,
scope and function pops, the grid's size write-back, a context menu's end) as a frame, in the
order the recursive walk did them, so the backends see the same calls.

- emit_node: the window, group, menu, menubar, tabs and custom cases push their leave frame
  (leave_node) and return; the rest is emit_node_tail, as before.
- The repeat expansion (begin_repeat, Op::Instances) and a grid's cells (Op::Cells) are frames,
  with the outermost repeat's bookkeeping, the per-frame instance cap and the instance's own copy.
- The menubar's tail (the loaded documents' menus, the Windows menu, the tray), emit_app_menus,
  emit_tray and emit_context_menu go on the walk as continuation frames.
- run_walk drops what a throwing step left and pops the scopes, functions and repeat it held, as
  the recursive walk's guards did.
- A registered widget fn's ctx.emit runs a walk of its own (the one native nesting left: a level
  per custom widget whose fn draws a child).
- plan_windows_menu (and its first_bar), drop_mount_ticks, the isolation verdict's two tree walks
  and substitute_index_in go through the tree with an explicit stack too.
- Widget copies and frees its tree a node at a time (WidgetNode holds every field but the two
  subtrees), so a repeat over a deep template, and freeing a deep document, do not recurse.

ariadne_deep_nesting_test draws trees thousands of widgets deep (plain groups, every container
kind, menus and tray menus, context menus in context menus, repeats over a deep template, a
40-level include: document, a 31-mount load: chain) on a 256 KiB stack, with a counting backend,
FtxuiBackend and TvisionBackend. FTXUI lays out nested boxes recursively itself (about 400 bytes a
box), so its nested-box test stays at 100. The loader still recurses (about 100 KB of stack a
widget level in MSVC Release); its DISABLED_Probe* tests measure where it runs out.

CMake/CvcHostStack.cmake and the 40-deep loader test's comment now say loading recurses and
drawing doesn't.
@transfix
transfix merged commit ed2f317 into master Nov 27, 2026
22 of 30 checks passed
@transfix
transfix deleted the tfx/focused-tu-7a36aa branch November 27, 2026 04:44
transfix added a commit that referenced this pull request Nov 27, 2026
…a small stack

Rebased onto the iterative walk (#1577):
- emit_mounts pushes a frame per open mount (its id and emit_path entry, the wrapper on the
  walk, then a Then that pops them) instead of emitting each in a nested walk.
- plan_windows_menu's Mounts case pushes each open mount's tree, then a marker visit that makes
  it an application if it brought no bar (ARIADNE_SHELL.md 4.9), keeping tree order.
- app_menu ends every application's menu through app_commands (Restart, Quit, and Close for a
  mounts: entry's); one without a bar gets those alone.
- drop_mount_ticks seeds its loop with the slots' wrappers.

Runtime::set_root's search for the document's own first mounts: slot recursed once a level:
it overflowed ariadne_deep_nesting_test's 256 KiB stack on PlainGroups, MenusAndTrayMenus and
the FTXUI and tvision plain-group tests. It is a loop now.

AriadneDeepNesting.AMountsSlotsEntriesDraw: three entries opened and drawn on the small stack, a
window nesting 3000 groups, a bar whose menu nests 3000 submenus, and one without a bar, each
with its Close item, every begin_* and push_id balanced.
transfix added a commit that referenced this pull request Nov 27, 2026
…ell.ari (#1566)

* Ariadne shell SH1: mounts:, app-open/app-close, budgets per mount, shell.ari

docs/roadmap/ARIADNE_SHELL.md 4.1, 4.2, 4.11 and the SH1 row; ARIADNE.md 6.7.

- mounts: <key>: one isolated mount per child of the key (uri, args), loaded through
  load_mount (expand_load in a fresh context, stateless) with the loader AppRuntime gives the
  Runtime (Runtime::set_mount_loader). Its init: runs and its on_tick: starts as it opens; it
  is an application (its bar one menu of the outer bar; one without a bar gets Restart, Quit,
  Close). Closing quits it, kills every process under its scope and frees its slot; its state
  stays, so reopening resumes it. Parsed again only when its uri, args or a file changes; hot
  reload keeps its resident when its program is the same. allow: (a list or { bind }) narrows
  what may be mounted, a refusal posted to the feed; catalog: names the documents that run as
  the local principal. The requires: check at mount refuses what the static scan would fail or
  warn on (a required entry, an unprovided call, gl.scene for a scene: on a host without one).
- (app-open URI [AS [ARGS]]) and (app-close AS) on the calling document's first slot; a mounted
  document gets a PermissionError.
- A budget per mount: cvc::state_exec::frame_account (execute_options::account, kept by spawn and
  fork). The async scheduler charges steps and seconds, ends a run at the limit, and parks a
  spent account's processes until the next frame (never killed); wake_awaiting starts each
  account's new frame. Memory is the processes' measured heap shares (process::measures_heap),
  summed by the Runtime. Defaults by principal (local 100000 steps, 0.025 s, 256 MB a frame;
  file: and remote: 20000, 0.005 s, 64 MB), settled against every shipped example (busiest:
  108 steps, under 0.4 ms a frame). Held for 3 s: a notice whose click offers Allow more, Quit
  and Restart. The apps mirror carries principal and budget.*; processes.ari shows each share.
- data: uri: { arg: NAME }; a menu_item: label that is an expression.
- components/shell.ari (--catalog in gallery.json format, --open), an Apps menu, a Launcher,
  processes.ari, a mounts: slot; examples/ariadne/documents/shell_apps.json (tray_demo,
  json_feed, apps/counter).
- The .ari schema learns mounts:, allow:, catalog: and uri: { arg }.

Tests: AriadneMounts.* (open, close with its memory freed, reopen with state kept, the verbs,
a refused URI, unmet requires:, hot reload, a busy mount held while another runs, a mount past
its memory, the shipped examples within the local budget), AriadneComponents.ShellOpensIts-
CatalogOnDemand, AriadneLoader.MountsSlotAndADataUriFromAnArgument and LoadMountParsesAn-
EntrysDocument, AsyncSchedulerTest.AFrameAccountHoldsItsProcessesToTheirFrame, and
ari_run_shell_{ftxui,tvision} (tmux, Linux).

* mounts: compare an allow: or catalog: path in the resolver's canonical form

On the Windows CI runner the temp directory is spelled with an 8.3
short name (C:\Users\RUNNER~1\...). The slot's base comes from the
resolver, which uses weakly_canonical, so it holds the long name. The
entry's path was only made lexically normal, so the two never matched,
and AriadneMounts.ARefusedUriIsPostedToTheFeed saw its allowed file
refused.

mount_norm now makes a local path canonical the way the resolver does
(links followed, short names spelled out), and keeps a trailing slash
so an allow: prefix still matches only under its directory.
Reproduced and verified locally by running ariadne_runtime_test with
TMP set to a short path: all 377 pass.

* mounts: no recursion on a deep document, and a mounts: slot drawn on a small stack

Rebased onto the iterative walk (#1577):
- emit_mounts pushes a frame per open mount (its id and emit_path entry, the wrapper on the
  walk, then a Then that pops them) instead of emitting each in a nested walk.
- plan_windows_menu's Mounts case pushes each open mount's tree, then a marker visit that makes
  it an application if it brought no bar (ARIADNE_SHELL.md 4.9), keeping tree order.
- app_menu ends every application's menu through app_commands (Restart, Quit, and Close for a
  mounts: entry's); one without a bar gets those alone.
- drop_mount_ticks seeds its loop with the slots' wrappers.

Runtime::set_root's search for the document's own first mounts: slot recursed once a level:
it overflowed ariadne_deep_nesting_test's 256 KiB stack on PlainGroups, MenusAndTrayMenus and
the FTXUI and tvision plain-group tests. It is a loop now.

AriadneDeepNesting.AMountsSlotsEntriesDraw: three entries opened and drawn on the small stack, a
window nesting 3000 groups, a bar whose menu nests 3000 submenus, and one without a bar, each
with its Close item, every begin_* and push_id balanced.

* mounts: a mounts: entry's state: block is written as it opens

#1563 added a document's state: block. A load:ed module's seeds are written at its sub-prefix
just before its init:, once, and again on a Restart. A mounts: entry's wrapper carries the same
seeds (load_mount goes through expand_load), but open_mount ran only its init:. That marked the
mount done in ran_inits, so the seeds were never written. open_mount now writes them just
before the init:, under the same once-only check. Reopening resumes without writing them, and a
Restart writes them again (emit_container, as for a load: module).

AriadneMounts.AMountsStateBlockIsWrittenAsItOpens checks that init: reads the seeds, a reopen
keeps a changed value, and a Restart writes the seeds again. It fails without the fix: the
mount opens but count is empty. ARIADNE.md 5.3 and 6.7 and the ARIADNE_SHELL.md 4.2 note say so.

* mounts: no function-local static for a slot without a spec

mount_principal and open_mount each kept a `static const MountsSpec kNoSpec` to stand for a slot
without a spec. A function-local static of a non-trivial type is a bss object and a guard
variable, so the writable-data scan on package-linux / libcvc-release-grpc failed on four
symbols that are not on the allowlist. Each is now a local MountsSpec: an empty one costs no
allocation, and nothing is left process-wide.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant