SlideShare a Scribd company logo
1 of 20
Download to read offline
CPS & TCO
2016/05/19
0x64 Tales
#08 Func6onal Programming
Livesense Inc.
HORINOUCHI Masato
Con$nua$on-passing style とは
継続渡しスタイル (CPS: Con)nua)on-passing style) とは、プログラ
ムの制御を継続を用いて陽に表すプログラミングスタイルのことで
ある。
継続渡しスタイルで書かれた関数は、通常の直接スタイル (direct
style) のように値を「返す」かわりに、「継続」を引数として陽に受
け取り、その継続に計算結果を渡す。継続とは、関数の計算結果を
受け取るための(一般には元の関数とは別の)関数のことである。
継続渡しスタイル - Wikipedia から引用
factorial (Scheme / direct style)
(define (fact n)
(if (zero? n)
1
(* n (fact (- n 1)))))
(fact 10)
factorial (Scheme / CPS)
(define (fact/cps n cont) # ← 引数 cont が継続
(if (zero? n)
(cont 1)
(fact/cps (- n 1) (lambda (x) (cont (* n x))))))
(fact/cps 10 (lambda (x) x))
factorial (Scheme / direct style / tail recursion)
(define (fact_tail n acc)
(if (zero? n)
acc
(fact_tail (- n 1) (* n acc))))
(fact_tail 10 1)
benchmark result (1,0003mes 1000!)
• non tail call
• 0.767s
• cps
• 0.854s
• tail call
• 0.819s
• まさかの非末尾呼出版が最も速い。
• CPS は呼び出し毎に lambda object 生成するから遅いのかな。
Stack Overflow (Scheme)
• 各パターンとも 100000! でも Stack Overflow しない。
• Scheme は末尾呼出最適化を行なうので CPS版と末尾呼出版が
overflow しないのはわかる。
• 非末尾呼出版でも overflow しないのは、CPS変換を自動的に行
なっているのだろうか?
factorial (Ruby / direct style / recursion)
def fact(n)
if n == 0
1
else
n * fact(n - 1)
end
end
fact(10)
factorial (Ruby / CPS)
def fact_cps(n, cont)
if n == 0
cont.call 1
else
fact_cps(n - 1, -> (x) { cont.call(n * x) })
end
end
fact_cps(10, -> (x) { x })
factorial (Ruby / direct style / tail recursion)
def fact_tail(n, acc = 1)
if n == 0
acc
else
fact_tail(n - 1, n * acc)
end
end
fact_tail(10)
benchmark
TIMES = 1000
FACT = 1000
Benchmark.bm 16 do |r|
r.report 'non tail call' do
TIMES.times do
fact(FACT)
end
end
r.report 'cps' do
TIMES.times do
fact_cps(FACT, -> (x) { x })
end
end
r.report 'tail call' do
TIMES.times do
fact_tail(FACT)
end
end
end
benchmark result (1,0003mes 1000!)
user system total real
non tail call 0.570000 0.010000 0.580000 ( 0.575446)
cps 1.220000 0.060000 1.280000 ( 1.280935)
tail call 0.650000 0.060000 0.710000 ( 0.705097)
• Scheme版と同様の傾向になった。
Stack Overflow (Ruby)
• 5000!
• 3パターンとも問題なし
• 10000!
• CPS版が stack level too deep
• 11000!
• 3パターンとも stack level too deep
Tail Call Op)miza)on とは
末尾呼出し最適化とは、末尾呼出しのコードを、戻り先を保存しないジャン
プに変換することによって、スタックの累積を無くし、効率の向上などを図
る手法である。
理論的には、継続渡しによる goto と同等のジャンプ処理では、手続きの呼出
し元の情報を保存する必要がないことに帰着する。この場合 return は goto
の特殊なケースで、ジャンプ先が呼出し元に等しいケースである。末尾最適化
があれば、手続きの再帰を行なう時でも、結果はループと等価な処理手順と
なり、どれほど深い再帰を行なってもスタックオーバーフローを起こさない。
末尾再帰 - Wikipedia から引用
Tail Call Op)miza)on in Ruby
• YARV だとオプションで TCO 有効にできる。
RubyVM::InstructionSequence.compile_option = {
tailcall_optimization: true,
trace_instruction: false
}
benchmark result (1,0003mes 1000! / TCO)
user system total real
non tail call 0.570000 0.010000 0.580000 ( 0.575446)
cps 1.220000 0.060000 1.280000 ( 1.280935)
tail call 0.650000 0.060000 0.710000 ( 0.705097)
↓ TCO on
user system total real
non tail call 0.560000 0.020000 0.580000 ( 0.578095)
cps 1.130000 0.050000 1.180000 ( 1.183501)
tail call 0.640000 0.040000 0.680000 ( 0.688400)
• CPS で約 8%改善、末尾呼出で 4%改善
Stack Overflow (Ruby / TCO)
• 10000!
• 3パターンとも問題なし
• 11000!
• 非末尾呼出版が stack level too deep
• 12000!
• CPS版が stack level too deep ← なぜ?
• 1000000! まで試したが、末尾呼出版では Stack Overflow しない (10分かかった)
TCO Problems
• (1) backtrace: Elimina3ng method frame means elimina3ng
backtrace.
• (2) settracefunc(): It is difficult to probe "return" event for tail-call
methods.
• (3) seman3cs: It is difficult to define tail-call in document (half is
joking, but half is serious)
Tail call op)miza)on: enable by default? から引用
まとめ
• CPS に置き換えるだけでは速くならない。
• YARV だと TCO 有効化ができる。
• TCO に関係なく 1.upto(FACT).inject(:*) だと Stack
Overflow しないのはヒミツ。
ご清聴ありがとうございました

