ruby continuationko1/pub/continuationfest-ruby.pdf · 2008. 4. 13. · ruby. ruby continuation,...

24
RUBY CONTINUATION Koichi Sasada Department of Creative Informatics, Graduate School of Information Science and Technology, The University of Tokyo 1 Ruby Continuation, 4/13 2008, Continuation [email protected]

Upload: others

Post on 05-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

RUBY CONTINUATION

Koichi SasadaDepartment of Creative Informatics, Graduate School of

Information Science and Technology, The University of Tokyo

1Ruby Continuation, 4/13 2008, Continuation [email protected]

Page 2: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

RUBY

CONTINUATION CONSIDERED

HARMFUL

Koichi [email protected]

2Ruby Continuation, 4/13 2008, Continuation [email protected]

Page 3: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Notice

Ruby Continuation, 4/13 2008, Continuation [email protected]

3

LanguagePlace

Page 4: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Ruby

Ruby Continuation, 4/13 2008, Continuation [email protected]

4

Object Oriented Scripting LanguageMinor Language hereClass based OO-SystemEasy to write Closure (Block)Dynamic NatureFrom Japan

Designed/Implemented by Yukihiro MatsumotoHave a Continuation class

Page 5: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

FYI

Ruby Meets VM, Koichi Sasada, EURUKO 2008

5

Ruby 2.0 – since 2003 3/31Not released

Perl 6 – since 2003 4/1Not released

Page 6: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Ruby History

Ruby Continuation, 4/13 2008, Continuation [email protected]

6

1993 2/24 Named “Ruby”1995 Released via NetNews1999 First Ruby book in Japanese2001 First Ruby book in English2004? Ruby on Rails2007 12/25 Ruby 1.9.0-0 Released

Page 7: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

YARV: Yet Another Ruby VMHistory

Ruby Meets VM, Koichi Sasada, EURUKO 2008

7

4 Years1, Jan 2004 Project Start2004-2005 VM Core, Optimization

Supported by MITO youth Project (IPA)2005-2006 Thread, etc

Supported by MITO Project (IPA)1, Apr 2006 Got a Job (Assistant on U-Tokyo)

2006-2007 etc, etcSupported by MITO Project (IPA)25, Dec 2007 Got a Ph.DYARV is merged into Ruby Official Repository

25, Dec 2007 (GMT) 1.9 Release

Page 8: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Ruby Development History

Ruby Continuation, 4/13 2008, Continuation [email protected]

8

Ruby Development History≠ Battle with Continuation bugs

( + other bugs )

Page 9: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Ruby Continuation

Ruby Continuation, 4/13 2008, Continuation [email protected]

9

Captured with “callcc” methodFull-Continuation

# Sample Codecallcc{|cont|$cont = cont # “$cont” is global variable# …

}$cont.call # call continuation

Page 10: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Ruby ContinuationSituation

Ruby Continuation, 4/13 2008, Continuation [email protected]

10

To Achieve “Generator”Rubyʼs loop primitives are “inner-iterator”

Call Closure per iterationE.g. same as Pythonʼs “generator”

Make a toy program (ex: amb)To be proud of owning “callcc” method

Page 11: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Implementation of Ruby Threadon Ruby 1.8

Ruby Continuation, 4/13 2008, Continuation [email protected]

11

Userlevel Thread modelCopy all machine stack and swap all Interpreter contextsI/O check on thread scheduling

Page 12: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

User-level Thread Model12

Ruby (YARV)

Native ThreadSystem S/W

Processor(s)PE PE PE・・・

H/W

RT RT RT

NT

Thread SchedulerS/W

PE: Processor Element, UL: User Level, KL: Kernel Level

・・・

・・・

Page 13: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Context Switch on Ruby 1.8

Ruby Continuation, 4/13 2008, Continuation [email protected]

13

MachineStack

MethodContexts

MachineStack

MethodContexts

Store Restore

Thread A Context Thread B Context

Thread Scheduling

Page 14: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Implementation of Ruby Continuation on Ruby 1.8

Ruby Continuation, 4/13 2008, Continuation [email protected]

14

Almost same as (1.8 Userlevel) Thread (== wrapper of Thread)Shugo Maeda (criminal) proposed that “Thread implementation can be used to make Continuation”Matz agreed this proposal (2nd criminal)

Page 15: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Problems of Ruby Continuation

Ruby Continuation, 4/13 2008, Continuation [email protected]

15

Inconsistency on C Functionʼs Context and Rubyʼs Context

Save all “Machine Stack”Produce Many Many BUG reports

[SEE blade]NO “dynamic-wind” feature

Canʼt make safety program with Continuation

Page 16: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Implementation of Ruby Threadon Ruby 1.9

Ruby Continuation, 4/13 2008, Continuation [email protected]

16

Using Native ThreadCanʼt Run in Parallel

With Giant VM LockCRuby has many “Thread-unsafe” C functions

Page 17: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

1:1 Thread Model17

Ruby (YARV)

Native ThreadSystem S/W

Processor(s)PE PE PE・・・

H/W

RT RT RT

NT NT NT

Thread SchedulerS/W

PE: Processor Element, UL: User Level, KL: Kernel Level

・・・

・・・

Page 18: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

18

Depth of Ruby Stack

Execution Time (sec)

FYI: Context Switch Performance

Page 19: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Implementation of Continuationon Ruby 1.9

Ruby Continuation, 4/13 2008, Continuation [email protected]

19

Make 1.8 Userlevel Thread without Thread Scheduler

Page 20: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Solution against Continuation issueson Ruby 1.9

Ruby Continuation, 4/13 2008, Continuation [email protected]

20

To use “callcc” method, need a libraryrequire ʻcontinuationʼcallcc{|…| …}Require library is only enable “callcc” methodAt last, I want to kick out to gem

Support “Fiber”Fiber == CoroutineFAQ: Why Fiber? → It sounds goodGenerator can be impl. with Fiber

Page 21: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Other Ideas

Ruby Continuation, 4/13 2008, Continuation [email protected]

21

Name long method name or library namecall_with_current_continuation{|…| …}require ʻcall_with_current_continuation_featureʼ

Page 22: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Other Ideas (cont.)

Ruby Continuation, 4/13 2008, Continuation [email protected]

22

Add a Agreement sentence to method name or library name (Same as “long name” solution)

call_with_current_continuation__if_you_call_this_method_this_interpreter__we_cant_guarantee_normal_execution{|…| …}requireʻcall_with_current_continuation_feature_if_you_call_this_method_this_interpreter__we_cant_guarantee_normal_executionʼ

Page 23: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Summary

Ruby Continuation, 4/13 2008, Continuation [email protected]

23

Rubyʼs Full-Continuation Considered Harmful

Produce many many bugsNobody use “callcc” method except experimental code

Ruby 1.9 or later solves this issue with kick out “callcc” feature to Library

Ruby 2.0 includes this library?

Page 24: RUBY CONTINUATIONko1/pub/ContinuationFest-ruby.pdf · 2008. 4. 13. · Ruby. Ruby Continuation, 4/13 2008, Continuation Fest@u-tokyo.ac.jp. 4 Object Oriented Scripting Language Minor

Ruby Continuation, 4/13 2008, Continuation [email protected]

24

Thank you for your attention!Any Questions?

ささだ こういちKoichi [email protected]