SlideShare a Scribd company logo
About me
1
Guelph ES&C Grad – spring 2014
Currently employed at IBM as a
Runtime Technologies Software Dev
Twitter: @craiglehmann
Ruby Under The Hood
http://lolsnaps.com/upload_pic/EveryTimeILookUnderThe
HoodOfACar-97946.jpg
Program compilation
3
Program compilation
4
Program compilation
5
Program compilation
6
3.times do |n|
puts n
end
Program compilation
7
Tokens:
3.times do |n|
puts n
end puts
ndo
end
3 dot times
Program compilation
8
AST
3.times do |n|
puts n
end
puts n
3 times
call
block
command
Program compilation
9
The Ruby Interpreter?
The Ruby Interpreter implements a virtual machine.
10
Bytecode Interpreter
 The main VM execution loop.
 Maps bytecodes to executable native instructions.
 Implements a virtual stack machine.
 Individual bytecodes implemented in C.
11
12
Ruby is a Stack Machine!
Push Down Stack
13
1 + 2
Push Down Stack
14
1 + 2 Push 1
Push 2
opt_plus
Bytecodes:
Push Down Stack
15
1 + 2 Push 1
Push 2
opt_plus
Bytecodes:
1
Push 1
Push Down Stack
16
1 + 2 Push 1
Push 2
opt_plus
Bytecodes:
1
Push 1
2
Push 2
1
Push Down Stack
17
1 + 2 Push 1
Push 2
opt_plus
Bytecodes:
1
Push 1
2
Push 2
3
opt_plus
1
Call Stack example and compiled method
18
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_simple
Three Stacks
19
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_simple
C Stack VM Stack Ruby Call Stack
Call Stack example and compiled method
20
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_simple
call_method
C Stack
exec_core
…
main()
VM Stack Ruby Call Stack
Call Stack example and compiled method
21
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_simple
call_method
C Stack
exec_core
…
main()
opt_send_simple puts
VM Stack
“hello World”
self
Ruby Call Stack
Call Stack example and compiled method
22
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_simple
call_method
C Stack
exec_core
…
main()
opt_send_simple puts
VM Stack
“hello World”
self
puts
Ruby Call Stack
do_things
Three Stacks
23
call_method
C Stack
exec_core
…
main()
opt_send_simple puts
VM Stack
“hello World”
self
puts
Ruby Call Stack
do_things
What does this look like in the Ruby’s VM?
24
puts
Ruby Call Stack
do_things
def do_things
puts "Hello World"
end
What does this look like in the Ruby’s VM?
25
puts
Ruby Call Stack
do_things
def do_things
puts "Hello World"
end
PC
Control Frame
SP
Self
type
What does this look like in the Ruby’s VM?
26
puts
Ruby Call Stack
do_things
def do_things
puts "Hello World"
end
PC
Control Frame
SP
Self
type
putself
putstring “Hello World”
opt_send_simple
What does this look like in the Ruby’s VM?
27
puts
Ruby Call Stack
do_things
def do_things
puts "Hello World"
end
PC
Control Frame
SP
Self
type
putself
putstring “Hello World”
opt_send_simple
opt_send_simple puts
VM Stack
“hello World”
self
Class lookup? What happens when you want to create
an object?
28
class Person
end
p = Person.new()
Class lookup? What happens when you want to create
an object?
29
class Person
end
p = Person.new()
Class lookup? What happens when you want to create
an object?
30
class Person
end
p = Person.new()
Person = "Craig"
#Warning: already initialized constant person
Intro to garbage collection
What is garbage collection?
Different types of garbage collection algorithms
Mark & Sweep demo
31
32
What is garbage collection?
 Automatic memory management
 Manually managing memory is hard!
 Aggregate freeing memory & object destruction operations
 Gives the illusion of unlimited memory
33
Different types of garbage collection algorithms
 Reference Counting
• Keep a count along with each object indicating how many references it currently
has. An object with 0 references can have it's memory re-used.
• Cannot manage cyclically referenced objects.
 Tracing Algorithms
