Slide 1

Slide 1 text

Compacting GC for MRI Version 2?

Slide 2

Slide 2 text

HELLO!!!

Slide 3

Slide 3 text

Aaron Patterson @tenderlove

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

I have cat stickers!

Slide 8

Slide 8 text

G GitHub

Slide 9

Slide 9 text

Ruby && Rails

Slide 10

Slide 10 text

10 Years On Ruby Core! W ow !!!

Slide 11

Slide 11 text

presentation << "joke"

Slide 12

Slide 12 text

Compacting GC for MRI

Slide 13

Slide 13 text

~3 years to complete

Slide 14

Slide 14 text

What is Compaction?

Slide 15

Slide 15 text

Compaction Allocated Memory Computer Memory Allocated Memory Allocated Memory Free Memory Free Memory

Slide 16

Slide 16 text

Compaction Allocated Memory Computer Memory Allocated Memory Allocated Memory Free Memory Free Memory

Slide 17

Slide 17 text

Compaction Allocated Memory Computer Memory Free Memory

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Why Compact?

Slide 20

Slide 20 text

Efficient Memory Usage

Slide 21

Slide 21 text

Efficient Memory Usage Allocated Memory Computer Memory Allocated Memory Allocated Memory Free Memory Free Memory Want To Allocate

Slide 22

Slide 22 text

Efficient Memory Usage Allocated Memory Computer Memory Allocated Memory Allocated Memory Free Memory Free Memory Want To Allocate

Slide 23

Slide 23 text

Efficient Memory Usage Allocated Memory Computer Memory Allocated Memory Allocated Memory Wanted To Allocate

Slide 24

Slide 24 text

CPU Caches

Slide 25

Slide 25 text

CPU Cache Hits Allocated Memory Computer Memory Allocated Memory Allocated Memory Free Memory Free Memory In CPU Cache

Slide 26

Slide 26 text

CPU Cache Hits Allocated Memory Computer Memory Allocated Memory Allocated Memory Free Memory Free Memory In CPU Cache

Slide 27

Slide 27 text

"Good Locality"

Slide 28

Slide 28 text

CoW Friendliness CoW is "Copy on Write"

Slide 29

Slide 29 text

CoW Friendliness Computer Memory Allocated Memory Allocated Memory Allocated Memory Free Memory Free Memory Allocated Memory Allocated Memory Allocated Memory Free Memory Free Memory Parent Process Child Process

Slide 30

Slide 30 text

CoW Friendliness Computer Memory Allocated Memory Allocated Memory Allocated Memory Free Memory Free Memory Allocated Memory Allocated Memory Allocated Memory Free Memory Free Memory Parent Process Child Process

Slide 31

Slide 31 text

CoW Friendliness Computer Memory Allocated Memory Allocated Memory Allocated Memory Free Memory Free Memory Allocated Memory Allocated Memory Allocated Memory Free Memory Free Memory Parent Process Child Process

Slide 32

Slide 32 text

CoW Friendliness Computer Memory Allocated Memory Allocated Memory Allocated Memory Free Memory Free Memory Allocated Memory Allocated Memory Allocated Memory Free Memory Free Memory Parent Process Child Process

Slide 33

Slide 33 text

Eliminating Fragmentation

Slide 34

Slide 34 text

Fragmented Memory Allocated Memory Computer Memory Allocated Memory Allocated Memory Free Memory Free Memory

Slide 35

Slide 35 text

No Fragmentation Allocated Memory Computer Memory Allocated Memory Allocated Memory Free Memory Free Memory

Slide 36

Slide 36 text

Two Heaps

Slide 37

Slide 37 text

Ruby Heaps System Memory Malloc Heap Ruby’s Object Heap

Slide 38

Slide 38 text

Ruby Heaps System Memory Malloc Heap Ruby’s Object Heap Object.new

Slide 39

Slide 39 text

Ruby Heaps System Memory Malloc Heap Ruby’s Object Heap String.new "The Quick Brown Fox Jumps Over The Lazy Dog"

Slide 40

Slide 40 text

Fragmentation Can Occur in Both Heaps

Slide 41

Slide 41 text

For Malloc Heap: jemalloc

Slide 42

Slide 42 text

For Ruby Heap: GC.compact

Slide 43

Slide 43 text

Ruby’s Heap

Slide 44

Slide 44 text

