Commit e02c21e
committed
fix(effect): embed Resume<T> scratch in state machine, drop per-perform malloc
cd3184a fixed the j3 stack-lifetime bug by switching the Resume<T>
struct from `Alloca` to `Intrinsic::Malloc(32)`. Correct but unbounded:
every perform-site invocation allocated 32 bytes with no free path.
Long-running programs with hot perform sites would accumulate the
allocations forever.
Reserve a 4-slot region (= 32 bytes, the exact Resume<T> size) at the
end of the state machine. `lower_async_function` now allocates
`num_slots + 4` and returns `resume_scratch_slot` (the base slot
index) in `LowerResult`. `upgrade_resume_struct_at_perform_sites`
takes that slot, computes `r_ptr = sm_ptr + (slot * 8)`, and writes
the four Resume<T> fields there instead of malloc'ing.
The scratch's lifetime matches the state machine's — already heap-
allocated by `generate_sync_entry`'s malloc, never freed today. So no
new free path is needed, and the Resume<T> outlives any out-of-line
capture (j3 still passes — the stashed pointer points into the SM,
valid as long as the SM is alive). Memory cost drops from O(perform-
site invocations) to O(state-machine instantiations).
Known limitation (documented inline): all perform sites in the same
function share this one scratch region. Nested or interleaved performs
with async stashing — host holds k from perform A while perform B's
handler runs — would alias the slot and corrupt the stashed pointer.
Current tests all have a single perform site per function; extending
to N per-perform scratches (one slot region per site) is a future
refinement when multi-perform-with-stash programs land.
Workspace `cargo test --tests` = 1439/0; effect runtime tests all
10/10; clippy + fmt clean.1 parent cd3184a commit e02c21e
3 files changed
Lines changed: 64 additions & 36 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1101 | 1101 | | |
1102 | 1102 | | |
1103 | 1103 | | |
| 1104 | + | |
1104 | 1105 | | |
1105 | 1106 | | |
1106 | 1107 | | |
| |||
1169 | 1170 | | |
1170 | 1171 | | |
1171 | 1172 | | |
1172 | | - | |
| 1173 | + | |
1173 | 1174 | | |
1174 | | - | |
1175 | | - | |
1176 | | - | |
1177 | | - | |
1178 | | - | |
1179 | | - | |
1180 | | - | |
1181 | | - | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
1182 | 1183 | | |
1183 | | - | |
1184 | | - | |
1185 | | - | |
1186 | | - | |
1187 | | - | |
1188 | | - | |
1189 | | - | |
1190 | | - | |
1191 | | - | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
1192 | 1192 | | |
1193 | | - | |
1194 | | - | |
1195 | | - | |
1196 | | - | |
1197 | | - | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
1198 | 1203 | | |
1199 | 1204 | | |
1200 | 1205 | | |
1201 | 1206 | | |
1202 | 1207 | | |
1203 | | - | |
1204 | | - | |
1205 | | - | |
1206 | | - | |
1207 | | - | |
1208 | | - | |
1209 | | - | |
1210 | | - | |
1211 | | - | |
1212 | | - | |
1213 | | - | |
1214 | | - | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
1215 | 1214 | | |
1216 | 1215 | | |
1217 | 1216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
99 | 117 | | |
100 | 118 | | |
101 | 119 | | |
| |||
245 | 263 | | |
246 | 264 | | |
247 | 265 | | |
248 | | - | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
249 | 276 | | |
250 | 277 | | |
251 | 278 | | |
| |||
270 | 297 | | |
271 | 298 | | |
272 | 299 | | |
| 300 | + | |
273 | 301 | | |
274 | 302 | | |
275 | 303 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
959 | 959 | | |
960 | 960 | | |
961 | 961 | | |
| 962 | + | |
962 | 963 | | |
963 | 964 | | |
964 | 965 | | |
| |||
0 commit comments