• keep track of which objects are in use by recursively tracing objects referring to
other objects, starting from a root set of objects.
What does it mean to die
• An object consumes one resource, memory.
• When an object becomes unreachable, it can never be used again.
• Cycles are collected together, the mutator cannot access these
object.
• Sweeping objects means adding it’s memory to a list for reuse.
• What about when an object consumes more than just one resource?
34
Mark and Sweep Demo
35
Mark and Sweep Demo
36
Mark and Sweep Demo
37
Mark and Sweep Demo
38
Mark and Sweep Demo
39
Mark and Sweep Demo
40
Mark and Sweep Demo
41
Mark and Sweep Demo
42
Object Finalization
• Some objects may have operations that need to occur pre/post
collection
• e.g. file object
43
Execution Environment
Architecture of a Managed Runtime
44
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/AST
Compiler
Just-In-Time Compiler
Interpreter
Execution Environment
Architecture of a Managed Runtime
45
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/AST
Compiler
Just-In-Time Compiler
Interpreter
Execution Environment
Architecture of a Managed Runtime
46
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/AST
Compiler
Just-In-Time Compiler
Interpreter
Execution Environment
Architecture of a Managed Runtime
47
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/AST
Compiler
Just-In-Time Compiler
Interpreter
 Interpreter context
 Thread context
 Language callstack
Execution Environment
Architecture of a Managed Runtime
48
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/AST
Compiler
Just-In-Time Compiler
Interpreter
Execution Environment
Architecture of a Managed Runtime
49
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/AST
Compiler
Just-In-Time Compiler
Interpreter
Execution Environment
Architecture of a Managed Runtime
50
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/AST
Compiler
Just-In-Time Compiler
Interpreter
Thank You!
Twitter: @craigLehmann
Email: craigl@ca.ibm.com

More Related Content

PPTX
Ruby3x3: How are we going to measure 3x
PDF
Building Asynchronous Applications
PPTX
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
PDF
Packed Objects: Fast Talking Java Meets Native Code - Steve Poole (IBM)
PDF
Data Summer Conf 2018, “Building unified Batch and Stream processing pipeline...
PPTX
Shall we play a game?
PDF
Seam 3 from a Web developer’s point of view, Matija Mazi (Parsek)
PDF
Async await...oh wait!
Ruby3x3: How are we going to measure 3x
Building Asynchronous Applications
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Packed Objects: Fast Talking Java Meets Native Code - Steve Poole (IBM)
Data Summer Conf 2018, “Building unified Batch and Stream processing pipeline...
Shall we play a game?
Seam 3 from a Web developer’s point of view, Matija Mazi (Parsek)
Async await...oh wait!

What's hot (20)

PDF
R ext world/ useR! Kiev
PDF
How DSL works on Ruby
PDF
Byteman and The Jokre, Sanne Grinovero (JBoss by RedHat)
PDF
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
PDF
Event Driven Architecture with Apache Camel
ODP
Integration using Apache Camel and Groovy
PPTX
Ruby/rails performance and profiling
PDF
Atlanta Hadoop Users Meetup 09 21 2016
ODP
Getting Started with Apache Camel at DevNation 2014
KEY
Rails performance at Justin.tv - Guillaume Luccisano
PDF
How the HotSpot and Graal JVMs execute Java Code
PDF
Flink Forward SF 2017: Dean Wampler - Streaming Deep Learning Scenarios with...
KEY
Devignition 2011
PDF
Introduction to Apache Beam & No Shard Left Behind: APIs for Massive Parallel...
PDF
The Future of Real-Time in Spark
PDF
Stackato v6
PDF
Perl-Critic
PDF
Apache Camel - The integration library
PPTX
The Actor Model - Towards Better Concurrency
PDF
Integrating systems in the age of Quarkus and Camel
R ext world/ useR! Kiev
How DSL works on Ruby
Byteman and The Jokre, Sanne Grinovero (JBoss by RedHat)
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
Event Driven Architecture with Apache Camel
Integration using Apache Camel and Groovy
Ruby/rails performance and profiling
Atlanta Hadoop Users Meetup 09 21 2016
Getting Started with Apache Camel at DevNation 2014
Rails performance at Justin.tv - Guillaume Luccisano
How the HotSpot and Graal JVMs execute Java Code
Flink Forward SF 2017: Dean Wampler - Streaming Deep Learning Scenarios with...
Devignition 2011
Introduction to Apache Beam & No Shard Left Behind: APIs for Massive Parallel...
The Future of Real-Time in Spark
Stackato v6
Perl-Critic
Apache Camel - The integration library
The Actor Model - Towards Better Concurrency
Integrating systems in the age of Quarkus and Camel
Ad

Similar to Ruby Under The Hood (20)

