SlideShare a Scribd company logo
Ruby 2.4 Internals
Koichi Sasada
ko1@cookpad.com
Note:
Ruby 2.4.0 has
several bugs.
Note2:
No x.y.0 doesn’t
have several bugs.
Ruby 2.4.0
New features
written in a release announcement
• Introduce hash table improvement (by Vladimir Makarov)
• Binding#irb: Start a REPL session similar to binding.pry
• Unify Fixnum and Bignum into Integer
• String supports Unicode case mappings
• Performance improvements
• Array#max, Array#min
• Regexp#match?
• speed up instance variable access
• Debugging
• Thread#report_on_exception and Thread.report_on_exception
• Thread deadlock detection now shows threads with their backtrace and dependency
• Other notable changes since 2.3
• Support OpenSSL 1.1.0 (drop support for 0.9.7 or prior)
• ext/tk is now removed from stdlib Feature #8539
• XMLRPC is now removed from stdlib Feature #12160
Ruby 2.4 Internals
Koichi Sasada
ko1@cookpad.com
https://blog.heroku.com/r
uby-2-4-features-hashes-
integers-rounding
Any other
topics?
benchmark/bm_app_lc_fizzbuzz.rb
Bug #10212 MRI is not for lambda calculus
JRuby 26 sec
mruby 27 sec
MRI 114 sec
Feature #12628
change block/env structs
Ruby 2.4 Internals
Change block/env structs
Koichi Sasada
ko1@cookpad.com
Issues
1. we need to clear
rb_control_frame_t::block_iseq for
every frame setup. It consumes space (a
VALUE for each frame) and initializing
time.
2. There are several block passing ways by
ISeq (iter{...}), Proc(iter(&pr)),
Symbol(iter(:sym)). However, they are
not optimized (for Symbol blocks, there
is only ad-hoc check code).
3. Env (and Proc, Binding) objects are not
WB-protected ([Bug #10212]).
Method dispatch (etc)
improvements
Cleanup src code
Improve GC perf.
Patch
•https://github.com/ruby/ruby/compare/tru
nk...ko1:block_code
•“Showing with 1,863 additions and 1,070
deletions.”
Approaches
•For (1), (2)
•Introduce Block Handler (BH)
•Using BH
•For (3)
•Introduce Write Barriers (WB) for Env objects
WB protected or unprotected?
•Ruby 2.1.0 introduced Generational GC
•Only newer objects
•GenGC requires “Write barriers” (WB), but
MRI allows WB unprotected objects
(See my past presentations for details)
•WB protected objects: GenGC → Fast
•WB unprotected objects: Not GenGC → Slow
RubyVM::Env objects
•Env objects represent
captured local variables
•Each Proc or Binding has
at least one Env object
•Proc object “$pr”
consists of 3 Env objects
a = 1
1.times{|b|
1.times{|c|
$pr = Proc.new{
# you can access a, b, c
}
}
}
Proc
$pr
Env
c=0
Env
b=0
Env
a=1
RubyVM::Env objects were
WB-unprotected
•They were WB unprotected because:
•Difficulty of implementation
•Performance issue
Performance issue
Assignment performance
• Ruby 2.3 or before
*(ep - idx) = val;
• Naïve implementation
#define VM_EP_IN_HEAP_P(th, ep) ¥
(!((th)->stack <= (ep) && ¥
(ep) < ((th)->stack + (th)->stack_size)))
if (VM_EP_IN_HEAP_P(ep)) {
RB_OBJ_WRITE(VM_ENV_EP_ENVVAL(ep),
ep-idx, val);
}
else *(ep - idx) = val;
Ideas
1. Lightweight escape detection
2. Skip WB except really required timing
Idea
Lightweight escape detection
•Move cfp->flags to ep[0]
•Introduce a VM_ENV_FLAG_ESCAPED flag to
represent escaped Env.
• // Before
#define VM_EP_IN_HEAP_P(th, ep) (!((th)->stack <= (ep) && (ep) < ((th)->stack + (th)->stack_size)))
• // After
#define VM_EP_IN_HEAP_P(ep) (ep[0] & VM_ENV_FLAG_ESCAPED)
Idea
Skip WB except really required timing
1. At initializing Env objects, VM_ENV_FLAG_WB_REQUIRED is true.
2. At first local variable assignment, VM_ENV_FLAG_WB_REQUIRED
is true, we remember this Env object forcibly. And turn off this
flag.
3. At next local variable assignment, VM_ENV_FLAG_WB_REQUIRED
is false, so we can ignore WB protection.
4. At GC marking for this Env object, we turn on
VM_ENV_FLAG_WB_REQUIRED and goto (2).
Very danger technique because it depends on GC implementation
Naïve code
#define VM_EP_IN_HEAP_P(th, ep) (!((th)->stack <= (ep)
&& (ep) < ((th)->stack + (th)->stack_size)))
vm_env_write(const VALUE *ep, int index, VALUE v) {
if (VM_EP_IN_HEAP_P(ep)) {
RB_OBJ_WRITE(VM_ENV_EP_ENVVAL(ep), ep-idx, val);
}
else {
*(ep - idx) = val;
}
}
Final code
vm_env_write(const VALUE *ep, int index, VALUE v) {
VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
if (LIKELY((flags & VM_ENV_FLAG_WB_REQUIRED) == 0)) {
*(ep - idx) = val; /* mostly used */
}
else {
/* remember env value forcibly */
vm_env_write_slowpath(ep, index, v);
}
}
Benchmark result
Bug #10212 MRI is not for lambda calculus
lc_fizzbuzz with MRI versions
0
50
100
150
ruby_2_0 ruby_2_1 ruby_2_2 ruby_2_3 trunk
Executiontime(sec)
app_lc_fizzbuzz
Bug #10212 MRI is not for lambda calculus
lc_fizzbuzz with MRI, JRuby, mruby
36.976
18.706
40.185
0
10
20
30
40
50
trunk jruby mruby
Executiontime(sec)
app_lc_fizzbuzz
Summary
•Ruby 2.4.0 has many improvements
•Now Proc (Env) objects are WB protected
and we have more faster GC (marking)
•My ideas allow to protect Env objects
without big performance impact
Thank you for your attention
Koichi Sasada
<ko1@cookpad.com>