More Related Content

What's hot

2011年12月9日
2011年12月9日2011年12月9日
2011年12月9日nukaemon
 
Rubyの御先祖CLUのお話(原本)
Rubyの御先祖CLUのお話(原本)Rubyの御先祖CLUのお話(原本)
Rubyの御先祖CLUのお話(原本)洋史 東平
 
Python vs ruby
Python vs rubyPython vs ruby
Python vs rubyosamunmun
 
C# linq入門 意図編
C# linq入門 意図編C# linq入門 意図編
C# linq入門 意図編Fujio Kojima
 
Delimited Dynamic Binding
Delimited Dynamic BindingDelimited Dynamic Binding
Delimited Dynamic BindingYouyou Cong
 
V6でJIT・部分適用・継続
V6でJIT・部分適用・継続V6でJIT・部分適用・継続
V6でJIT・部分適用・継続7shi
 
20150928楽しいlambda
20150928楽しいlambda20150928楽しいlambda
20150928楽しいlambdaNorifumi Homma
 
JS 6th edition reading circle part 3
JS 6th edition reading circle part 3JS 6th edition reading circle part 3
JS 6th edition reading circle part 3Teloo
 
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swift
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swiftみんなで Swift 復習会での談笑用スライド – 4th #minna_de_swift
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swiftTomohiro Kumagai
 
Rubyの御先祖CLUのお話(OSC 2011 Shimane LT 資料)
Rubyの御先祖CLUのお話(OSC 2011 Shimane LT 資料)Rubyの御先祖CLUのお話(OSC 2011 Shimane LT 資料)
Rubyの御先祖CLUのお話(OSC 2011 Shimane LT 資料)洋史 東平
 
Whole Program Paths 等の紹介@PLDIr#3
Whole Program Paths 等の紹介@PLDIr#3Whole Program Paths 等の紹介@PLDIr#3
Whole Program Paths 等の紹介@PLDIr#3Masahiro Sakai
 
代数的データ型をラムダ計算の中で表現する方法
代数的データ型をラムダ計算の中で表現する方法代数的データ型をラムダ計算の中で表現する方法
代数的データ型をラムダ計算の中で表現する方法syamino
 
kagamicomput201707
kagamicomput201707kagamicomput201707
kagamicomput201707swkagami
 
2011年11月18日
2011年11月18日2011年11月18日
2011年11月18日nukaemon
 
サイ本読書会4章変数
サイ本読書会4章変数サイ本読書会4章変数
サイ本読書会4章変数ztyper
 

What's hot (16)

2011年12月9日
2011年12月9日2011年12月9日
2011年12月9日
 
Rubyの御先祖CLUのお話(原本)
Rubyの御先祖CLUのお話(原本)Rubyの御先祖CLUのお話(原本)
Rubyの御先祖CLUのお話(原本)
 
Python vs ruby
Python vs rubyPython vs ruby
Python vs ruby
 
