Skip to content

Commit dd60567

Browse files
authored
web/docs: swap duplicate tailcall example for an inlining showcase (#256)
The trace-playground "Watching the optimizer" section carried two near-identical tail-call examples (TailSum and TailFactorial). Drop TailSum and add an inlining example (InlineDemo) alongside the remaining TailFactorial, so the section demonstrates two distinct optimizer transforms rather than the same one twice. InlineDemo's leaf `square` helper is inlined into both call sites at O2; the spliced instructions carry transform: ["inline"] and the call stack renders them as virtual inline activations. Reworks the section intro and closing prose to cover both transforms.
1 parent a73533c commit dd60567

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

‎packages/web/docs/explore/trace-playground.mdx‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { TracePlayground, TraceExample } from "@theme/ProgramExample";
88
import {
99
counterIncrement,
1010
functionCallAndReturn,
11-
tailRecursiveSum,
1211
tailRecursiveFactorial,
12+
inlineDemo,
1313
} from "../core-schemas/programs/tracing-examples";
1414

1515
# Trace playground
@@ -57,26 +57,39 @@ For the exact shape of invoke, return, and revert contexts, see the
5757

5858
Compilers rewrite code as they optimize, and **transform** contexts
5959
record what they did. Set the **Opt** selector to **O2** and step through
60-
these tail-recursive programs: the recursive call folds into a loop, and
61-
the back-edge JUMP carries `transform: ["tailcall"]` next to its invoke
62-
and return. The call stack stays flat instead of growing one frame per
63-
iteration.
60+
the examples below: each instruction the optimizer rewrote carries a
61+
`transform` array naming the passes responsible.
6462

65-
<TraceExample
66-
title="Tail-recursive sum"
67-
description="Sums 1..5 with an accumulator; TCO folds it into a loop"
68-
source={tailRecursiveSum}
69-
/>
63+
**Tail-call optimization.** In `TailFactorial` the recursive call folds
64+
into a loop. The back-edge JUMP carries `transform: ["tailcall"]` next to
65+
its invoke and return, and the call stack stays flat instead of growing
66+
one frame per iteration.
7067

7168
<TraceExample
7269
title="Tail-recursive factorial"
7370
description="Computes 5! with an accumulator; TCO folds it into a loop"
7471
source={tailRecursiveFactorial}
7572
/>
7673

77-
Flip the **Opt** selector between **O0** and **O2** to see the call stack
78-
change. For how the `tailcall` transform composes with invoke/return, see
79-
the [transform context spec](/spec/program/context/transform) and the
74+
**Inlining.** In `InlineDemo` the leaf helper `square` is small enough
75+
that at **O2** the compiler splices its body straight into each call
76+
site. `sumOfSquares = square(a) + square(b)` calls it twice, and after
77+
inlining neither call JUMPs into `square` — each multiply runs inline, and
78+
those instructions carry `transform: ["inline"]` pointing back to the
79+
`square` body they came from. The call stack marks the inlined `square`
80+
as a virtual activation, not a real pushed frame.
81+
82+
<TraceExample
83+
title="Inlined leaf calls"
84+
description="Inlines the square helper into both call sites at O2"
85+
source={inlineDemo}
86+
/>
87+
88+
Flip the **Opt** selector between **O0** and **O2** to see the difference:
89+
at **O0** each program calls its helper the ordinary way; at **O2** the
90+
transforms above take over. For how these transforms compose with
91+
invoke/return, see the
92+
[transform context spec](/spec/program/context/transform) and the
8093
[tail-call optimization walkthrough](/docs/core-schemas/programs/tracing).
8194

8295
</TracePlayground>

0 commit comments

Comments
 (0)