More Related Content

What's hot

How to Begin Developing Ruby Core
How to Begin Developing Ruby CoreHow to Begin Developing Ruby Core
How to Begin Developing Ruby Core
Hiroshi SHIBATA
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
bobmcwhirter
 
TorqueBox for Rubyists
TorqueBox for RubyistsTorqueBox for Rubyists
TorqueBox for Rubyists
bobmcwhirter
 
JRuby @ Boulder Ruby
JRuby @ Boulder RubyJRuby @ Boulder Ruby
JRuby @ Boulder Ruby
Nick Sieger
 
When Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of TorqueboxWhen Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of Torquebox
rockyjaiswal
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)goccy
 
Fiber in the 10th year
Fiber in the 10th yearFiber in the 10th year
Fiber in the 10th year
Koichi Sasada
 
MongoUK 2011 - Rplacing RabbitMQ with MongoDB
MongoUK 2011 - Rplacing RabbitMQ with MongoDBMongoUK 2011 - Rplacing RabbitMQ with MongoDB
MongoUK 2011 - Rplacing RabbitMQ with MongoDBBoxed Ice
 
Concurrency in Python
Concurrency in PythonConcurrency in Python
Concurrency in Python
Mosky Liu
 
TorqueBox - When Java meets Ruby
TorqueBox - When Java meets RubyTorqueBox - When Java meets Ruby
TorqueBox - When Java meets RubyBruno Oliveira
 
Mасштабирование микросервисов на Go, Matt Heath (Hailo)
Mасштабирование микросервисов на Go, Matt Heath (Hailo)Mасштабирование микросервисов на Go, Matt Heath (Hailo)
Mасштабирование микросервисов на Go, Matt Heath (Hailo)
Ontico
 
Pfm technical-inside
Pfm technical-insidePfm technical-inside
Pfm technical-inside
iyatomi takehiro
 
Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016
Charles Nutter
 
TorqueBox - Ultrapassando a fronteira entre Java e Ruby
TorqueBox - Ultrapassando a fronteira entre Java e RubyTorqueBox - Ultrapassando a fronteira entre Java e Ruby
TorqueBox - Ultrapassando a fronteira entre Java e RubyBruno Oliveira
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in Practise
Christian Melchior
 
Loom and concurrency latest
Loom and concurrency latestLoom and concurrency latest
Loom and concurrency latest
Srinivasan Raghavan
 
Ruby Concurrency and EventMachine
Ruby Concurrency and EventMachineRuby Concurrency and EventMachine
Ruby Concurrency and EventMachine
Christopher Spring
 