C# linq入門 意図編
C# linq入門 意図編C# linq入門 意図編
C# linq入門 意図編
 
Delimited Dynamic Binding
Delimited Dynamic BindingDelimited Dynamic Binding
Delimited Dynamic Binding
 
P5utda day3
P5utda day3P5utda day3
P5utda day3
 
V6でJIT・部分適用・継続
V6でJIT・部分適用・継続V6でJIT・部分適用・継続
V6でJIT・部分適用・継続
 
20150928楽しいlambda
20150928楽しいlambda20150928楽しいlambda
20150928楽しいlambda
 
JS 6th edition reading circle part 3
JS 6th edition reading circle part 3JS 6th edition reading circle part 3
JS 6th edition reading circle part 3
 
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swift
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swiftみんなで Swift 復習会での談笑用スライド – 4th #minna_de_swift
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swift
 
Rubyの御先祖CLUのお話(OSC 2011 Shimane LT 資料)
Rubyの御先祖CLUのお話(OSC 2011 Shimane LT 資料)Rubyの御先祖CLUのお話(OSC 2011 Shimane LT 資料)
Rubyの御先祖CLUのお話(OSC 2011 Shimane LT 資料)
 
Whole Program Paths 等の紹介@PLDIr#3
Whole Program Paths 等の紹介@PLDIr#3Whole Program Paths 等の紹介@PLDIr#3
Whole Program Paths 等の紹介@PLDIr#3
 
代数的データ型をラムダ計算の中で表現する方法
代数的データ型をラムダ計算の中で表現する方法代数的データ型をラムダ計算の中で表現する方法
代数的データ型をラムダ計算の中で表現する方法
 
kagamicomput201707
kagamicomput201707kagamicomput201707
kagamicomput201707
 
2011年11月18日
2011年11月18日2011年11月18日
2011年11月18日
 
サイ本読書会4章変数
サイ本読書会4章変数サイ本読書会4章変数
サイ本読書会4章変数
 

Viewers also liked

¿Qué flores son apropiadas según la ocasión?
¿Qué flores son apropiadas según la ocasión?¿Qué flores son apropiadas según la ocasión?
¿Qué flores son apropiadas según la ocasión?Laura Lima
 
17th July 2016 - What is Praye...
17th July 2016 - What is Praye...17th July 2016 - What is Praye...
17th July 2016 - What is Praye...Thorn Group Pvt Ltd
 
Class 2 in class essay
Class 2 in class essayClass 2 in class essay
Class 2 in class essayjordanlachance
 
L'interet de comparer des devis avant de créer son site internet
L'interet de comparer des devis avant de créer son site internetL'interet de comparer des devis avant de créer son site internet
L'interet de comparer des devis avant de créer son site internetOuelid Sassi
 
Guangzhou yiyun clothing co.,ltd
Guangzhou yiyun clothing co.,ltdGuangzhou yiyun clothing co.,ltd
Guangzhou yiyun clothing co.,ltdIvan Lin
 
1 a 15 concept essay workshop
1 a 15 concept essay workshop 1 a 15 concept essay workshop
1 a 15 concept essay workshop kimpalmore
 

Viewers also liked (11)

¿Qué flores son apropiadas según la ocasión?
¿Qué flores son apropiadas según la ocasión?¿Qué flores son apropiadas según la ocasión?
¿Qué flores son apropiadas según la ocasión?
 
20160122142226271
2016012214222627120160122142226271
20160122142226271
 
17th July 2016 - What is Praye...
17th July 2016 - What is Praye...17th July 2016 - What is Praye...
17th July 2016 - What is Praye...
 
Class 2 in class essay
Class 2 in class essayClass 2 in class essay
Class 2 in class essay
 
L'interet de comparer des devis avant de créer son site internet
L'interet de comparer des devis avant de créer son site internetL'interet de comparer des devis avant de créer son site internet
L'interet de comparer des devis avant de créer son site internet
 
Guangzhou yiyun clothing co.,ltd
Guangzhou yiyun clothing co.,ltdGuangzhou yiyun clothing co.,ltd
Guangzhou yiyun clothing co.,ltd
 
La Filiación
La FiliaciónLa Filiación
La Filiación
 
Psp%20%28 personal%20software%20process%29
Psp%20%28 personal%20software%20process%29Psp%20%28 personal%20software%20process%29
Psp%20%28 personal%20software%20process%29
 
