Ruby Under The Hood

51
About me 1 Guelph ES&C Grad – spring 2014 Currently employed at IBM as a Runtime Technologies Software De Twitter: @craiglehmann

Transcript of Ruby Under The Hood

Page 1: Ruby Under The Hood

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

Ruby Under The Hood

http://lolsnaps.com/upload_pic/EveryTimeILookUnderTheHoodOfACar-97946.jpg

Page 3: Ruby Under The Hood

3

Program compilation

Page 4: Ruby Under The Hood

4

Program compilation

Page 5: Ruby Under The Hood

5

Program compilation

Page 6: Ruby Under The Hood

6

Program compilation

3.times do |n|

puts nend

Page 7: Ruby Under The Hood

7

Program compilation

Tokens:3.times do |n|

puts nend

puts

ndo

end

3 dot times

Page 8: Ruby Under The Hood

8

Program compilation

AST3.times do |n|

puts nend

puts n

3 times

call

block

command

Page 9: Ruby Under The Hood

9

Program compilation

Page 10: Ruby Under The Hood

10

The Ruby Interpreter?

The Ruby Interpreter implements a virtual machine.

Page 11: Ruby Under The Hood

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

12

Ruby is a Stack Machine!

Page 13: Ruby Under The Hood

13

Push Down Stack

1 + 2

Page 14: Ruby Under The Hood

14

Push Down Stack

1 + 2 Push 1Push 2opt_plus

Bytecodes:

Page 15: Ruby Under The Hood

15

Push Down Stack

1 + 2 Push 1Push 2opt_plus

Bytecodes:

1Push 1

Page 16: Ruby Under The Hood

16

Push Down Stack

1 + 2 Push 1Push 2opt_plus

Bytecodes:

1Push 1

2Push 2

1

Page 17: Ruby Under The Hood

17

Push Down Stack

1 + 2 Push 1Push 2opt_plus

Bytecodes:

1Push 1

2Push 2

3opt_plus

1

Page 18: Ruby Under The Hood

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

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

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

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

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

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

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

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

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

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

28

Class lookup? What happens when you want to create an object?

class Person

end

p = Person.new()

Page 29: Ruby Under The Hood

29

Class lookup? What happens when you want to create an object?class Person

end

p = Person.new()

Page 30: Ruby Under The Hood

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

31

Intro to garbage collectionWhat is garbage collection?Different types of garbage collection algorithmsMark & Sweep demo

Page 32: Ruby Under The Hood

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

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

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

35

Mark and Sweep Demo

Page 36: Ruby Under The Hood

36

Mark and Sweep Demo

Page 37: Ruby Under The Hood

37

Mark and Sweep Demo

Page 38: Ruby Under The Hood

38

Mark and Sweep Demo

Page 39: Ruby Under The Hood

39

Mark and Sweep Demo

Page 40: Ruby Under The Hood

40

Mark and Sweep Demo

Page 41: Ruby Under The Hood

41

Mark and Sweep Demo

Page 42: Ruby Under The Hood

42

Mark and Sweep Demo

Page 43: Ruby Under The Hood

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

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

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

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

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

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

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

50

Execution Environment

Architecture of a Managed Runtime

Platform Abstraction Layer

Garbage Collector

Diagnostic Services

Source Code Bytecode/ASTCompiler

Just-In-Time Compiler

Interpreter

Page 51: Ruby Under The Hood

Thank You!Twitter: @craigLehmann

Email: [email protected]