DataMapper on Infinispan
DataMapper on InfinispanDataMapper on Infinispan
DataMapper on InfinispanLance Ball
 
Managing large and distributed Eclipse server applications.
Managing large and distributed Eclipse server applications.Managing large and distributed Eclipse server applications.
Managing large and distributed Eclipse server applications.
Gunnar Wagenknecht
 
RubyKaigi2015 making robots-with-mruby
RubyKaigi2015 making robots-with-mrubyRubyKaigi2015 making robots-with-mruby
RubyKaigi2015 making robots-with-mruby
yamanekko
 

What's hot (20)

How to Begin Developing Ruby Core
How to Begin Developing Ruby CoreHow to Begin Developing Ruby Core
How to Begin Developing Ruby Core
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
TorqueBox for Rubyists
TorqueBox for RubyistsTorqueBox for Rubyists
TorqueBox for Rubyists
 
JRuby @ Boulder Ruby
JRuby @ Boulder RubyJRuby @ Boulder Ruby
JRuby @ Boulder Ruby
 
When Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of TorqueboxWhen Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of Torquebox
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
 
Fiber in the 10th year
Fiber in the 10th yearFiber in the 10th year
Fiber in the 10th year
 
MongoUK 2011 - Rplacing RabbitMQ with MongoDB
MongoUK 2011 - Rplacing RabbitMQ with MongoDBMongoUK 2011 - Rplacing RabbitMQ with MongoDB
MongoUK 2011 - Rplacing RabbitMQ with MongoDB
 
Concurrency in Python
Concurrency in PythonConcurrency in Python
Concurrency in Python
 
TorqueBox - When Java meets Ruby
TorqueBox - When Java meets RubyTorqueBox - When Java meets Ruby
TorqueBox - When Java meets Ruby
 
Mасштабирование микросервисов на Go, Matt Heath (Hailo)
Mасштабирование микросервисов на Go, Matt Heath (Hailo)Mасштабирование микросервисов на Go, Matt Heath (Hailo)
Mасштабирование микросервисов на Go, Matt Heath (Hailo)
 
Pfm technical-inside
Pfm technical-insidePfm technical-inside
Pfm technical-inside
 
Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016
 
TorqueBox - Ultrapassando a fronteira entre Java e Ruby
TorqueBox - Ultrapassando a fronteira entre Java e RubyTorqueBox - Ultrapassando a fronteira entre Java e Ruby
TorqueBox - Ultrapassando a fronteira entre Java e Ruby
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in Practise
 
Loom and concurrency latest
Loom and concurrency latestLoom and concurrency latest
Loom and concurrency latest
 
Ruby Concurrency and EventMachine
Ruby Concurrency and EventMachineRuby Concurrency and EventMachine
Ruby Concurrency and EventMachine
 
DataMapper on Infinispan
DataMapper on InfinispanDataMapper on Infinispan
DataMapper on Infinispan
 
Managing large and distributed Eclipse server applications.
Managing large and distributed Eclipse server applications.Managing large and distributed Eclipse server applications.
Managing large and distributed Eclipse server applications.
 
RubyKaigi2015 making robots-with-mruby
RubyKaigi2015 making robots-with-mrubyRubyKaigi2015 making robots-with-mruby
RubyKaigi2015 making robots-with-mruby
 

Viewers also liked

How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
Leslie Samuel
 
20170302 tryswift tasting_tests
20170302 tryswift tasting_tests20170302 tryswift tasting_tests
20170302 tryswift tasting_tests
Kazuaki Matsuo
 
Information sharing and Experience consistency at Cookpad mobile application
Information sharing and Experience consistency at Cookpad mobile applicationInformation sharing and Experience consistency at Cookpad mobile application
Information sharing and Experience consistency at Cookpad mobile application
ichiko_revjune
 
EL EFECTO DE LAS HERRAMIENTAS TECNOLÓGICAS EN EL APRENDIZAJE
EL EFECTO DE LAS HERRAMIENTAS TECNOLÓGICAS EN EL APRENDIZAJEEL EFECTO DE LAS HERRAMIENTAS TECNOLÓGICAS EN EL APRENDIZAJE
EL EFECTO DE LAS HERRAMIENTAS TECNOLÓGICAS EN EL APRENDIZAJE
Eliani Cardenas Benitez
 
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 SpringGoでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Yahoo!デベロッパーネットワーク
 