PDF
Reddirt2011
PDF
Virtual Machines Lecture
KEY
MacRuby: What is it? and why should you care?
PDF
Ruby Under The Hood - By Craig Lehmann and Robert Young - Ottawa Ruby Novembe...
PDF
The Future of JRuby - Baruco 2013
PPTX
Memory models in c#
PDF
JRuby: The Hard Parts
PPT
12 virtualmachine
PDF
MacRuby
PDF
RubyMotion: Under the Hood
PPTX
Grow and Shrink - Dynamically Extending the Ruby VM Stack
PPTX
Ruby for .NET developers
PDF
robert-kovacsics-part-ii-dissertation
PDF
Rubinius - A Tool of the Future
PDF
Ruby for C#-ers (ScanDevConf 2010)
ZIP
Ruby on the JVM
PDF
Ruby seen from a C# developer
PDF
Ruby seen by a C# developer
ODP
The D Programming Language - Why I love it!
PDF
Metaprogramming ruby
Reddirt2011
Virtual Machines Lecture
MacRuby: What is it? and why should you care?
Ruby Under The Hood - By Craig Lehmann and Robert Young - Ottawa Ruby Novembe...
The Future of JRuby - Baruco 2013
Memory models in c#
JRuby: The Hard Parts
12 virtualmachine
MacRuby
RubyMotion: Under the Hood
Grow and Shrink - Dynamically Extending the Ruby VM Stack
Ruby for .NET developers
robert-kovacsics-part-ii-dissertation
Rubinius - A Tool of the Future
Ruby for C#-ers (ScanDevConf 2010)
Ruby on the JVM
Ruby seen from a C# developer
Ruby seen by a C# developer
The D Programming Language - Why I love it!
Metaprogramming ruby
Ad

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
Project quality management in manufacturing
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Well-logging-methods_new................
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Digital Logic Computer Design lecture notes
PPTX
web development for engineering and engineering
PPTX
OOP with Java - Java Introduction (Basics)
PDF
composite construction of structures.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Project quality management in manufacturing
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Well-logging-methods_new................
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
R24 SURVEYING LAB MANUAL for civil enggi
Digital Logic Computer Design lecture notes
web development for engineering and engineering
OOP with Java - Java Introduction (Basics)
composite construction of structures.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Foundation to blockchain - A guide to Blockchain Tech
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Automation-in-Manufacturing-Chapter-Introduction.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CH1 Production IntroductoryConcepts.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx

