The document provides an overview of how Ruby programs are compiled and executed. It discusses how Ruby source code is tokenized and turned into an abstract syntax tree (AST) before being compiled into bytecode. It then describes how the Ruby interpreter implements a virtual machine that maps bytecode instructions to native operations. Key aspects covered include Ruby using a stack-based execution model, the interaction between the C stack, virtual machine stack, and Ruby call stack, and how garbage collection works through mark and sweep to reclaim unused memory.
1 of 51
Download to read offline
More Related Content
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
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
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
24. What does this look like in the Rubys VM?
24
puts
Ruby Call Stack
do_things
def do_things
puts "Hello World"
end
25. What does this look like in the Rubys 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 Rubys 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 Rubys 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 its memory to a list for reuse.
What about when an object consumes more than just one resource?
34