Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Continuation is to be continued

Masayuki Mizuno
April 22, 2025
300

Continuation is to be continued

Masayuki Mizuno

April 22, 2025
Tweet

Transcript

  1. ©2025 Wantedly, Inc. Continuation is to be continued A revisit

    of first-class continuations or an invitation to delimited continuations RubyKaigi 2025 Mar 16, 2025 - Masayuki Mizuno
  2. ©2025 Wantedly, Inc. .BTBZVLJ.J[VOP "CPVUNF • Backend engineer at Wantedly,

    Inc. • Born in Takamatsu City, Kagawa • Programming language lover
  3. ©2025 Wantedly, Inc. 1. What is a continuation? 2. Continuations’

    use cases: backtracking 3. call/cc’s problem 4. Better alternative: shift/reset 5. Conclusion Agenda
  4. ©2025 Wantedly, Inc. What is a continuation? Computation that receive

    the evaluation result Or run-time point in program execution
  5. ©2025 Wantedly, Inc. Ruby’s built-in continuation operator: call/cc Call/cc captures

    current continuation Kernel.#callcc / Continuation#call behaves like setjmp / longjmp
  6. ©2025 Wantedly, Inc. Calling continuation multiple times Continuation can be

    invoked arbitrary times However, terminated fiber cannot be called Let's see how call/cc is special, using a DSL as an example
  7. ©2025 Wantedly, Inc. Implementing DSL with call/cc Ruby-level backtracking by

    call/cc Thanks to one-pass evaluation, most Ruby expressions are also viable in DSL
  8. ©2025 Wantedly, Inc. Problem of call/cc (1/2) Captures unnecessarily large

    code pieces Need call/cc calls in two different places, one for backtracking and one for conclusion
  9. ©2025 Wantedly, Inc. shift/reset: yet another continuation operators Captures delimited

    code block from shift to reset Unlike call/cc, delimited continuation will go back when reaches reset
  10. ©2025 Wantedly, Inc. Implementing “do” syntax like DSL with shift/reset

    Delimiting continuation leads to simple code Enumerable#flat_map can be used as is
  11. ©2025 Wantedly, Inc. Implementing shift/reset with call/cc shift/reset are reproducible

    by call/cc Thread-local variable is needed to record return addresses
  12. ©2025 Wantedly, Inc. Benchmarking “do” syntax like DSL with shift/reset

    Delimited continuation improves performance Kernel.#callcc’s optimization may be effective for shift/reset implementation
  13. ©2025 Wantedly, Inc. Foresight to native shift/reset implementation shift/reset may

    provide optimization hints Programmers can specify necessary stack range to the interpreter
  14. ©2025 Wantedly, Inc. 1. call/cc can manipulate continuations 2. call/cc

    is sometimes suitable for DSL 3. call/cc has some problems • Hard to use for developers • Poor performance 4. shift/reset probably resolve problems of call/cc • I'm going to introduce the new, performant API Conclusion