P630i p640i Zebra
P630i p640i ZebraP630i p640i Zebra
P630i p640i Zebra
 
1 a 15 concept essay workshop
1 a 15 concept essay workshop 1 a 15 concept essay workshop
1 a 15 concept essay workshop
 
La Juste Dose. Journée Réseau VRAC #3
La Juste Dose. Journée Réseau VRAC #3La Juste Dose. Journée Réseau VRAC #3
La Juste Dose. Journée Réseau VRAC #3
 

Similar to CPS & CTO

lispmeetup#63 Common Lispでゼロから作るDeep Learning
lispmeetup#63 Common Lispでゼロから作るDeep Learninglispmeetup#63 Common Lispでゼロから作るDeep Learning
lispmeetup#63 Common Lispでゼロから作るDeep LearningSatoshi imai
 
ji-5. 繰り返し計算
ji-5. 繰り返し計算ji-5. 繰り返し計算
ji-5. 繰り返し計算kunihikokaneko1
 
Javaセキュアコーディングセミナー東京第2回講義
Javaセキュアコーディングセミナー東京第2回講義Javaセキュアコーディングセミナー東京第2回講義
Javaセキュアコーディングセミナー東京第2回講義JPCERT Coordination Center
 
関数型言語&形式的手法セミナー(3)
関数型言語&形式的手法セミナー(3)関数型言語&形式的手法セミナー(3)
関数型言語&形式的手法セミナー(3)啓 小笠原
 
Introduction to NumPy & SciPy
Introduction to NumPy & SciPyIntroduction to NumPy & SciPy
Introduction to NumPy & SciPyShiqiao Du
 
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編Yosuke Onoue
 
言語処理系入門€10
言語処理系入門€10言語処理系入門€10
言語処理系入門€10Kenta Hattori
 
T69 c++cli ネイティブライブラリラッピング入門
T69 c++cli ネイティブライブラリラッピング入門T69 c++cli ネイティブライブラリラッピング入門
T69 c++cli ネイティブライブラリラッピング入門伸男 伊藤
 
OpenCVの拡張ユーティリティ関数群
OpenCVの拡張ユーティリティ関数群OpenCVの拡張ユーティリティ関数群
OpenCVの拡張ユーティリティ関数群Norishige Fukushima
 
ディープラーニングフレームワーク とChainerの実装
ディープラーニングフレームワーク とChainerの実装ディープラーニングフレームワーク とChainerの実装
ディープラーニングフレームワーク とChainerの実装Ryosuke Okuta
 
あなたのScalaを爆速にする7つの方法(日本語版)
あなたのScalaを爆速にする7つの方法(日本語版)あなたのScalaを爆速にする7つの方法(日本語版)
あなたのScalaを爆速にする7つの方法(日本語版)x1 ichi
 
OpenFOAMの混相流用改造solver(S-CLSVOF法)の設定・使い方
OpenFOAMの混相流用改造solver(S-CLSVOF法)の設定・使い方OpenFOAMの混相流用改造solver(S-CLSVOF法)の設定・使い方
OpenFOAMの混相流用改造solver(S-CLSVOF法)の設定・使い方takuyayamamoto1800
 

Similar to CPS & CTO (20)

Coqでsprintf
CoqでsprintfCoqでsprintf
Coqでsprintf
 
Coqでsprintf
CoqでsprintfCoqでsprintf
Coqでsprintf
 
Pfi Seminar 2010 1 7
Pfi Seminar 2010 1 7Pfi Seminar 2010 1 7
Pfi Seminar 2010 1 7
 
lispmeetup#63 Common Lispでゼロから作るDeep Learning
lispmeetup#63 Common Lispでゼロから作るDeep Learninglispmeetup#63 Common Lispでゼロから作るDeep Learning
lispmeetup#63 Common Lispでゼロから作るDeep Learning
 
ji-5. 繰り返し計算
ji-5. 繰り返し計算ji-5. 繰り返し計算
ji-5. 繰り返し計算
 
Javaセキュアコーディングセミナー東京第2回講義
Javaセキュアコーディングセミナー東京第2回講義Javaセキュアコーディングセミナー東京第2回講義
Javaセキュアコーディングセミナー東京第2回講義
 
関数型言語&形式的手法セミナー(3)
関数型言語&形式的手法セミナー(3)関数型言語&形式的手法セミナー(3)
関数型言語&形式的手法セミナー(3)
 