Ruby Heaps System Memory Malloc Heap Ruby’s Object Heap

Slide 45

Slide 45 text

Ruby’s Heap Layout 40 bytes Each chunk is a "slot" Em pty Filled Empty Filled Moved

Slide 46

Slide 46 text

Ruby’s Heap Layout ~16 kb Contiguous slots make a "page"

Slide 47

Slide 47 text

Ruby’s Heap Layout

Slide 48

Slide 48 text

Ruby’s Heap Layout malloc( malloc( malloc( ) ) )

Slide 49

Slide 49 text

Ruby’s Heap Layout Each slot has a unique address 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Slide 50

Slide 50 text

Compaction Algorithm

Slide 51

Slide 51 text

Two Finger Compaction The Programming Language LISP: Its Operation and Applications (1964)

Slide 52

Slide 52 text

Move Objects Update References

Slide 53

Slide 53 text

Moving Objects A B C D E F Free Scan 1 2 3 4 5 6 7 8 9 10 Empty Filled Moved 4 5

Slide 54

Slide 54 text

Updating References A B C D E F 1 2 3 4 5 6 7 8 9 10 Empty Filled Moved Before Moving Objects

Slide 55

Slide 55 text

Updating References A B C D 5 4 1 2 3 4 5 6 7 8 9 10 Empty Filled F Moved E After Moving Objects

Slide 56

Slide 56 text

Updating References A B C D 5 4 1 2 3 4 5 6 7 8 9 10 Empty Filled F Moved E After Moving Objects

Slide 57

Slide 57 text

Object Movement def compact heap = [ ... ] # many slots left = 0 right = heap.length - 1 while left < right left_slot = heap[left] right_slot = heap[right] if is_empty?(left_slot) && !is_empty?(right_slot) && can_move?(right_slot) swap(left, right) heap[right] = T_MOVED.new(left) # leave forwarding address end while !is_empty?(heap[left]) left += 1 end while is_empty?(heap[right]) || !can_move?(heap[right]) right -= 1 end end end Pointers Met? Copy / Forward Advance "free" Retreat "scan"

Slide 58

Slide 58 text

Reference Updating def update_references heap.each do |slot| next if is_empty?(slot) || is_moved?(slot) slot.references.each_with_index do |child, i| if is_moved?(child) slot.set_reference(i, child.new_location) end end end end How are references stored?

Slide 59

Slide 59 text

Finding References • How do Hashes hold references? • How do Arrays hold references? • How do Objects hold references? • … • … • …

Slide 60

Slide 60 text

Reference Updating static void gc_ref_update_array(rb_objspace_t * objspace, VALUE v) { long i, len; if (FL_TEST(v, ELTS_SHARED)) return; len = RARRAY_LEN(v); if (len > 0) { VALUE *ptr = (VALUE *)RARRAY_CONST_PTR_TRANSIENT(v); for(i = 0; i < len; i++) { UPDATE_IF_MOVED(objspace, ptr[i]); } } } static void gc_ref_update_object(rb_objspace_t * objspace, VALUE v) { uint32_t i, len = ROBJECT_NUMIV(v); VALUE *ptr = ROBJECT_IVPTR(v); for (i = 0; i < len; i++) { UPDATE_IF_MOVED(objspace, ptr[i]); } } static int hash_replace_ref(st_data_t *key, st_data_t *value, st_data_t argp, int existing)

Slide 61

Slide 61 text

Reference Updating

Slide 62

Slide 62 text

Supporting C Extensions

Slide 63

Slide 63 text

Where Are References Stored?

Slide 64

Slide 64 text

Array Array VALUE* Some Object Some Object Some Object

Slide 65

Slide 65 text

Hashes Hash VALUE* Some Object Some Object VALUE* Some Object Some Object Keys Values

Slide 66

Slide 66 text

Strings, Classes, Modules, Symbols, etc

Slide 67

Slide 67 text

GC Can Update All "Known Types"

Slide 68

Slide 68 text

"Known Types" Are Types Implemented by Ruby

Slide 69

Slide 69 text

What About "Unknown Types"?

Slide 70

Slide 70 text

Unknown Types are Types Implemented in C

Slide 71

Slide 71 text

