82
Growing the Ruby Interpreter Koichi Sasada <[email protected]> Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Growingthe Ruby Interpreter

Koichi Sasada<[email protected]>

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 2: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Today’s talk

•Ruby 2.1 and Ruby 2.2

•How to grow up the Ruby interpreter?• Evaluator• Threading• Object management / Garbage collection

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 3: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Koichi Sasada as a Japanese

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

12 hours

15 hours [HOME]8/25 5:00am (BRT)

[HOTEL]8/26 7:30pm (BRT)

4 hourstransit

[TOTAL]38.5 hours

Page 4: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Koichi Sasada as a Programmer

•CRuby/MRI committer• Core components developer

• Virtual machine (YARV) since 2004/1/1

• Rewrote Threads, GC, and so on

•Matz team at Heroku, Inc.

• Director of Ruby Association

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 5: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

• Foundation to encourage Ruby dev. and communities

• Activities• Ruby programmer certification program

• http://www.ruby.or.jp/en/certification/examination/ in English

• Grant project. Submit your proposal now!• 3 projects. About 5,000 USD per project (deadline: 3rd ,Oct)

• http://www.ruby.or.jp/en/news/20140805.html

• Ruby Prize

• Maintenance of Ruby (Cruby) interpreter

• Events, especially RubyWorld Conference

• Donation for Ruby developments and communities

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Advertisement

Page 6: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Heroku, Inc. http://www.heroku.com

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Advertisement

Page 7: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Heroku, Inc. http://www.heroku.com

Ask Nando Vieira for more details

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Advertisement

Page 8: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

• Heroku supports OSSs• Many talents for Ruby, and also other languages

• Heroku employs 3 Ruby interpreter core developers• Matz

• Nobu

• Ko1 (me)

• We name our group “Matz team”

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 9: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

MatzFamous title collector• He has so many (job) title

• Chairman - Ruby Association• Fellow - NaCl• Chief architect, Ruby - Heroku• Research institute fellow – Rakuten• Chairman – NPO mruby Forum• Senior researcher – Kadokawa Ascii Research Lab• Visiting professor – Shimane University• Honorable citizen (living) – Matsue city• Honorable member – Nihon Ruby no Kai• …

• This margin is too narrow to contain

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 10: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

NobuPatch monster

• Great patch creator

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 11: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Nobu isGreat Patch Monster

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

nobu29%

akr12%

svn9%

naruse8%

usa4%

ko14%

drbrain3%

kosaki3%

kazu2%

zzak2%

tenderlove2%

matz2%

marcandre2%

mame2%

tadf2%

knu1%

shugo1%

nagachika1%

yugui1%

kou1%

mrkn1%

emboss1%

shyouhei1%

nari0%

glass0%

ktsj0%

nahi0%

ayumin0%

tarui0%

sorah0%

ryan0%

charliesome0%

shirosaki0%

xibbar0%

nagai0%

eregon0%

ngoto0%

wanabe0%azav0%

keiju0%suke0%

kouji0%

duerst0%

takano320%

luislavena0%jeg20%hsbt0%

arton0%seki0%

kanemoto0%

tmm10%

eban0%

muraken0%

headius0%

evan0%

a_matsuda0%

iwamatsu0%

technorama0%

davidflanagan0%

gotoken0%

okkez0%

COMMIT RATIO IN LAST 5 YEARS

Page 12: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

0

5

10

15

20

25

20

10

/11

/8

20

11

/1/8

20

11

/3/8

20

11

/5/8

20

11

/7/8

20

11

/9/8

20

11

/11

/8

20

12

/1/8

20

12

/3/8

20

12

/5/8

20

12

/7/8

20

12

/9/8

20

12

/11

/8

20

13

/1/8

20

13

/3/8

20

13

/5/8

20

13

/7/8

20

13

/9/8

20

13

/11

/8

Commit number of ko1 (last 3 years)

Ko1EDD developer

RubyConf2012

RubyKaigi2013

Ruby 2.0

Euruko2013

RubyConf2013

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

EDD: Event Driven Development

Page 13: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

“Mission of Matz team”

Improve quality of next version of CRuby

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 14: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

“Mission of Matz team”

• Improve quality of next version of CRuby• Matz decides a spec finally

• Nobu fixed huge number of bugs

• Ko1 improves the performance

• Next version of CRuby is “Ruby 2.2.0”

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 15: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby 2.1Current stable

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