Design in Tech Report 2017
Design in Tech Report 2017Design in Tech Report 2017
Design in Tech Report 2017
John Maeda
 
Secret of body language by Ritesh Gupta
Secret of body language by Ritesh GuptaSecret of body language by Ritesh Gupta
Secret of body language by Ritesh Gupta
Ritesh Gupta
 
Congress seeks metrics for the NIST Cybersecurity Framework
Congress seeks metrics for the NIST Cybersecurity FrameworkCongress seeks metrics for the NIST Cybersecurity Framework
Congress seeks metrics for the NIST Cybersecurity Framework
David Sweigert
 
DAR Resposta - Guia para famílias após o diagnóstico de autismo
DAR Resposta - Guia para famílias após o diagnóstico de autismoDAR Resposta - Guia para famílias após o diagnóstico de autismo
DAR Resposta - Guia para famílias após o diagnóstico de autismo
Sara Martins
 
TEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of WorkTEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of Work
Volker Hirsch
 
English Prefixes to Improve Your Vocabulary
English Prefixes to Improve Your VocabularyEnglish Prefixes to Improve Your Vocabulary
English Prefixes to Improve Your Vocabulary
Kelly Tracy
 
White Paper: Legislation to Ensure Veterans’ Access to Mental Health Care
White Paper: Legislation to Ensure Veterans’ Access to Mental Health Care	White Paper: Legislation to Ensure Veterans’ Access to Mental Health Care
White Paper: Legislation to Ensure Veterans’ Access to Mental Health Care
Swords to Plowshares
 
Michael Collins - TBEX International 2017 - How to sell your blog to sponsors...
Michael Collins - TBEX International 2017 - How to sell your blog to sponsors...Michael Collins - TBEX International 2017 - How to sell your blog to sponsors...
Michael Collins - TBEX International 2017 - How to sell your blog to sponsors...
TravelMedia.ie
 
Crisi econòmica i salut infantil. 2017
Crisi econòmica i salut infantil. 2017Crisi econòmica i salut infantil. 2017
Crisi econòmica i salut infantil. 2017
Pediatriadeponent
 
Johnson & Johnson Scholarship for BDPA Students
Johnson & Johnson Scholarship for BDPA StudentsJohnson & Johnson Scholarship for BDPA Students
Johnson & Johnson Scholarship for BDPA Students
BDPA Education and Technology Foundation
 
The social care common inspection framework (SCCIF): an introduction
The social care common inspection framework (SCCIF): an introductionThe social care common inspection framework (SCCIF): an introduction
The social care common inspection framework (SCCIF): an introduction
Ofsted
 
結果を出すチームビルディング術
結果を出すチームビルディング術結果を出すチームビルディング術
結果を出すチームビルディング術
Mao Ohnishi
 
26 Reasons You Need an MPS Assessment
26 Reasons You Need an MPS Assessment26 Reasons You Need an MPS Assessment
26 Reasons You Need an MPS Assessment
Chief Optimist
 
Zero Suicide in Healthcare International Declaration (March 2016)
Zero Suicide in Healthcare International Declaration (March 2016)Zero Suicide in Healthcare International Declaration (March 2016)
Zero Suicide in Healthcare International Declaration (March 2016)
David Covington
 