Yajl typedef struct { VALUE builderStack; VALUE parse_complete_callback; int nestedArrayLevel; int nestedHashLevel; int objectsFound; int symbolizeKeys; yajl_handle parser; } yajl_parser_wrapper; C Code (yajl_ext.h) malloc(yajl_parser_wrapper) Ruby Object T_DATA Ruby Object Ruby Object builderStack parse_complete_callback

Slide 72

Slide 72 text

Yajl typedef struct { VALUE builderStack; VALUE parse_complete_callback; int nestedArrayLevel; int nestedHashLevel; int objectsFound; int symbolizeKeys; yajl_handle parser; } yajl_parser_wrapper; C Code (yajl_ext.h) malloc(yajl_parser_wrapper) Ruby Object T_DATA Ruby Object Ruby Object builderStack parse_complete_callback GC: "idk, " MOVED!

Slide 73

Slide 73 text

Yajl Mark Function void yajl_parser_wrapper_mark(void * wrapper) { yajl_parser_wrapper * w = wrapper; if (w) { rb_gc_mark(w->builderStack); rb_gc_mark(w->parse_complete_callback); } } malloc(yajl_parser_wrapper) Ruby Object T_DATA Ruby Object Ruby Object rb_gc_mark(builderStack) rb_gc_mark(parse_complete_callback)

Slide 74

Slide 74 text

Anything Marked With `rb_gc_mark` Cannot Move

Slide 75

Slide 75 text

Pinning Bits 1 2 3 4 5 6 7 8 9 10 Yajl [ ] ? "foo" "bar" ? Address Content Pinned x = [ "foo", "bar" ] y = Yajl.new Ruby Code rb_gc_m ark rb_gc_m ark gc_m ark_no_pin gc_m ark_no_pin

Slide 76

Slide 76 text

Pinning Bits 1 2 3 4 5 6 7 8 9 10 Yajl [ ] ? ? Address Content Pinned x = [ "foo", "bar" ] y = Yajl.new Ruby Code Free Scan "bar" "foo" 4 5 Move Step

Slide 77

Slide 77 text

Pinning Bits 1 2 3 4 5 6 7 8 9 10 Yajl [ ] ? ? Address Content Pinned x = [ "foo", "bar" ] y = Yajl.new Ruby Code 4 5 Reference Update Step "bar" "foo" Update

Slide 78

Slide 78 text

Known Types Use `gc_mark_no_pin`

Slide 79

Slide 79 text

Unknown Types Use `rb_gc_mark`

Slide 80

Slide 80 text

Allowing Movement in C Extensions

Slide 81

Slide 81 text

Compaction Callback "No Pin" Marking New Location Function

Slide 82

Slide 82 text

GC Cannot Update a C Extension

Slide 83

Slide 83 text

C Extension Can Update Itself

Slide 84

Slide 84 text

Compaction Callback static const rb_data_type_t yajl_parser_type = { "Yajl/parser", {yajl_parser_wrapper_mark, yajl_parser_wrapper_free, NULL,}, 0, 0, RUBY_TYPED_FREE_IMMEDIATELY, }; Mark No Compaction Callback static const rb_data_type_t yajl_parser_type = { "Yajl/parser", {yajl_parser_wrapper_mark, yajl_parser_wrapper_free, NULL, yajl_parser_compact}, 0, 0, RUBY_TYPED_FREE_IMMEDIATELY, }; Compact With Compaction Callback Sweep

Slide 85

Slide 85 text

"No Pin" Marking void yajl_parser_wrapper_mark(void * wrapper) { yajl_parser_wrapper * w = wrapper; if (w) { rb_gc_mark(w->builderStack); rb_gc_mark(w->parse_complete_callback); } } No Compaction Support void yajl_parser_wrapper_mark(void * wrapper) { yajl_parser_wrapper * w = wrapper; if (w) { rb_gc_mark_no_pin(w->builderStack); rb_gc_mark_no_pin(w->parse_complete_callback); } } With Compaction Support

Slide 86

Slide 86 text

Compaction Callback void yajl_parser_compact(void *wrapper) { yajl_parser_wrapper * w = wrapper; if (w) { w->builderStack = rb_gc_new_location(w->builderStack); w->parse_complete_callback = rb_gc_new_location(w->parse_complete_callback); } } New Location

Slide 87

Slide 87 text

Known Issue

Slide 88

Slide 88 text

Problem Object Graph Object Implemented in Ruby Object Implemented in C Some Object Automatically Marked!! (gc_mark_no_pin) Not Marked