http://www.flickr.com/photos/loginesta/5266114104

Page 16: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby 2.1a bit old Ruby

• Ruby 2.1.0 was released at 2013/12/25• New features

• Performance improvements

• Ruby 2.1.1 was released at 2014/02/24• Includes many bug fixes found after 2.1.0 release

• Introduce a new GC tuning parameter to change generational GC behavior (introduce it later)

• Ruby 2.1.2 was released at 2014/05/09• Solves critical bugs (OpenSSL and so on)

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 17: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby 2.1 New syntax

•New syntaxes• Required keyword

parameter• Rational number literal• Complex number literal• `def’ returns symbol of

method name

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

http://www.flickr.com/photos/rooreynolds/4133549889

Page 18: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby 2.1 Runtime new features

• String#scrub

• Process.clock_gettime

• Binding#local_variable_get/set

• Bignum now uses GMP (if available)

• Extending ObjectSpace

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 19: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Performance improvements

• Optimize “string literal”.freeze

• Sophisticated inline method cache

• Introducing Generational GC: RGenGC

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 20: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

0

2

4

6

8

10

12

14

Total mark time (ms) Total sweep time (sec)

Acc

um

ula

ted

exe

cuti

on

tim

e (s

ec)

w/o RGenGC RGenGC

RGenGCPerformance evaluation (RDoc)

About x15 speedup!

* Disabled lazy sweep to measure correctly.Growing the Ruby interpreter, Koichi Sasada,

RubyConf Brasil 2014

Page 21: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby 2.2Next version

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

http://www.flickr.com/photos/adafruit/8483990604

Page 22: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby 2.2 schedule

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

2014/12/25Ruby 2.2.0

RubyKaigi9/18, 19, 20

RubyConf11/17, 18, 19

We are here!2013/12

Ruby 2.1.0

Events are important forEDD (Event Driven Development) Developers

RubyConfBrasil

8/28, 29

Page 23: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby 2.2 (rough) schedule

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

2014/12/25Ruby 2.2.0

2013/12Ruby 2.1.0

Sep/2014Preview 1

Big feature freeze

Nov/2014Preview2

Feature freeze

Dec/2014Release

candidate

Bug fix only

Critical Bug fix only

We are here!

Sep/2014Dev. MeetingFeature proposal

Page 24: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

2.2 big features (planned)

• New syntax: not available now

• New method: no notable methods available now

• Libraries:• Minitest and test/unit will be removed (provided by

bundled gem)

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 25: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

2.2 internal changes

• Internal• C APIs

• Hide internal structures for Hash, Struct and so on

• Remove obsolete APIs

• GC• Symbol GC (merged recently)

• More ages strategy to reduce too-fast-promotion

• Incremental GC to reduce major GC pause time

• VM• More sophisticated method cache

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 26: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby 2.2 internalsSymbol GC

1_000_000.times{|i| i.to_s.to_sym}

p Symbol.all_symbols.size

# Ruby 2.1

#=> 1,002,376

# Ruby 2.2 (dev)

#=> 25,412

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 27: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

NOTE: Drink a drop of water

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

http://www.flickr.com/photos/donkeyhotey/8422065722

Page 28: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Growing upthe Ruby InterpreterHow do we grow up the Ruby interpreter?

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 29: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Software consists of many components

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 30: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Ruby interpreter

Ruby (Rails) app

RubyGems/Bundler

So many gemssuch as Ruby on Rails (ActiveSupport, …) and so on.

Ruby’s components for users

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

i gigantum umeris insidentesStanding on the shoulders of giants

Page 31: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Interpret on RubyVM

Rubyscript

Parse

Compile

RubyBytecode

Object management(GC)

Threading

Embeddedclasses and methods

(Array, String, …)

BundledLibraries

Evaluator

GemLibraries

Ruby’s componentsfrom core developer’s perspective

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 32: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

My contributions

• Improve the performance for• Evaluator (10 years)• Thread management (10 years)•Memory management (recent years)

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 33: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

History of Ruby interpreter

1993 2/24Birth of Ruby(in Matz’ computer)

1995/12Ruby 0.951st release

1996/12Ruby 1.0

1998/12Ruby 1.2

1999/12Ruby 1.4

2000/6Ruby 1.6

2003/8Ruby 1.8

2009/1Ruby 1.9.0

2013/2Ruby 2.0

2004/1YARV development

2013/12Ruby 2.1.0

2013/3RGenGC

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 34: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Grow up Ruby interpreter by modification of core components

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 35: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Interpret on RubyVM

Rubyscript

Parse

Compile

RubyBytecode

Object management(GC)

Threading

Embeddedclasses and methods

(Array, String, …)

BundledLibraries

Evaluator

GemLibraries

Evaluator

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 36: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Evaluator

• Named YARV: Yet another RubyVM• Start until 10 years ago (2004/01/01)

• Simple stack machine architecture

• Execute each bytecode instructions one by one

• Apply many known optimization techniques

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 37: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

EvaluatorCompile Ruby to AST

a =

MethodDispatch(:+)

cb

Abstract Syntax TreeRuby Program

a = b + c

a =

MethodDispatch(:+)

cb

a =

MethodDispatch(:+)

cb

Parse

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 38: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

EvaluatorCompile AST to Bytecode

getlocal bgetlocal csend +setlocal a

VM Instructionsa =

MethodDispatch(:+)

cb

Abstract Syntax Tree

a =

MethodDispatch(:+)

cb

a =

MethodDispatch(:+)

cb

Tree data Sequential dataGrowing the Ruby interpreter, Koichi Sasada,

RubyConf Brasil 2014

Page 39: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

EvaluatorExecution as stack machine

Ruby Program

a = b + c

getlocal bgetlocal csend +setlocal a

YARV Instructions

a

b

c b

c

b+c

b+c

VM Stack

Compile

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 40: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

EvaluatorOptimizations• Apply many techniques to improve performance

• Peephole optimizations

• Specialized instructions

• Stack frame layout

• Efficient exception handling

• Efficient block representation

• Direct threading

• Stack caching

• Instructions and operands unifications

• …

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 41: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 42: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

EvaluatorOptimizations: Basic concept

•Analysis usage

And optimize for frequent cases

• Example: Exception handling• Exceptions occur *EXCEPTIONAL* so

optimize for no-exception control flow

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 43: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Performance evaluationcompare with Ruby 1.8

Higher is good

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 44: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Main components

•Evaluator

•Thread management

•Memory management

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 45: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Interpret on RubyVM

Rubyscript

Parse

Compile

RubyBytecode

Object management(GC)

Threading

Embeddedclasses and methods

(Array, String, …)

BundledLibraries

Evaluator

GemLibraries

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Threading

Page 46: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Threading

• Using native threads for each Ruby threads

• Parallel ruby execution is prohibited by GVL• You can free GVL if you write a code carefully in C level

and run it in parallel

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 47: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

ThreadingRuby 1.8 and before

CPU 1

CPU 2

IDLE

OS Thread 1

Thread 1 Thread 2 Thread 1

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

One OS (native) thread manages all Ruby threadsThis technique is a.k.a. Green Thread

Page 48: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

ThreadingLayered view

Ruby

Native ThreadSystem S/W

Processor(s)PE PE PE・・・

H/W

RT RT RT

NT

Thread SchedulerS/W

・・・

・・・

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 49: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

ThreadingRuby 1.9 and later

Thread 1

Thread 2

Thread 1

CPU 1

CPU 2IDLE

Native threads with Giant VM Lock

OS Thread 1

OS Thread 2

LockLock

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 50: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

ThreadingLayered view

Ruby

Native ThreadSystem S/W

Processor(s)PE PE PE・・・

H/W

RT RT RT

NT NT NT

Thread SchedulerS/W

・・・

・・・

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 51: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

ThreadingWhy not green threads?

• Advantage of green threads• Lightweight creation

• Disadvantage of green threads• Slow context switching (under portable way)

• Need to take care for blocking methods• Such as network read/write

• Difficult to collaborate with other C libraries using threads

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

We don’t need to make threads frequently.

(and we also have Fiber)

Page 52: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

ThreadingWhy GVL?

• To protect Ruby users from nightmare debugging• Shared parallel threading can make non-deterministic

bugs which is too hard to debug

• “Thread programming is too difficult for human being”

• Disadvantage• CRITICAL ISSUE: No parallel programming in Ruby

• Need another programming model for parallel• Current *SHARED EVERYTHING* model is not match

• Correct isolation level for each parallel execution units

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 53: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

ThreadingHow to make parallel ruby program?

• Now:• Use parallel threads provided by JRuby/Rubinius

• If you think you can make correct thread programs

• Use process (for example, w/ parallel gem, w/ dRuby)

• Future:• Introduce smart conventions to avoid threading bugs

• Matz likes Actor model (Erlang)

• Introduce limited shared memory model

• Introduce smart debugging feature• Detecting bugs, avoid nondeterministic behaviors, …

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 54: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Interpret on RubyVM

Rubyscript

Parse

Compile

RubyBytecode

Object management(GC)

Threading

Embeddedclasses and methods

(Array, String, …)

BundledLibraries

Evaluator

GemLibraries

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Object management (GC)

Page 55: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Object and memory management

• “Object.new” allocate a new object• “foo” (string literal) also allocate a new object

• Everything are objects in Ruby!

• We don’t need to “de-allocate” objects manually

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 56: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

http://www.flickr.com/photos/circasassy/6817999189/

Garbage collectionThe automatic memory management

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 57: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Automatic memory managementBasic concept

• Garbage collector recycled “unused” objects automatically

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 58: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

marked

marked marked

markedmarked

Mark & Sweep algorithm

1. Mark reachable objects from root objects

2. Sweep unmarkedobjects (collection and de-allocation)

Root objects

free

traverse

traverse traverse

traverse traverse

free

free

Collect unreachable objects

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 59: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

RGenGC: Restricted Generational GC

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 60: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Generational GC (GenGC) from Ruby 2.1•Weak generational hypothesis:

“Most objects die young”

→ Concentrate reclamation effort

only on the young objectsGrowing the Ruby interpreter, Koichi Sasada,

RubyConf Brasil 2014

http://www.flickr.com/photos/ell-r-brown/5026593710

Page 61: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Generational hypothesis

0

10

20

30

40

50

60

70

80

90

100

0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102

Perc

enta

ge o

f d

ead

ob

ject

#

Lifetime (Survibing GC count)

Object lifetime in RDoc(How many GCs surviving?)

95% of objects dead by the first GC

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 62: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Generational GC (GenGC)

• Separate young generation and old generation• Create objects as young generation

• Promote to old generation after surviving n-th GC

• In CRuby, n == 1 (after 1 GC, objects become old)• n == 2 or 3 from Ruby 2.2

• Usually, GC on young space (minor GC)

• GC on both spaces if no memory (major/full GC)

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 63: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

GenGC [Minor M&S GC] (1/2)

• Mark reachable objects from root objects.• Mark and promote to old

generation• Stop traversing after old

objects

→ Reduce mark overhead

• Sweep not (marked or old) objects

• Can’t collect Some unreachable objects

Root objects

new

new new

new/free

newnew

traverse

traverse traverse

traverse traverse

new/free

old/free

Don’t collect old objecteven if it is unreachable.

collect

1st MinorGC

old

old old

oldold

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 64: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

GenGC [Minor M&S GC] (2/2)

• Mark reachable objects from root objects.• Mark and promote to old

generation• Stop traversing after old

objects

→ Reduce mark overhead

• Sweep not (marked or old) objects

• Can’t collect Some unreachable objects

Root objects

old

old old

new/free

oldold

traverse

ignore ignore

ignore ignore

new/free

old/free

Don’t collect old objecteven if it is unreachable.

collect

2nd MinorGC

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 65: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

GenGC [Major M&S GC]

• Normal M&S

• Mark reachable objects from root objects• Mark and promote to old gen

• Sweep unmarked objects

• Sweep all unreachable (unused) objects

Root objects

new

old new

new/free

oldold

traverse

traverse traverse

traverse traverse

new/free

old/free

collect

collect

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 66: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

0

2

4

6

8

10

12

14

Total mark time (ms) Total sweep time (sec)

Acc

um

ula

ted

exe

cuti

on

tim

e (s

ec)

w/o RGenGC RGenGC

RGenGCPerformance evaluation (RDoc)

About x15 speedup!

* Disabled lazy sweep to measure correctly.Growing the Ruby interpreter, Koichi Sasada,

RubyConf Brasil 2014

Page 67: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

RGenGCPerformance evaluation (RDoc)

* 12% improvements compare with w/ and w/o RGenGC* Disabled lazy sweep to measure correctly.

103.7627479 102.3799865

16.043938154.946003494

0

20

40

60

80

100

120

140

w/o RGenGC RGenGC

Tota

l exe

cuti

on

tim

e (s

ec)

other than GC GC

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 68: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

RincGC: Restricted incremental GC

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 69: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

RincGCBackground and motivation• Ruby 2.1 had introduced generational GC

• Short marking time on minor GC

• Improve application throughput

• Still long pause time on major GC• Long pause time affects user response time

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 70: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Proposal:RincGC: Incremental GC for major GC

• Introducing incremental GC to reduce pause time

• Can combine with Generational GC

Generational GC

Incremental GC

Gen+Inc GC

Throughput High Low (a bit slow)

High

Pause time Long Short Short

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 71: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

RincGC: Base ideaIncremental GC algorithm• Move forward GC processes incrementally

• Mark slots incrementally

• Sweep slots incrementally

• Incremental marking in 3 phase• (1) Mark roots (pause)

• (2) Mark objects reachable from roots (incremental)

• (3) Mark roots again, and mark remembered objects (pause)

• Mark objects with three state (white/grey/black)• White: Untouched objects

• Grey: Marked, and prepare to mark directly reachable objects

• Black: Marked, and all directly reachable objects are marked

• Use write barriers to avoid marking miss from marked objects to live objects

• Detect new reference from black objects to white objects

• Remember such source black objects (marked at above (3))Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 72: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

RincGC:Incremental GC for CRuby/MRI• Incremental marking

• (1) mark roots (gc_mark_roots())

• (2) Do incremental mark at rb_newobj_of()

• (3) Make sure write barrier with WB-protected objects

• (4) Take care of WB-unprotected objects (MRI specific)

• Incremental sweeping• Modify current lazy sweep implementation

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 73: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

RincGC:Incremental marking• (1) mark roots (gc_mark_roots())

• Push all root objects onto “mark_stack”

• (2) Do incremental mark at rb_newobj_of()• Fall back incremental marking process periodically• Consume (pop) some objects from “mark_stack” and make

forward incremental marking

• (3) Make sure write barrier with WB-protected objects• Mark and push pointed object onto “mark_stack”

• (4) Take care of WB-unprotected objects (MRI specific)• After incremental marking (“mark_stack” is empty), re-scan

all roots and all living non-WB-protected objects• WB-unprotected objects are represented by bitmap

(WB_UNPROTECTED_BITS)

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 74: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

garbage_collect()

marks_start()

marks_step()

marks_finish()

sweep_start()

sweep_step()

sweep_finish()

marks_continue()

sweep_continue()

State: marking State: sweeping

State: none

Direct transition

Via mutator(clear doing flag)

newobj()

if (incremental_marking)

if (sweep_pages)

if (no pages)

doing: true

doing: true

RincGC:Diagram

if (root scan detect unmarked objects)

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 75: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Growing upthe Ruby InterpreterHow do we grow up the Ruby interpreter?

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 76: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Interpret on RubyVM

Rubyscript

Parse

Compile

RubyBytecode

Object management(GC)

Threading

Embeddedclasses and methods

(Array, String, …)

BundledLibraries

Evaluator

GemLibraries

DO EVERYTHING!NO SILVER BULLET!

Page 77: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Loop do•Survey techniques• Implement techniques• Invent new techniques•Evaluate techniques

end # endless

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

DO EVERYTHING!NO SILVER BULLET!

Page 78: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

DO EVERYTHING!NO SILVER BULLET!

We did.

We are doing.

We will do!!

Only continuous effort improves software quality.

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 79: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Future work: Many many many!!

• Evaluator• JIT compilation

• More drastic optimizations

• Threading• Parallel execution model (not a threading?)

• Object management and GC• Compaction GC

• Lightweight object allocation

• CoW friendly memory management

• And more

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 80: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Summary

•Ruby 2.1 and Ruby 2.2

•How to grow up the Ruby interpreter?• Evaluator• Threading• Object management / Garbage collection

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 81: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Summary

•Ruby 2.1 and Ruby 2.2

•How to grow up the Ruby interpreter?

My answers is:

#=> Continue software development

(with love?)

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014

Page 82: Growing Ruby Interpreterko1/activities/2014_rubyconfbr_pub.pdfRuby (Rails) app RubyGems/Bundler So many gems such as Ruby on Rails (ActiveSupport, …) and so on. Ruby’s components

Thank you for your attention

Koichi Sasada<[email protected]>

Growing the Ruby interpreter, Koichi Sasada, RubyConf Brasil 2014