Ruby Under The Hood

  • 1. About me 1 Guelph ES&C Grad – spring 2014 Currently employed at IBM as a Runtime Technologies Software Dev Twitter: @craiglehmann
  • 2. Ruby Under The Hood http://lolsnaps.com/upload_pic/EveryTimeILookUnderThe HoodOfACar-97946.jpg
  • 7. Program compilation 7 Tokens: 3.times do |n| puts n end puts ndo end 3 dot times
  • 8. Program compilation 8 AST 3.times do |n| puts n end puts n 3 times call block command
  • 10. The Ruby Interpreter? The Ruby Interpreter implements a virtual machine. 10
  • 11. Bytecode Interpreter  The main VM execution loop.  Maps bytecodes to executable native instructions.  Implements a virtual stack machine.  Individual bytecodes implemented in C. 11
  • 12. 12 Ruby is a Stack Machine!
  • 14. Push Down Stack 14 1 + 2 Push 1 Push 2 opt_plus Bytecodes:
  • 15. Push Down Stack 15 1 + 2 Push 1 Push 2 opt_plus Bytecodes: 1 Push 1
  • 16. Push Down Stack 16 1 + 2 Push 1 Push 2 opt_plus Bytecodes: 1 Push 1 2 Push 2 1
  • 17. Push Down Stack 17 1 + 2 Push 1 Push 2 opt_plus Bytecodes: 1 Push 1 2 Push 2 3 opt_plus 1
  • 18. Call Stack example and compiled method 18 def do_things puts "Hello World" end Putself Putstring “Hello World” opt_send_simple
  • 19. Three Stacks 19 def do_things puts "Hello World" end Putself Putstring “Hello World” opt_send_simple C Stack VM Stack Ruby Call Stack
  • 20. Call Stack example and compiled method 20 def do_things puts "Hello World" end Putself Putstring “Hello World” opt_send_simple call_method C Stack exec_core … main() VM Stack Ruby Call Stack
  • 21. Call Stack example and compiled method 21 def do_things puts "Hello World" end Putself Putstring “Hello World” opt_send_simple call_method C Stack exec_core … main() opt_send_simple puts VM Stack “hello World” self Ruby Call Stack
  • 22. Call Stack example and compiled method 22 def do_things puts "Hello World" end Putself Putstring “Hello World” opt_send_simple call_method C Stack exec_core … main() opt_send_simple puts VM Stack “hello World” self puts Ruby Call Stack do_things
  • 23. Three Stacks 23 call_method C Stack exec_core … main() opt_send_simple puts VM Stack “hello World” self puts Ruby Call Stack do_things
  • 24. What does this look like in the Ruby’s VM? 24 puts Ruby Call Stack do_things def do_things puts "Hello World" end
  • 25. What does this look like in the Ruby’s VM? 25 puts Ruby Call Stack do_things def do_things puts "Hello World" end PC Control Frame SP Self type
  • 26. What does this look like in the Ruby’s VM? 26 puts Ruby Call Stack do_things def do_things puts "Hello World" end PC Control Frame SP Self type putself putstring “Hello World” opt_send_simple
  • 27. What does this look like in the Ruby’s VM? 27 puts Ruby Call Stack do_things def do_things puts "Hello World" end PC Control Frame SP Self type putself putstring “Hello World” opt_send_simple opt_send_simple puts VM Stack “hello World” self
  • 28. Class lookup? What happens when you want to create an object? 28 class Person end p = Person.new()
  • 29. Class lookup? What happens when you want to create an object? 29 class Person end p = Person.new()
  • 30. Class lookup? What happens when you want to create an object? 30 class Person end p = Person.new() Person = "Craig" #Warning: already initialized constant person
  • 31. Intro to garbage collection What is garbage collection? Different types of garbage collection algorithms Mark & Sweep demo 31
  • 32. 32 What is garbage collection?  Automatic memory management  Manually managing memory is hard!  Aggregate freeing memory & object destruction operations  Gives the illusion of unlimited memory
  • 33. 33 Different types of garbage collection algorithms  Reference Counting • Keep a count along with each object indicating how many references it currently has. An object with 0 references can have it's memory re-used. • Cannot manage cyclically referenced objects.  Tracing Algorithms • keep track of which objects are in use by recursively tracing objects referring to other objects, starting from a root set of objects.
  • 34. What does it mean to die • An object consumes one resource, memory. • When an object becomes unreachable, it can never be used again. • Cycles are collected together, the mutator cannot access these object. • Sweeping objects means adding it’s memory to a list for reuse. • What about when an object consumes more than just one resource? 34
  • 35. Mark and Sweep Demo 35
  • 36. Mark and Sweep Demo 36
  • 37. Mark and Sweep Demo 37
  • 38. Mark and Sweep Demo 38
  • 39. Mark and Sweep Demo 39
  • 40. Mark and Sweep Demo 40
  • 41. Mark and Sweep Demo 41
  • 42. Mark and Sweep Demo 42
  • 43. Object Finalization • Some objects may have operations that need to occur pre/post collection • e.g. file object 43
  • 44. Execution Environment Architecture of a Managed Runtime 44 Platform Abstraction Layer Garbage Collector Diagnostic Services Source Code Bytecode/AST Compiler Just-In-Time Compiler Interpreter
  • 45. Execution Environment Architecture of a Managed Runtime 45 Platform Abstraction Layer Garbage Collector Diagnostic Services Source Code Bytecode/AST Compiler Just-In-Time Compiler Interpreter
  • 46. Execution Environment Architecture of a Managed Runtime 46 Platform Abstraction Layer Garbage Collector Diagnostic Services Source Code Bytecode/AST Compiler Just-In-Time Compiler Interpreter
  • 47. Execution Environment Architecture of a Managed Runtime 47 Platform Abstraction Layer Garbage Collector Diagnostic Services Source Code Bytecode/AST Compiler Just-In-Time Compiler Interpreter  Interpreter context  Thread context  Language callstack
  • 48. Execution Environment Architecture of a Managed Runtime 48 Platform Abstraction Layer Garbage Collector Diagnostic Services Source Code Bytecode/AST Compiler Just-In-Time Compiler Interpreter
  • 49. Execution Environment Architecture of a Managed Runtime 49 Platform Abstraction Layer Garbage Collector Diagnostic Services Source Code Bytecode/AST Compiler Just-In-Time Compiler Interpreter
  • 50. Execution Environment Architecture of a Managed Runtime 50 Platform Abstraction Layer Garbage Collector Diagnostic Services Source Code Bytecode/AST Compiler Just-In-Time Compiler Interpreter