Slide 89

Slide 89 text

Compaction 1 2 3 4 5 6 7 8 9 10 Ruby Obj C Obj ? 4 5 3

Slide 90

Slide 90 text

Maybe Not Common

Slide 91

Slide 91 text

RubyVM Instruction Sequence ISeq Object (in C) def foo "bar" end Code Mark Array (Ruby) "bar" Marked Marked NOT Marked

Slide 92

Slide 92 text

"Direct Marking" in Ruby 2.6 ISeq Object (in C) def foo "bar" end Code "bar" Marked https://bugs.ruby-lang.org/issues/14370

Slide 93

Slide 93 text

MsgPack Object Implemented in Ruby Object Implemented in C Some Object Automatically Marked!! (gc_mark) Not Marked

Slide 94

Slide 94 text

Pure Ruby Shouldn’t Crash https://github.com/msgpack/msgpack-ruby/issues/133

Slide 95

Slide 95 text

If you hold a reference, you must mark the reference

Slide 96

Slide 96 text

More Challenges Object#object_id

Slide 97

Slide 97 text

Direct Memory Access Prevents Movement

Slide 98

Slide 98 text

object_id is based on location 1 2 3 4 5 6 7 8 9 10 Ruby Obj Ruby Obj Ruby Obj 5 object#object_id => 1 object#object_id => 2 object#object_id => 9 object#object_id => ?

Slide 99

Slide 99 text

Object ID After Move x = Object.new GC.compact x.object_id 1 2 3 4 X x.object_id => 1 Heap

Slide 100

Slide 100 text

Object ID After Move x = Object.new x.object_id GC.compact x.object_id 1 2 3 4 X x.object_id => 4 Heap

Slide 101

Slide 101 text

"Seen" Object IDs $seen_object_id = {} class Object def object_id $seen_object_id[memory_location] ||= memory_location end end

Slide 102

Slide 102 text

Object ID After Move x = Object.new x.object_id GC.compact x.object_id 1 2 3 4 X Heap Object ID Table Memory Location Object ID 4 4 Updated Object ID Table Memory Location Object ID 1 4

Slide 103

Slide 103 text

Object ID Collisions x = Object.new x.object_id GC.compact x.object_id y = Object.new y.object_id 1 2 3 4 X Heap Object ID Table Memory Location Object ID 4 4 Updated Object ID Table Memory Location Object ID 1 4 y.object_id => ??? x.object_id => 4 Y

Slide 104

Slide 104 text

Collision Resolution $seen_object_id = {} $location_to_object_id = {} class Object def object_id id = memory_location while $seen_object_id[id] id += 1 end $seen_object_id[id] = id $location_to_object_id[memory_location] = id end end

Slide 105

Slide 105 text

Object ID Collisions x = Object.new x.object_id GC.compact x.object_id y = Object.new y.object_id 1 2 3 4 X Heap Object ID Table Memory Location Object ID 4 4 Updated Object ID Table Memory Location Object ID 1 4 y.object_id => 5 x.object_id => 4 Y Updated Object ID Table- Memory Location Object ID 1 4 4 5

Slide 106

Slide 106 text

GC Cleanup $seen_object_id = {} $location_to_object_id = {} def free_obj(obj) if $location_to_object_id[obj.memory_location] id = $location_to_object_id.delete(obj.memory_location) $seen_object_id.delete(id) end end

Slide 107

Slide 107 text

Don’t Use Object ID!

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

Compaction Impact Patch Results

Slide 110

Slide 110 text

Basic Rails Application Before Compaction

Slide 111

Slide 111 text

Basic Rails Application After Compaction

Slide 112

Slide 112 text

GitHub Before Compaction After Compaction

Slide 113

Slide 113 text

Future Plans

Slide 114

Slide 114 text

Performance Improvements

Slide 115

Slide 115 text

Full GC Move Objects Update References Full GC

Slide 116

Slide 116 text

Sliding Compaction

Slide 117

Slide 117 text

Sliding Compaction 1 2 3 4 5 6 7 8 9 10 Yajl [ ] ? "foo" "bar" ? Address Content

Slide 118

Slide 118 text

Better Locality

Slide 119

Slide 119 text

Supports Variable Widths

Slide 120

Slide 120 text

Variable Width Allocation

Slide 121

Slide 121 text

END http://bugs.ruby-lang.org/issues/15626