Introduction to NumPy & SciPy
Introduction to NumPy & SciPyIntroduction to NumPy & SciPy
Introduction to NumPy & SciPy
 
C言語講習会3
C言語講習会3C言語講習会3
C言語講習会3
 
Scheme to x86コンパイラ
Scheme to x86コンパイラScheme to x86コンパイラ
Scheme to x86コンパイラ
 
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
 
Rの高速化
Rの高速化Rの高速化
Rの高速化
 
言語処理系入門€10
言語処理系入門€10言語処理系入門€10
言語処理系入門€10
 
R-hpc-1 TokyoR#11
R-hpc-1 TokyoR#11R-hpc-1 TokyoR#11
R-hpc-1 TokyoR#11
 
T69 c++cli ネイティブライブラリラッピング入門
T69 c++cli ネイティブライブラリラッピング入門T69 c++cli ネイティブライブラリラッピング入門
T69 c++cli ネイティブライブラリラッピング入門
 
OpenCVの拡張ユーティリティ関数群
OpenCVの拡張ユーティリティ関数群OpenCVの拡張ユーティリティ関数群
OpenCVの拡張ユーティリティ関数群
 
R intro
R introR intro
R intro
 
ディープラーニングフレームワーク とChainerの実装
ディープラーニングフレームワーク とChainerの実装ディープラーニングフレームワーク とChainerの実装
ディープラーニングフレームワーク とChainerの実装
 
あなたのScalaを爆速にする7つの方法(日本語版)
あなたのScalaを爆速にする7つの方法(日本語版)あなたのScalaを爆速にする7つの方法(日本語版)
あなたのScalaを爆速にする7つの方法(日本語版)
 
OpenFOAMの混相流用改造solver(S-CLSVOF法)の設定・使い方
OpenFOAMの混相流用改造solver(S-CLSVOF法)の設定・使い方OpenFOAMの混相流用改造solver(S-CLSVOF法)の設定・使い方
OpenFOAMの混相流用改造solver(S-CLSVOF法)の設定・使い方
 

More from Masato HORINOUCHI (9)

Church Numerals
Church NumeralsChurch Numerals
Church Numerals
 
FM synthesis
FM synthesisFM synthesis
FM synthesis
 
Inside mml2wav.rb
Inside mml2wav.rbInside mml2wav.rb
Inside mml2wav.rb
 
A440
A440A440
A440
 
Scheme Interpreter in Ruby
Scheme Interpreter in RubyScheme Interpreter in Ruby
Scheme Interpreter in Ruby
 
Clock / Timer
Clock / TimerClock / Timer
Clock / Timer
 
Hash Tree
Hash TreeHash Tree
Hash Tree
 
POSIX Threads
POSIX ThreadsPOSIX Threads
POSIX Threads
 
Elixir紹介
Elixir紹介Elixir紹介
Elixir紹介
 

Recently uploaded

論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)UEHARA, Tetsutaro
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案sugiuralab
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfFumieNakayama
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfFumieNakayama
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...博三 太田
 

Recently uploaded (12)

論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
 

