ruby under the hood
TRANSCRIPT
![Page 1: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/1.jpg)
1
About me
Guelph ES&C Grad – spring 2014
Currently employed at IBM as aRuntime Technologies Software Dev
Twitter: @craiglehmann
![Page 2: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/2.jpg)
Ruby Under The Hood
http://lolsnaps.com/upload_pic/EveryTimeILookUnderTheHoodOfACar-97946.jpg
![Page 3: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/3.jpg)
3
Program compilation
![Page 4: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/4.jpg)
4
Program compilation
![Page 5: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/5.jpg)
5
Program compilation
![Page 6: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/6.jpg)
6
Program compilation
3.times do |n|
puts nend
![Page 7: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/7.jpg)
7
Program compilation
Tokens:3.times do |n|
puts nend
puts
ndo
end
3 dot times
![Page 8: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/8.jpg)
8
Program compilation
AST3.times do |n|
puts nend
puts n
3 times
call
block
command
![Page 9: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/9.jpg)
9
Program compilation
![Page 10: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/10.jpg)
10
The Ruby Interpreter?
The Ruby Interpreter implements a virtual machine.
![Page 11: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/11.jpg)
11
Bytecode Interpreter
The main VM execution loop. Maps bytecodes to executable native instructions. Implements a virtual stack machine. Individual bytecodes implemented in C.
![Page 12: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/12.jpg)
12
Ruby is a Stack Machine!
![Page 13: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/13.jpg)
13
Push Down Stack
1 + 2
![Page 14: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/14.jpg)
14
Push Down Stack
1 + 2 Push 1Push 2opt_plus
Bytecodes:
![Page 15: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/15.jpg)
15
Push Down Stack
1 + 2 Push 1Push 2opt_plus
Bytecodes:
1Push 1
![Page 16: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/16.jpg)
16
Push Down Stack
1 + 2 Push 1Push 2opt_plus
Bytecodes:
1Push 1
2Push 2
1
![Page 17: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/17.jpg)
17
Push Down Stack
1 + 2 Push 1Push 2opt_plus
Bytecodes:
1Push 1
2Push 2
3opt_plus
1
![Page 18: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/18.jpg)
18
Call Stack example and compiled method
def do_thingsputs "Hello
World"end
PutselfPutstring “Hello World”opt_send_simple
![Page 19: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/19.jpg)
19
Three Stacksdef do_things
puts "Hello World"end
PutselfPutstring “Hello World”opt_send_simple
C Stack VM Stack Ruby Call Stack
![Page 20: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/20.jpg)
20
Call Stack example and compiled method
def do_thingsputs "Hello
World"end
PutselfPutstring “Hello World”opt_send_simple
call_method
C Stack
exec_core…
main()
VM Stack Ruby Call Stack
![Page 21: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/21.jpg)
21
Call Stack example and compiled method
def do_thingsputs "Hello
World"end
PutselfPutstring “Hello World”opt_send_simple
call_method
C Stack
exec_core…
main()
opt_send_simple putsVM Stack
“hello World”self
Ruby Call Stack
![Page 22: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/22.jpg)
22
Call Stack example and compiled method
def do_thingsputs "Hello
World"end
PutselfPutstring “Hello World”opt_send_simple
call_method
C Stack
exec_core…
main()
opt_send_simple putsVM Stack
“hello World”self
putsRuby Call Stack
do_things
![Page 23: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/23.jpg)
23
Three Stacks
call_method
C Stack
exec_core…
main()
opt_send_simple putsVM Stack
“hello World”self
putsRuby Call Stack
do_things
![Page 24: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/24.jpg)
24
What does this look like in the Ruby’s VM?
putsRuby Call Stack
do_things
def do_thingsputs "Hello
World"end
![Page 25: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/25.jpg)
25
What does this look like in the Ruby’s VM?
putsRuby Call Stack
do_things
def do_thingsputs "Hello
World"end
PCControl Frame
SPSelftype
![Page 26: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/26.jpg)
26
What does this look like in the Ruby’s VM?
putsRuby Call Stack
do_things
def do_thingsputs "Hello
World"end
PCControl Frame
SPSelftype
putselfputstring “Hello World”opt_send_simple
![Page 27: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/27.jpg)
27
What does this look like in the Ruby’s VM?
putsRuby Call Stack
do_things
def do_thingsputs "Hello
World"end
PCControl Frame
SPSelftype
putselfputstring “Hello World”opt_send_simple
opt_send_simple putsVM Stack
“hello World”self
![Page 28: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/28.jpg)
28
Class lookup? What happens when you want to create an object?
class Person
end
p = Person.new()
![Page 29: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/29.jpg)
29
Class lookup? What happens when you want to create an object?class Person
end
p = Person.new()
![Page 30: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/30.jpg)
30
Class lookup? What happens when you want to create an object?
class Person
end
p = Person.new()
Person = "Craig"
#Warning: already initialized constant person
![Page 31: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/31.jpg)
31
Intro to garbage collectionWhat is garbage collection?Different types of garbage collection algorithmsMark & Sweep demo
![Page 32: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/32.jpg)
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
![Page 33: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/33.jpg)
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.
![Page 34: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/34.jpg)
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?
![Page 35: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/35.jpg)
35
Mark and Sweep Demo
![Page 36: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/36.jpg)
36
Mark and Sweep Demo
![Page 37: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/37.jpg)
37
Mark and Sweep Demo
![Page 38: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/38.jpg)
38
Mark and Sweep Demo
![Page 39: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/39.jpg)
39
Mark and Sweep Demo
![Page 40: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/40.jpg)
40
Mark and Sweep Demo
![Page 41: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/41.jpg)
41
Mark and Sweep Demo
![Page 42: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/42.jpg)
42
Mark and Sweep Demo
![Page 43: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/43.jpg)
43
Object Finalization
• Some objects may have operations that need to occur pre/post collection
• e.g. file object
![Page 44: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/44.jpg)
44
Execution Environment
Architecture of a Managed Runtime
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/ASTCompiler
Just-In-Time Compiler
Interpreter
![Page 45: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/45.jpg)
45
Execution Environment
Architecture of a Managed Runtime
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/ASTCompiler
Just-In-Time Compiler
Interpreter
![Page 46: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/46.jpg)
46
Execution Environment
Architecture of a Managed Runtime
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/ASTCompiler
Just-In-Time Compiler
Interpreter
![Page 47: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/47.jpg)
47
Execution Environment
Architecture of a Managed Runtime
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/ASTCompiler
Just-In-Time Compiler
Interpreter
Interpreter context Thread context
Language callstack
![Page 48: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/48.jpg)
48
Execution Environment
Architecture of a Managed Runtime
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/ASTCompiler
Just-In-Time Compiler
Interpreter
![Page 49: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/49.jpg)
49
Execution Environment
Architecture of a Managed Runtime
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/ASTCompiler
Just-In-Time Compiler
Interpreter
![Page 50: Ruby Under The Hood](https://reader030.vdocuments.mx/reader030/viewer/2022021422/58f26c1a1a28abe7698b458b/html5/thumbnails/50.jpg)
50
Execution Environment
Architecture of a Managed Runtime
Platform Abstraction Layer
Garbage Collector
Diagnostic Services
Source Code Bytecode/ASTCompiler
Just-In-Time Compiler
Interpreter