Skip to content

with ast: one buffer per list and a fill printed as written; check_.w >36 min -> 0.15 s (#1358) - #1513

Merged
ehartford merged 1 commit into
fix-1352-oneliner-streamingfrom
fix-1358-ast-linear
Sep 23, 2026
Merged

ehartford merged 1 commit into
fix-1352-oneliner-streamingfrom
fix-1358-ast-linear

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #1358.

Root cause. Timing ast per top-level declaration of lib/std/tommyds/check_.w: 37 declarations dump in 0.01 s each; one, var the_cache: [16777216]u8 = [0 as u8; 16777216], takes the time.

  1. The parser desugars [v; N] into N copies of v's one node, and the renderer printed every copy: 16,777,216 elements, ~150 MB of dump for a 50-byte declaration.
  2. The renderer grew each list with out = out ++ piece after if i > 0: out = out ++ ", ". The append after the if lowers to the copying with_str_concat_n (lower_if forgets the string flow facts, so the in-place move_first append is refused — filed as MIR: a string self-append after an if statement copies the whole string (lower_if forgets string flow facts): s = s ++ f() in a loop is quadratic #1491), so each element copied the list so far: a fill of 16000 dumped in 0.38 s, of 64000 in 6.32 s. sample: 1851/2161 samples in str_concat_n_copy.

Fix. Every string accumulator in render.w is a StringBuilder, and an NK_ARRAY_LIT whose elements are all one node (only the fill desugar produces that) prints as [v; N].

Evidence. ast of every tracked .w file (3392) with stage1 before/after: 3325 dumps byte-identical; the other 63 are byte-identical once each [E; N] of the new dump is expanded back into N copies; the 4 that timed out at 20 s now finish. Before → after: check_.w >20 s (>36 min in the issue) → 0.15 s, lib/std/re/defs.w >20 s → 0.12 s, rt/fiber_core_{darwin,windows}.w >20 s → 0.01 s, lib/std/build.w 12.7 s → 0.02 s, src/compiler/LlvmBridge.w 9.3 s → 0.02 s. A written literal of 40000 / 160000 elements: 2.00 s / 29.34 s before, 0.10 s / 0.43 s after.

Test. test/behavior/behav_ast_dump_linear.w: the check_.w fill dumps as [0 as u8; 16777216] (before: killed at its 120 s capture timeout), and a written list's dump time at 4N stays within the test/complexity bound of the time at N.

Found on the way: #1478 ([v; N] with a const N silently builds one element; the desugar stores N extras).

@ehartford
ehartford added this pull request to stack #1516 September 23, 2026 15:59
@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1516 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main + #1509 → … → #1515 (head 63e8340): build ok, :fixpoint ok (stage2 == stage3 with DWARF variables), :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merge the stack: gh stack merge 1515 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 16:30
… written; check_.w from >36 min to 0.15 s (#1358)

Two causes, found by timing `ast` per top-level declaration of
lib/std/tommyds/check_.w: 37 declarations dump in 0.01 s each and one,
`var the_cache: [16777216]u8 = [0 as u8; 16777216]`, takes the time.

1. The parser desugars `[v; N]` into N copies of v's one node, and the
   renderer printed every copy: 16,777,216 elements, about 150 MB of dump
   for a 50-byte declaration.
2. The renderer grew each list with `out = out ++ piece` after
   `if i > 0: out = out ++ ", "`. The append after the `if` lowers to the
   copying with_str_concat_n (lower_if forgets the string flow facts, so
   `out` may alias and the in-place move_first append is refused; filed as
   #1491), so each element copied the whole list so far: a fill of 16000
   dumped in 0.38 s, of 64000 in 6.32 s. `sample` of the check_.w run:
   1851 of 2161 samples in str_concat_n_copy, 246 in move_first.

render.w: every string accumulator is a StringBuilder (a one-off Lexer-free
rewrite of the `var out = ..`/`out = out ++ ..` lines, completed by hand
where the type checker flagged a use), and an NK_ARRAY_LIT whose elements
are all one node (only the fill desugar produces that) prints as
`[v; N]`.

Evidence, `ast` of every tracked .w file (3392) with stage1 before and
after this change: 3325 dumps byte-identical; the other 63 are byte-identical
once each `[E; N]` of the new dump is expanded back into N copies of E; the
4 that timed out at 20 s before now finish. Times before -> after:
check_.w >20 s (>36 min in the issue) -> 0.15 s, lib/std/re/defs.w >20 s ->
0.12 s, rt/fiber_core_{darwin,windows}.w >20 s -> 0.01 s, lib/std/build.w
12.7 s -> 0.02 s, src/compiler/LlvmBridge.w 9.3 s -> 0.02 s. A written
literal of 40000 / 160000 elements: 2.00 s / 29.34 s before, 0.10 s /
0.43 s after.

test/behavior/behav_ast_dump_linear.w: the check_.w fill dumps as
`[0 as u8; 16777216]` (before: killed at its 120 s capture timeout), and a
written list's dump time at 4N stays within the test/complexity bound of the
time at N.

The fill desugar itself (N extras per fill, and `[v; N]` with a const N
silently building one element) is #1478.
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.

with ast: render_module is quadratic; lib/std/tommyds/check_.w (72 KB) runs for more than 36 minutes

1 participant