| @@ -356,22 +356,51 @@ pub(crate) unsafe fn preempt_in_fiber(ctx: *mut Ctx) -> bool { |
| 356 | 356 | /// operation of this handler; returns the handler's result. |
| 357 | 357 | /// |
| 358 | 358 | /// A clause that calls `resume` and then does more work (`x + resume(())`) |
| 359 | | −/// nests `resume_code`, `drive` and `call_clause` once per operation: each |
| 360 | | −/// ends with a tail call, so that the nesting costs as little stack as |
| 361 | | −/// possible. |
| 359 | +/// nests `resume_code`, `drive` and `call_clause` once per operation, so |
| 360 | +/// `drive`'s frame is paid once per pending `resume`: 100,000 of them in |
| 361 | +/// `tests/bench/effects.lse`. Running the fiber needs a large frame, so it |
| 362 | +/// is out of line ([`run`]), and what nests here is only the call of the |
| 363 | +/// clause. Inlined, with the runtime built as one LTO unit (ADR 0031), it |
| 364 | +/// took 352 bytes of stack per operation instead of 192: twice the page |
| 365 | +/// faults, and `effects` 17% slower. |
| 362 | 366 | /// |
| 363 | 367 | /// # Safety |
| 364 | 368 | /// `ctx` must be the running actor's context; `block` a live frame. |
| 365 | | −unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 369 | +unsafe fn drive(ctx: *mut Ctx, block: Value, input: Resume) -> Value { |
| 370 | + // SAFETY: forwarded caller guarantee; the frame outlives the call. |
| 371 | + unsafe { |
| 372 | + match run(ctx, block, input) { |
| 373 | + Stop::Done(value) => value, |
| 374 | + Stop::Clause(payload, resume) => call_clause(ctx, frame(block).cap, payload, resume), |
| 375 | + } |
| 376 | + } |
| 377 | +} |
| 378 | + |
| 379 | +/// Where [`run`] left a handler. |
| 380 | +enum Stop { |
| 381 | + /// Its result. |
| 382 | + Done(Value), |
| 383 | + /// The operation in the payload is to be handled by its clause, with |
| 384 | + /// this `resume` (none for a `final ctl`). |
| 385 | + Clause(Value, Option<Value>), |
| 386 | +} |
| 387 | + |
| 388 | +/// [`drive`]'s loop: resume the fiber until the handler has a result or |
| 389 | +/// a clause to run. |
| 390 | +/// |
| 391 | +/// # Safety |
| 392 | +/// As [`drive`]. |
| 393 | +#[inline(never)] |
| 394 | +unsafe fn run(ctx: *mut Ctx, block: Value, mut input: Resume) -> Stop { |
| 366 | 395 | // SAFETY: caller guarantees a live frame; the fiber runs on this actor. |
| 367 | 396 | unsafe { |
| 368 | 397 | loop { |
| 369 | 398 | let f = frame(block); |
| 370 | 399 | let Some(mut fiber) = f.fiber.take() else { |
| 371 | | − return crate::effects::tl_panic_str( |
| 400 | + return Stop::Done(crate::effects::tl_panic_str( |
| 372 | 401 | ctx, |
| 373 | 402 | c"`resume` called after its handler finished".as_ptr(), |
| 374 | | − ); |
| 403 | + )); |
| 375 | 404 | }; |
| 376 | 405 | let saved = *current(ctx); |
| 377 | 406 | let step = fiber.resume(input); |
| @@ -392,15 +421,15 @@ unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 392 | 421 | if c.yielding == YIELD_FINAL && c.yield_marker == f.marker { |
| 393 | 422 | let payload = std::mem::replace(&mut c.yield_value, UNIT); |
| 394 | 423 | c.yielding = YIELD_NONE; |
| 395 | | − return call_clause(ctx, f.cap, payload, None); |
| 424 | + return Stop::Clause(payload, None); |
| 396 | 425 | } |
| 397 | 426 | if c.yielding != YIELD_NONE { |
| 398 | | − return UNIT; // unwinding continues in the caller |
| 427 | + return Stop::Done(UNIT); // unwinding continues in the caller |
| 399 | 428 | } |
| 400 | 429 | if is_ptr(f.ret) { |
| 401 | | − return call_closure(ctx, dup_value(f.ret), &[value]); |
| 430 | + return Stop::Done(call_closure(ctx, dup_value(f.ret), &[value])); |
| 402 | 431 | } |
| 403 | | − return value; |
| 432 | + return Stop::Done(value); |
| 404 | 433 | } |
| 405 | 434 | CoroutineResult::Yield(request) => { |
| 406 | 435 | f.fiber = Some(fiber); |
| @@ -419,7 +448,7 @@ unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 419 | 448 | f.waiting = true; |
| 420 | 449 | resume_closure(ctx, block, f.suspensions, resume_code) |
| 421 | 450 | }; |
| 422 | | − return call_clause(ctx, f.cap, payload, Some(resume)); |
| 451 | + return Stop::Clause(payload, Some(resume)); |
| 423 | 452 | } |
| 424 | 453 | Request::Op { marker, payload } => { |
| 425 | 454 | // Not ours: ask the handlers around this one. |
| @@ -437,7 +466,7 @@ unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 437 | 466 | if !finish(ctx, block) { |
| 438 | 467 | (*ctx).yielding = YIELD_CANCEL; |
| 439 | 468 | } |
| 440 | | − return UNIT; |
| 469 | + return Stop::Done(UNIT); |
| 441 | 470 | } |
| 442 | 471 | } |
| 443 | 472 | } |