App Store Optimization For Extreme Newbies (Do not read if you're on a diet!)
App Store Optimization For Extreme Newbies (Do not read if you're on a diet!)App Store Optimization For Extreme Newbies (Do not read if you're on a diet!)
App Store Optimization For Extreme Newbies (Do not read if you're on a diet!)
AppTweak
 

Viewers also liked (20)

How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 
20170302 tryswift tasting_tests
20170302 tryswift tasting_tests20170302 tryswift tasting_tests
20170302 tryswift tasting_tests
 
Information sharing and Experience consistency at Cookpad mobile application
Information sharing and Experience consistency at Cookpad mobile applicationInformation sharing and Experience consistency at Cookpad mobile application
Information sharing and Experience consistency at Cookpad mobile application
 
EL EFECTO DE LAS HERRAMIENTAS TECNOLÓGICAS EN EL APRENDIZAJE
EL EFECTO DE LAS HERRAMIENTAS TECNOLÓGICAS EN EL APRENDIZAJEEL EFECTO DE LAS HERRAMIENTAS TECNOLÓGICAS EN EL APRENDIZAJE
EL EFECTO DE LAS HERRAMIENTAS TECNOLÓGICAS EN EL APRENDIZAJE
 
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 SpringGoでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
 
Design in Tech Report 2017
Design in Tech Report 2017Design in Tech Report 2017
Design in Tech Report 2017
 
Secret of body language by Ritesh Gupta
Secret of body language by Ritesh GuptaSecret of body language by Ritesh Gupta
Secret of body language by Ritesh Gupta
 
Congress seeks metrics for the NIST Cybersecurity Framework
Congress seeks metrics for the NIST Cybersecurity FrameworkCongress seeks metrics for the NIST Cybersecurity Framework
Congress seeks metrics for the NIST Cybersecurity Framework
 
DAR Resposta - Guia para famílias após o diagnóstico de autismo
DAR Resposta - Guia para famílias após o diagnóstico de autismoDAR Resposta - Guia para famílias após o diagnóstico de autismo
DAR Resposta - Guia para famílias após o diagnóstico de autismo
 
TEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of WorkTEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of Work
 
English Prefixes to Improve Your Vocabulary
English Prefixes to Improve Your VocabularyEnglish Prefixes to Improve Your Vocabulary
English Prefixes to Improve Your Vocabulary
 
White Paper: Legislation to Ensure Veterans’ Access to Mental Health Care
White Paper: Legislation to Ensure Veterans’ Access to Mental Health Care	White Paper: Legislation to Ensure Veterans’ Access to Mental Health Care
White Paper: Legislation to Ensure Veterans’ Access to Mental Health Care
 
Michael Collins - TBEX International 2017 - How to sell your blog to sponsors...
Michael Collins - TBEX International 2017 - How to sell your blog to sponsors...Michael Collins - TBEX International 2017 - How to sell your blog to sponsors...
Michael Collins - TBEX International 2017 - How to sell your blog to sponsors...
 
Crisi econòmica i salut infantil. 2017
Crisi econòmica i salut infantil. 2017Crisi econòmica i salut infantil. 2017
Crisi econòmica i salut infantil. 2017
 
Johnson & Johnson Scholarship for BDPA Students
Johnson & Johnson Scholarship for BDPA StudentsJohnson & Johnson Scholarship for BDPA Students
Johnson & Johnson Scholarship for BDPA Students
 
The social care common inspection framework (SCCIF): an introduction
The social care common inspection framework (SCCIF): an introductionThe social care common inspection framework (SCCIF): an introduction
The social care common inspection framework (SCCIF): an introduction
 
結果を出すチームビルディング術
結果を出すチームビルディング術結果を出すチームビルディング術
結果を出すチームビルディング術
 
26 Reasons You Need an MPS Assessment
26 Reasons You Need an MPS Assessment26 Reasons You Need an MPS Assessment
26 Reasons You Need an MPS Assessment
 
Zero Suicide in Healthcare International Declaration (March 2016)
Zero Suicide in Healthcare International Declaration (March 2016)Zero Suicide in Healthcare International Declaration (March 2016)
Zero Suicide in Healthcare International Declaration (March 2016)
 
App Store Optimization For Extreme Newbies (Do not read if you're on a diet!)
App Store Optimization For Extreme Newbies (Do not read if you're on a diet!)App Store Optimization For Extreme Newbies (Do not read if you're on a diet!)
App Store Optimization For Extreme Newbies (Do not read if you're on a diet!)
 

Similar to Ruby 2.4 Internals

Experiments in Sharing Java VM Technology with CRuby
Experiments in Sharing Java VM Technology with CRubyExperiments in Sharing Java VM Technology with CRuby
Experiments in Sharing Java VM Technology with CRuby
Matthew Gaudet
 
New features in Ruby 2.5
New features in Ruby 2.5New features in Ruby 2.5
New features in Ruby 2.5
Ireneusz Skrobiś
 
Writing a Gem with native extensions
Writing a Gem with native extensionsWriting a Gem with native extensions
Writing a Gem with native extensions
Tristan Penman
 
Grow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM StackGrow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM Stack
KeitaSugiyama1
 
introduction-infra-as-a-code using terraform
introduction-infra-as-a-code using terraformintroduction-infra-as-a-code using terraform
introduction-infra-as-a-code using terraform
niyof97
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
rivierarb
 
Fighting Against Chaotically Separated Values with Embulk
Fighting Against Chaotically Separated Values with EmbulkFighting Against Chaotically Separated Values with Embulk
Fighting Against Chaotically Separated Values with Embulk
Sadayuki Furuhashi
 
Linux kernel bug hunting
Linux kernel bug huntingLinux kernel bug hunting
Linux kernel bug hunting
Andrea Righi
 
MacRuby, an introduction
MacRuby, an introductionMacRuby, an introduction
MacRuby, an introduction
Olivier Gutknecht
 
Практики применения JRuby
Практики применения JRubyПрактики применения JRuby
Практики применения JRuby
.toster
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
Code lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf LinzCode lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf Linz
Ivan Krylov
 
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
chen yuki
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialTom Croucher
 
Java 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from OredevJava 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from Oredev
Mattias Karlsson
 
Mac ruby deployment
Mac ruby deploymentMac ruby deployment
Mac ruby deployment
Thilo Utke
 
JavaScript in 2015
JavaScript in 2015JavaScript in 2015
JavaScript in 2015
Igor Laborie
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
Troy Miles
 
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
Jung Kim
 

Similar to Ruby 2.4 Internals (20)

Experiments in Sharing Java VM Technology with CRuby
Experiments in Sharing Java VM Technology with CRubyExperiments in Sharing Java VM Technology with CRuby
Experiments in Sharing Java VM Technology with CRuby
 
New features in Ruby 2.5
New features in Ruby 2.5New features in Ruby 2.5
New features in Ruby 2.5
 
Writing a Gem with native extensions
Writing a Gem with native extensionsWriting a Gem with native extensions
Writing a Gem with native extensions
 
Grow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM StackGrow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM Stack
 
introduction-infra-as-a-code using terraform
introduction-infra-as-a-code using terraformintroduction-infra-as-a-code using terraform
introduction-infra-as-a-code using terraform
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
 
Fighting Against Chaotically Separated Values with Embulk
Fighting Against Chaotically Separated Values with EmbulkFighting Against Chaotically Separated Values with Embulk
Fighting Against Chaotically Separated Values with Embulk
 
Linux kernel bug hunting
Linux kernel bug huntingLinux kernel bug hunting
Linux kernel bug hunting
 
Lp seminar
Lp seminarLp seminar
Lp seminar
 
MacRuby, an introduction
MacRuby, an introductionMacRuby, an introduction
MacRuby, an introduction
 
Практики применения JRuby
Практики применения JRubyПрактики применения JRuby
Практики применения JRuby
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Code lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf LinzCode lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf Linz
 
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js Tutorial
 
Java 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from OredevJava 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from Oredev
 
Mac ruby deployment
Mac ruby deploymentMac ruby deployment
Mac ruby deployment
 
JavaScript in 2015
JavaScript in 2015JavaScript in 2015
JavaScript in 2015
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
 
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
 

Recently uploaded

De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 

Recently uploaded (20)

De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 

Ruby 2.4 Internals

  • 1. Ruby 2.4 Internals Koichi Sasada ko1@cookpad.com
  • 2.
  • 3.
  • 4. Note: Ruby 2.4.0 has several bugs. Note2: No x.y.0 doesn’t have several bugs.
  • 6. New features written in a release announcement • Introduce hash table improvement (by Vladimir Makarov) • Binding#irb: Start a REPL session similar to binding.pry • Unify Fixnum and Bignum into Integer • String supports Unicode case mappings • Performance improvements • Array#max, Array#min • Regexp#match? • speed up instance variable access • Debugging • Thread#report_on_exception and Thread.report_on_exception • Thread deadlock detection now shows threads with their backtrace and dependency • Other notable changes since 2.3 • Support OpenSSL 1.1.0 (drop support for 0.9.7 or prior) • ext/tk is now removed from stdlib Feature #8539 • XMLRPC is now removed from stdlib Feature #12160
  • 7. Ruby 2.4 Internals Koichi Sasada ko1@cookpad.com
  • 10.
  • 11.
  • 12.
  • 14.
  • 15. Bug #10212 MRI is not for lambda calculus JRuby 26 sec mruby 27 sec MRI 114 sec
  • 16.
  • 18. Ruby 2.4 Internals Change block/env structs Koichi Sasada ko1@cookpad.com
  • 19. Issues 1. we need to clear rb_control_frame_t::block_iseq for every frame setup. It consumes space (a VALUE for each frame) and initializing time. 2. There are several block passing ways by ISeq (iter{...}), Proc(iter(&pr)), Symbol(iter(:sym)). However, they are not optimized (for Symbol blocks, there is only ad-hoc check code). 3. Env (and Proc, Binding) objects are not WB-protected ([Bug #10212]). Method dispatch (etc) improvements Cleanup src code Improve GC perf.
  • 21. Approaches •For (1), (2) •Introduce Block Handler (BH) •Using BH •For (3) •Introduce Write Barriers (WB) for Env objects
  • 22. WB protected or unprotected? •Ruby 2.1.0 introduced Generational GC •Only newer objects •GenGC requires “Write barriers” (WB), but MRI allows WB unprotected objects (See my past presentations for details) •WB protected objects: GenGC → Fast •WB unprotected objects: Not GenGC → Slow
  • 23. RubyVM::Env objects •Env objects represent captured local variables •Each Proc or Binding has at least one Env object •Proc object “$pr” consists of 3 Env objects a = 1 1.times{|b| 1.times{|c| $pr = Proc.new{ # you can access a, b, c } } } Proc $pr Env c=0 Env b=0 Env a=1
  • 24. RubyVM::Env objects were WB-unprotected •They were WB unprotected because: •Difficulty of implementation •Performance issue
  • 25. Performance issue Assignment performance • Ruby 2.3 or before *(ep - idx) = val; • Naïve implementation #define VM_EP_IN_HEAP_P(th, ep) ¥ (!((th)->stack <= (ep) && ¥ (ep) < ((th)->stack + (th)->stack_size))) if (VM_EP_IN_HEAP_P(ep)) { RB_OBJ_WRITE(VM_ENV_EP_ENVVAL(ep), ep-idx, val); } else *(ep - idx) = val;
  • 26. Ideas 1. Lightweight escape detection 2. Skip WB except really required timing
  • 27. Idea Lightweight escape detection •Move cfp->flags to ep[0] •Introduce a VM_ENV_FLAG_ESCAPED flag to represent escaped Env. • // Before #define VM_EP_IN_HEAP_P(th, ep) (!((th)->stack <= (ep) && (ep) < ((th)->stack + (th)->stack_size))) • // After #define VM_EP_IN_HEAP_P(ep) (ep[0] & VM_ENV_FLAG_ESCAPED)
  • 28. Idea Skip WB except really required timing 1. At initializing Env objects, VM_ENV_FLAG_WB_REQUIRED is true. 2. At first local variable assignment, VM_ENV_FLAG_WB_REQUIRED is true, we remember this Env object forcibly. And turn off this flag. 3. At next local variable assignment, VM_ENV_FLAG_WB_REQUIRED is false, so we can ignore WB protection. 4. At GC marking for this Env object, we turn on VM_ENV_FLAG_WB_REQUIRED and goto (2). Very danger technique because it depends on GC implementation
  • 29. Naïve code #define VM_EP_IN_HEAP_P(th, ep) (!((th)->stack <= (ep) && (ep) < ((th)->stack + (th)->stack_size))) vm_env_write(const VALUE *ep, int index, VALUE v) { if (VM_EP_IN_HEAP_P(ep)) { RB_OBJ_WRITE(VM_ENV_EP_ENVVAL(ep), ep-idx, val); } else { *(ep - idx) = val; } }
  • 30. Final code vm_env_write(const VALUE *ep, int index, VALUE v) { VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS]; if (LIKELY((flags & VM_ENV_FLAG_WB_REQUIRED) == 0)) { *(ep - idx) = val; /* mostly used */ } else { /* remember env value forcibly */ vm_env_write_slowpath(ep, index, v); } }
  • 32. Bug #10212 MRI is not for lambda calculus lc_fizzbuzz with MRI versions 0 50 100 150 ruby_2_0 ruby_2_1 ruby_2_2 ruby_2_3 trunk Executiontime(sec) app_lc_fizzbuzz
  • 33. Bug #10212 MRI is not for lambda calculus lc_fizzbuzz with MRI, JRuby, mruby 36.976 18.706 40.185 0 10 20 30 40 50 trunk jruby mruby Executiontime(sec) app_lc_fizzbuzz
  • 34. Summary •Ruby 2.4.0 has many improvements •Now Proc (Env) objects are WB protected and we have more faster GC (marking) •My ideas allow to protect Env objects without big performance impact
  • 35. Thank you for your attention Koichi Sasada <ko1@cookpad.com>