Repository navigation
Commit 017780e
authored
perf: keep calls out of the tail-call instruction handlers (#77)
With the tail-call dispatch, a handler that contains a call saves and
restores a frame record on every instruction it executes, even when the
call is on a path that validated code never takes. On `next` all but
three of the 615 handlers had such a call: the
`instruction_handler_mismatch` and `stack_underflow` panics, the
bounds-check panics of value-stack and global indexing, or the
conversion of the `Trap::ValueStackOverflow` a push could return into an
`ExecError`.
The handlers now `become` the mismatch and instruction-pointer panics
instead of calling them. Value-stack and global accesses that validation
rules out stop through `invariant_violated`, which with
`nightly-tail-calls` in release builds is `core::intrinsics::abort` (an
inline trap instruction) and otherwise the panic it was before. A push
inside a function body no longer returns a `Result`: `enter_locals`
reserves the function's whole operand stack or traps before the body
runs (#59). Only modules that skip validation
can break these invariants, and archives, which do, are already
documented as trusted input.
The second commit inlines the five memory helpers that were not
`#[inline(always)]`, so a load or store through a local address no
longer calls `exec_load_local` and similar.
448 of the 615 handlers are now frameless, and `i32.add` is 23
instructions instead of 31. Memory, call and return handlers still keep
a frame for their traps and slow paths.
Change in cycles per call against `next` (a0ea681), on the efficiency
cores of an iPhone 12 (A14), iPhone XS Max (A12) and iPhone SE (A13),
median of five interleaved launches per build:
| benchmark | A14 | A12 | A13 |
|---|---:|---:|---:|
| xmrsplayer (1024-frame buffer) | −5.5% | −4.0% | −12.3% |
| audio DSP (1000 frames × 512) | −3.8% | −4.3% | −9.7% |
| graphql-validation (AS) | −4.6% | −5.5% | −7.7% |
| multi-memory twin: one memory | −9.2% | −8.7% | −11.1% |
| crc32 (64 KB) | −5.5% | −10.4% | −7.7% |
| convolution 256×256 | −3.5% | −5.9% | −6.1% |
| sieve (10000) | −14.1% | −8.6% | −13.9% |
| bulk_memory (memory.copy/fill) | −6.7% | −8.3% | −7.6% |
| matmul relaxed-simd FMA | −6.0% | −8.4% | −9.7% |
| GC binary trees (~130K struct.new) | −1.3% | −2.5% | −1.9% |
| fib(30) | −8.7% | −6.4% | −7.6% |
| tail-call FSM (65536 return_call) | −8.3% | −6.5% | −7.7% |
| call_indirect (200K) | −2.9% | −5.9% | −3.9% |
| call_ref (200K) | −4.3% | −3.8% | −5.0% |
| vtable_poly4 (200K) | −4.3% | −4.1% | −5.3% |
| EH parser, exnref (4096 stmts, 25% throw) | −2.8% | −1.4% | −3.2% |
| **geomean, cycles** | **−5.8%** | **−6.0%** | **−7.6%** |
| geomean, instructions | −7.2% | −7.2% | −7.2% |
| geomean, wall time | −5.7% | −6.0% | −7.7% |
| rows faster (cycles) | 16/16 | 16/16 | 16/16 |
On an M4's efficiency cores these two commits give −8.6% instructions
and −5.6% cycles, and the loop dispatch (without `nightly-tail-calls`)
−3.5% instructions and −1.1% cycles. Some gory details:
* xmrsplayer cycles per call fall from 39.5M to 37.4M, graphql from
32.3M to 30.8M.
* branch mispredicts stay flat, within ±5%.
* the main one cost is memory-order flushes: 7.7k → 13.2k per xmrsplayer
call, roughly 0.3–0.4% of cycles against the 5.5% gain.
I elaborated on the memory-order flushes in a separate PR. The good news
is that the extra inlining isn't busting the cache, and the call
avoidance isn't unraveling some other CPU performance in a way I didn't
predict. we'll keep slamming into this memory-order flush stall pileup
until we deal with it explicitly, but it's still net positive on wall
clock and benchmarks even on the cut-down efficiency cores :)1 parent a0ea681 commit 017780e
10 files changed
Lines changed: 189 additions & 104 deletions
File tree
- crates/tinywasm/src
- interpreter
- executor
- stack
- store
- gc
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | 9 | | |
16 | 10 | | |
17 | 11 | | |
| |||
36 | 30 | | |
37 | 31 | | |
38 | 32 | | |
39 | | - | |
40 | | - | |
41 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
42 | 38 | | |
43 | 39 | | |
44 | 40 | | |
| |||
55 | 51 | | |
56 | 52 | | |
57 | 53 | | |
58 | | - | |
| 54 | + | |
59 | 55 | | |
60 | 56 | | |
61 | | - | |
| 57 | + | |
62 | 58 | | |
63 | 59 | | |
64 | 60 | | |
| |||
98 | 94 | | |
99 | 95 | | |
100 | 96 | | |
101 | | - | |
102 | | - | |
103 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
104 | 102 | | |
105 | 103 | | |
106 | 104 | | |
| |||
116 | 114 | | |
117 | 115 | | |
118 | 116 | | |
119 | | - | |
| 117 | + | |
120 | 118 | | |
121 | 119 | | |
122 | | - | |
| 120 | + | |
123 | 121 | | |
124 | 122 | | |
125 | 123 | | |
| |||
130 | 128 | | |
131 | 129 | | |
132 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
133 | 146 | | |
134 | 147 | | |
135 | 148 | | |
136 | 149 | | |
137 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
138 | 165 | | |
139 | 166 | | |
140 | 167 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | | - | |
| 12 | + | |
12 | 13 | | |
13 | | - | |
| 14 | + | |
14 | 15 | | |
15 | | - | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | | - | |
| 25 | + | |
25 | 26 | | |
26 | | - | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | | - | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | | - | |
| 34 | + | |
34 | 35 | | |
35 | | - | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
99 | 100 | | |
100 | 101 | | |
101 | 102 | | |
102 | | - | |
103 | | - | |
104 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
105 | 106 | | |
106 | 107 | | |
107 | 108 | | |
| |||
201 | 202 | | |
202 | 203 | | |
203 | 204 | | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
208 | 209 | | |
209 | 210 | | |
210 | 211 | | |
| |||
289 | 290 | | |
290 | 291 | | |
291 | 292 | | |
292 | | - | |
293 | | - | |
| 293 | + | |
| 294 | + | |
294 | 295 | | |
295 | 296 | | |
296 | 297 | | |
| |||
459 | 460 | | |
460 | 461 | | |
461 | 462 | | |
462 | | - | |
| 463 | + | |
463 | 464 | | |
464 | 465 | | |
465 | 466 | | |
| |||
0 commit comments