Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
About me
1
Guelph ES&C Grad – spring 2014
Currently employed at IBM as a
Runtime Technologies Software Dev
Twitter: @craig...
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 virt...
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_si...
Three Stacks
19
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_simple
C Stack VM Stack Ruby...
Call Stack example and compiled method
20
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_si...
Call Stack example and compiled method
21
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_si...
Call Stack example and compiled method
22
def do_things
puts "Hello World"
end
Putself
Putstring “Hello World”
opt_send_si...
Three Stacks
23
call_method
C Stack
exec_core
…
main()
opt_send_simple puts
VM Stack
“hello World”
self
puts
Ruby Call Sta...
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
Contr...
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
Contr...
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
Contr...
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"
#Warni...
Intro to garbage collection
What is garbage collection?
Different types of garbage collection algorithms
Mark & Sweep demo...
32
What is garbage collection?
 Automatic memory management
 Manually managing memory is hard!
 Aggregate freeing memor...
33
Different types of garbage collection algorithms
 Reference Counting
• Keep a count along with each object indicating ...
What does it mean to die
• An object consumes one resource, memory.
• When an object becomes unreachable, it can never be ...
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 Service...
Execution Environment
Architecture of a Managed Runtime
45
Platform Abstraction Layer
Garbage Collector
Diagnostic Service...
Execution Environment
Architecture of a Managed Runtime
46
Platform Abstraction Layer
Garbage Collector
Diagnostic Service...
Execution Environment
Architecture of a Managed Runtime
47
Platform Abstraction Layer
Garbage Collector
Diagnostic Service...
Execution Environment
Architecture of a Managed Runtime
48
Platform Abstraction Layer
Garbage Collector
Diagnostic Service...
Execution Environment
Architecture of a Managed Runtime
49
Platform Abstraction Layer
Garbage Collector
Diagnostic Service...
Execution Environment
Architecture of a Managed Runtime
50
Platform Abstraction Layer
Garbage Collector
Diagnostic Service...
Thank You!
Twitter: @craigLehmann
Email: craigl@ca.ibm.com
Próxima SlideShare
Cargando en…5
×

Ruby Under The Hood

425 visualizaciones

Publicado el

Slides describing high level overview of Ruby internals.

Publicado en: Ingeniería
  • Sé el primero en comentar

  • Sé el primero en recomendar esto

Ruby Under The Hood

  1. 1. About me 1 Guelph ES&C Grad – spring 2014 Currently employed at IBM as a Runtime Technologies Software Dev Twitter: @craiglehmann
  2. 2. Ruby Under The Hood http://lolsnaps.com/upload_pic/EveryTimeILookUnderThe HoodOfACar-97946.jpg
  3. 3. Program compilation 3
  4. 4. Program compilation 4
  5. 5. Program compilation 5
  6. 6. Program compilation 6 3.times do |n| puts n end
  7. 7. Program compilation 7 Tokens: 3.times do |n| puts n end puts ndo end 3 dot times
  8. 8. Program compilation 8 AST 3.times do |n| puts n end puts n 3 times call block command
  9. 9. Program compilation 9
  10. 10. The Ruby Interpreter? The Ruby Interpreter implements a virtual machine. 10
  11. 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. 12 Ruby is a Stack Machine!
  13. 13. Push Down Stack 13 1 + 2
  14. 14. Push Down Stack 14 1 + 2 Push 1 Push 2 opt_plus Bytecodes:
  15. 15. Push Down Stack 15 1 + 2 Push 1 Push 2 opt_plus Bytecodes: 1 Push 1
  16. 16. Push Down Stack 16 1 + 2 Push 1 Push 2 opt_plus Bytecodes: 1 Push 1 2 Push 2 1
  17. 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. 18. Call Stack example and compiled method 18 def do_things puts "Hello World" end Putself Putstring “Hello World” opt_send_simple
  19. 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. 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. 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. 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. 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. 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. 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. 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. 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. 28. Class lookup? What happens when you want to create an object? 28 class Person end p = Person.new()
  29. 29. Class lookup? What happens when you want to create an object? 29 class Person end p = Person.new()
  30. 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. 31. Intro to garbage collection What is garbage collection? Different types of garbage collection algorithms Mark & Sweep demo 31
  32. 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. 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. 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. 35. Mark and Sweep Demo 35
  36. 36. Mark and Sweep Demo 36
  37. 37. Mark and Sweep Demo 37
  38. 38. Mark and Sweep Demo 38
  39. 39. Mark and Sweep Demo 39
  40. 40. Mark and Sweep Demo 40
  41. 41. Mark and Sweep Demo 41
  42. 42. Mark and Sweep Demo 42
  43. 43. Object Finalization • Some objects may have operations that need to occur pre/post collection • e.g. file object 43
  44. 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. 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. 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. 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. 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. 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. 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
  51. 51. Thank You! Twitter: @craigLehmann Email: craigl@ca.ibm.com

×