CPS & CTO

  • 1. CPS & TCO 2016/05/19 0x64 Tales #08 Func6onal Programming Livesense Inc. HORINOUCHI Masato
  • 2. Con$nua$on-passing style とは 継続渡しスタイル (CPS: Con)nua)on-passing style) とは、プログラ ムの制御を継続を用いて陽に表すプログラミングスタイルのことで ある。 継続渡しスタイルで書かれた関数は、通常の直接スタイル (direct style) のように値を「返す」かわりに、「継続」を引数として陽に受 け取り、その継続に計算結果を渡す。継続とは、関数の計算結果を 受け取るための(一般には元の関数とは別の)関数のことである。 継続渡しスタイル - Wikipedia から引用
  • 3. factorial (Scheme / direct style) (define (fact n) (if (zero? n) 1 (* n (fact (- n 1))))) (fact 10)
  • 4. factorial (Scheme / CPS) (define (fact/cps n cont) # ← 引数 cont が継続 (if (zero? n) (cont 1) (fact/cps (- n 1) (lambda (x) (cont (* n x)))))) (fact/cps 10 (lambda (x) x))
  • 5. factorial (Scheme / direct style / tail recursion) (define (fact_tail n acc) (if (zero? n) acc (fact_tail (- n 1) (* n acc)))) (fact_tail 10 1)
  • 6. benchmark result (1,0003mes 1000!) • non tail call • 0.767s • cps • 0.854s • tail call • 0.819s • まさかの非末尾呼出版が最も速い。 • CPS は呼び出し毎に lambda object 生成するから遅いのかな。
  • 7. Stack Overflow (Scheme) • 各パターンとも 100000! でも Stack Overflow しない。 • Scheme は末尾呼出最適化を行なうので CPS版と末尾呼出版が overflow しないのはわかる。 • 非末尾呼出版でも overflow しないのは、CPS変換を自動的に行 なっているのだろうか?
  • 8. factorial (Ruby / direct style / recursion) def fact(n) if n == 0 1 else n * fact(n - 1) end end fact(10)
  • 9. factorial (Ruby / CPS) def fact_cps(n, cont) if n == 0 cont.call 1 else fact_cps(n - 1, -> (x) { cont.call(n * x) }) end end fact_cps(10, -> (x) { x })
  • 10. factorial (Ruby / direct style / tail recursion) def fact_tail(n, acc = 1) if n == 0 acc else fact_tail(n - 1, n * acc) end end fact_tail(10)
  • 11. benchmark TIMES = 1000 FACT = 1000 Benchmark.bm 16 do |r| r.report 'non tail call' do TIMES.times do fact(FACT) end end r.report 'cps' do TIMES.times do fact_cps(FACT, -> (x) { x }) end end r.report 'tail call' do TIMES.times do fact_tail(FACT) end end end
  • 12. benchmark result (1,0003mes 1000!) user system total real non tail call 0.570000 0.010000 0.580000 ( 0.575446) cps 1.220000 0.060000 1.280000 ( 1.280935) tail call 0.650000 0.060000 0.710000 ( 0.705097) • Scheme版と同様の傾向になった。
  • 13. Stack Overflow (Ruby) • 5000! • 3パターンとも問題なし • 10000! • CPS版が stack level too deep • 11000! • 3パターンとも stack level too deep
  • 14. Tail Call Op)miza)on とは 末尾呼出し最適化とは、末尾呼出しのコードを、戻り先を保存しないジャン プに変換することによって、スタックの累積を無くし、効率の向上などを図 る手法である。 理論的には、継続渡しによる goto と同等のジャンプ処理では、手続きの呼出 し元の情報を保存する必要がないことに帰着する。この場合 return は goto の特殊なケースで、ジャンプ先が呼出し元に等しいケースである。末尾最適化 があれば、手続きの再帰を行なう時でも、結果はループと等価な処理手順と なり、どれほど深い再帰を行なってもスタックオーバーフローを起こさない。 末尾再帰 - Wikipedia から引用
  • 15. Tail Call Op)miza)on in Ruby • YARV だとオプションで TCO 有効にできる。 RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, trace_instruction: false }
  • 16. benchmark result (1,0003mes 1000! / TCO) user system total real non tail call 0.570000 0.010000 0.580000 ( 0.575446) cps 1.220000 0.060000 1.280000 ( 1.280935) tail call 0.650000 0.060000 0.710000 ( 0.705097) ↓ TCO on user system total real non tail call 0.560000 0.020000 0.580000 ( 0.578095) cps 1.130000 0.050000 1.180000 ( 1.183501) tail call 0.640000 0.040000 0.680000 ( 0.688400) • CPS で約 8%改善、末尾呼出で 4%改善
  • 17. Stack Overflow (Ruby / TCO) • 10000! • 3パターンとも問題なし • 11000! • 非末尾呼出版が stack level too deep • 12000! • CPS版が stack level too deep ← なぜ? • 1000000! まで試したが、末尾呼出版では Stack Overflow しない (10分かかった)
  • 18. TCO Problems • (1) backtrace: Elimina3ng method frame means elimina3ng backtrace. • (2) settracefunc(): It is difficult to probe "return" event for tail-call methods. • (3) seman3cs: It is difficult to define tail-call in document (half is joking, but half is serious) Tail call op)miza)on: enable by default? から引用
  • 19. まとめ • CPS に置き換えるだけでは速くならない。 • YARV だと TCO 有効化ができる。 • TCO に関係なく 1.upto(FACT).inject(:*) だと Stack Overflow しないのはヒミツ。