Slide 1

Slide 1 text

©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

Slide 2

Slide 2 text

©2025 Wantedly, Inc. .BTBZVLJ.J[VOP "CPVUNF • Backend engineer at Wantedly, Inc. • Born in Takamatsu City, Kagawa • Programming language lover

Slide 3

Slide 3 text

©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

Slide 4

Slide 4 text

©2025 Wantedly, Inc. 8IBUJTDPOUJOVBUJPO

Slide 5

Slide 5 text

©2025 Wantedly, Inc. What is a continuation? Computation that receive the evaluation result Or run-time point in program execution

Slide 6

Slide 6 text

©2025 Wantedly, Inc. Ruby’s built-in continuation operator: call/cc Call/cc captures current continuation Kernel.#callcc / Continuation#call behaves like setjmp / longjmp

Slide 7

Slide 7 text

©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

Slide 8

Slide 8 text

©2025 Wantedly, Inc. $POUJOVBUJPOT`VTFDBTFT CBDLUSBDLJOH

Slide 9

Slide 9 text

©2025 Wantedly, Inc. $BOXFXSJUFCBDLUSBDLJOHGMVFOUMZ :PVDBOEPJUJO)BTLFMM $POUJOVBUJPOT`VTFDBTFTCBDLUSBDLJOH 

Slide 10

Slide 10 text

©2025 Wantedly, Inc. /BJWFUSBOTMBUJPOMFBETUPDBMMCBDLIFMM 8BOUBNPSFSFTFNCMBODFUP)BTLFMMTPOF $POUJOVBUJPOT`VTFDBTFTCBDLUSBDLJOH 

Slide 11

Slide 11 text

©2025 Wantedly, Inc. .FUBQSPHSBNNJOHBWPJETDBMMCBDLIFMM #VJME"45GPSSFTPMWJOHWBSJBCMFTDPQF *OUSPEVDUJPOPGlEPzTZOUBYMJLF%4-

Slide 12

Slide 12 text

©2025 Wantedly, Inc. &WFSZTZOUBYOFFETSFJOWFOUJPO 5IF%4-JOUFSQSFUFSIBTUPSFQSPEVDFQSFUUZNVDIFWFSZUIJOH *TTVFPGNFUBQSPHSBNNJOHCBTFEBQQSPBDI 

Slide 13

Slide 13 text

©2025 Wantedly, Inc. *ODPNQBUJCMFWBSJBCMFTDPQF .BOVBMWBSJBCMFQBTTJOHJTSFRVJSFE *TTVFPGNFUBQSPHSBNNJOHCBTFEBQQSPBDI 

Slide 14

Slide 14 text

©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

Slide 15

Slide 15 text

©2025 Wantedly, Inc. 1SPCMFNPGDBMMDD

Slide 16

Slide 16 text

©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

Slide 17

Slide 17 text

©2025 Wantedly, Inc. 1PPSQFSGPSNBODF DBMMDDCBTFEDPEFCMPBUTVQJONFNPSZBOE$16DPOTVNQUJPO 1SPCMFNPGDBMMDD  Let's discover the cause for performance degradation

Slide 18

Slide 18 text

©2025 Wantedly, Inc. 5JHIUMZDPVQMFEXJUI7.JOUFSOBMT *NQMFNFOUBUJPOMJFTXJUIJO$3VCZ *OTJEFlDPOUJOVBUJPOzMJCSBSZ

Slide 19

Slide 19 text

©2025 Wantedly, Inc. 3FDPSETOBQTIPUPG7.TUBUF 4BWF7.TUBDLBOENBSLSVOUJNFQPJOU *NQMFNFOUBUJPOPG,FSOFMDBMMDD

Slide 20

Slide 20 text

©2025 Wantedly, Inc. %JGGFSFOUJBMTOBQTIPUPG7.TUBDL 3FVTFQSFWJPVT7.TUBDLCBDLVQJGUIFSFJTPOF ,FSOFMDBMMDD`TPQUJNJ[BUJPO

Slide 21

Slide 21 text

©2025 Wantedly, Inc. 3FTUPSFTTUBDLBOEQFSGPSNlMPOHKNQz *OTJEFPG$POUJOVBUJPODBMM 0WFSBMM DBMMDDEFWPUFTFYFDVUJPOUJNFTUPCBDLVQBOESFTUPSFPGTUBDL

Slide 22

Slide 22 text

©2025 Wantedly, Inc. #FUUFSBMUFSOBUJWFTIJGUSFTFU

Slide 23

Slide 23 text

©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

Slide 24

Slide 24 text

©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

Slide 25

Slide 25 text

©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

Slide 26

Slide 26 text

©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

Slide 27

Slide 27 text

©2025 Wantedly, Inc. Foresight to native shift/reset implementation shift/reset may provide optimization hints Programmers can specify necessary stack range to the interpreter

Slide 28

Slide 28 text

©2025 Wantedly, Inc. $PODMVTJPO

Slide 29

Slide 29 text

©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