Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Continuation is to be continued
Search
Masayuki Mizuno
April 22, 2025
1
1.1k
Continuation is to be continued
Masayuki Mizuno
April 22, 2025
Tweet
Share
More Decks by Masayuki Mizuno
See All by Masayuki Mizuno
Omotesando.rb #110
fetburner
0
87
callccの実装を読む
fetburner
0
140
RubyでSlackのbotを作ろう
fetburner
0
150
継続オペレータの話
fetburner
0
220
Rubyでリストモナドを使う
fetburner
0
170
次世代のプロファイラVernierを試す
fetburner
0
120
数値計算とメモリ最適化
fetburner
0
220
3/14なので円周率を計算する
fetburner
0
390
ネットワーク機器の中の OCaml
fetburner
1
720
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
46
14k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
We Have a Design System, Now What?
morganepeng
52
7.6k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Writing Fast Ruby
sferik
628
61k
The Cost Of JavaScript in 2023
addyosmani
49
8.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.5k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Rails Girls Zürich Keynote
gr2m
94
13k
Transcript
©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
©2025 Wantedly, Inc. .BTBZVLJ.J[VOP "CPVUNF • Backend engineer at Wantedly,
Inc. • Born in Takamatsu City, Kagawa • Programming language lover
©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
©2025 Wantedly, Inc. 8IBUJTDPOUJOVBUJPO
©2025 Wantedly, Inc. What is a continuation? Computation that receive
the evaluation result Or run-time point in program execution
©2025 Wantedly, Inc. Ruby’s built-in continuation operator: call/cc Call/cc captures
current continuation Kernel.#callcc / Continuation#call behaves like setjmp / longjmp
©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
©2025 Wantedly, Inc. $POUJOVBUJPOT`VTFDBTFT CBDLUSBDLJOH
©2025 Wantedly, Inc. $BOXFXSJUFCBDLUSBDLJOHGMVFOUMZ :PVDBOEPJUJO)BTLFMM $POUJOVBUJPOT`VTFDBTFTCBDLUSBDLJOH
©2025 Wantedly, Inc. /BJWFUSBOTMBUJPOMFBETUPDBMMCBDLIFMM 8BOUBNPSFSFTFNCMBODFUP)BTLFMMTPOF $POUJOVBUJPOT`VTFDBTFTCBDLUSBDLJOH
©2025 Wantedly, Inc. .FUBQSPHSBNNJOHBWPJETDBMMCBDLIFMM #VJME"45GPSSFTPMWJOHWBSJBCMFTDPQF *OUSPEVDUJPOPGlEPzTZOUBYMJLF%4-
©2025 Wantedly, Inc. &WFSZTZOUBYOFFETSFJOWFOUJPO 5IF%4-JOUFSQSFUFSIBTUPSFQSPEVDFQSFUUZNVDIFWFSZUIJOH *TTVFPGNFUBQSPHSBNNJOHCBTFEBQQSPBDI
©2025 Wantedly, Inc. *ODPNQBUJCMFWBSJBCMFTDPQF .BOVBMWBSJBCMFQBTTJOHJTSFRVJSFE *TTVFPGNFUBQSPHSBNNJOHCBTFEBQQSPBDI
©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
©2025 Wantedly, Inc. 1SPCMFNPGDBMMDD
©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
©2025 Wantedly, Inc. 1PPSQFSGPSNBODF DBMMDDCBTFEDPEFCMPBUTVQJONFNPSZBOE$16DPOTVNQUJPO 1SPCMFNPGDBMMDD Let's discover the
cause for performance degradation
©2025 Wantedly, Inc. 5JHIUMZDPVQMFEXJUI7.JOUFSOBMT *NQMFNFOUBUJPOMJFTXJUIJO$3VCZ *OTJEFlDPOUJOVBUJPOzMJCSBSZ
©2025 Wantedly, Inc. 3FDPSETOBQTIPUPG7.TUBUF 4BWF7.TUBDLBOENBSLSVOUJNFQPJOU *NQMFNFOUBUJPOPG,FSOFMDBMMDD
©2025 Wantedly, Inc. %JGGFSFOUJBMTOBQTIPUPG7.TUBDL 3FVTFQSFWJPVT7.TUBDLCBDLVQJGUIFSFJTPOF ,FSOFMDBMMDD`TPQUJNJ[BUJPO
©2025 Wantedly, Inc. 3FTUPSFTTUBDLBOEQFSGPSNlMPOHKNQz *OTJEFPG$POUJOVBUJPODBMM 0WFSBMM DBMMDDEFWPUFTFYFDVUJPOUJNFTUPCBDLVQBOESFTUPSFPGTUBDL
©2025 Wantedly, Inc. #FUUFSBMUFSOBUJWFTIJGUSFTFU
©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
©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
©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
©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
©2025 Wantedly, Inc. Foresight to native shift/reset implementation shift/reset may
provide optimization hints Programmers can specify necessary stack range to the interpreter
©2025 Wantedly, Inc. $PODMVTJPO
©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