Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
@
Kaigi The Future Shape of Ruby Objects 202:1
name: ‘Chris’
age: 36
company: ‘Shopify’
home: ‘Cheshire’
struct RObject {
VALUE klass;
int numiv;
VALUE[] ivptr;
};
struct RClass {
struct st_table *iv_index_tbl; // Hash of Symbo...
VALUE vm_getivar(VALUE obj, IVC inline_cache) {
if (inline_cache && inline_cache->class_serial == obj->klass->class_serial...
def vm_getivar(cache, obj)
if cache.serial == obj.klass.serial && cache.index < obj.numiv
obj.ivptr[cache.index]
else
# sl...
class TwoVariablesObject {
Object var0;
Object var1;
}
def vm_getivar(cache, obj)
if cache.id == obj.getMetaClass().getRea...
name: 0
home: 1
company: 2
age: 3
klass
shape
‘Chris’
‘Cheshire’
‘Shopify’
36
index = obj.shape[:name]
obj[index]
index = obj.shape[:name]
obj[index] = value
name: 0
home: 1
company: 2
age: 3
klass
shape
‘Chris’
‘Cheshire’
‘Shopify’
36
181 name: 0
home: 1
company: 2
age: 3
height...
new_shape = obj.shape.transitions[:add_height]
index = new_shape[:name]
obj.shape = new_shape
obj.resize
obj[index] = value
slow_path unless obj.shape == 0x12345678
obj[4]
def getter
@ivar
End
0x1242ec600: cmp dword ptr [rax*8 + 0xc], expected_shape
0x1242ec60b: jne slow_path
0x1242ec611: mov ...
def setter(value)
@ivar = value
end
0x11fd28a00: cmp dword ptr [rsi*8 + 0xc], expected_shape
0x11fd28a0b: jne slow_path
0x...
def add
@a + @b
end
0x12258d380: cmp dword ptr [rax*8 + 0xc], expected_shape
0x12258d38b: jne slow_path
0x12258d391: mov e...
name: 0
home: 1
company: 2
age: 3
klass
shape
‘Chris’
‘Cheshire’
‘Shopify’
36
181 name: 0
home: 1
company: 2
age: 3
(froze...
0x11fd28a00: cmp dword ptr [rsi*8 + 0xc], expected_shape
0x11fd28a0b: jne slow_path
0x11fd28a11: mov eax, dword ptr [rdx +...
@chrisgseaton
The Future Shape of Ruby Objects
The Future Shape of Ruby Objects
The Future Shape of Ruby Objects
The Future Shape of Ruby Objects
The Future Shape of Ruby Objects
The Future Shape of Ruby Objects
The Future Shape of Ruby Objects
You’ve finished this document.
Download and read it offline.
Upcoming SlideShare
What to Upload to SlideShare
Next
Upcoming SlideShare
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

Share

The Future Shape of Ruby Objects

Download to read offline

TruffleRuby uses an optimisation called object shapes to optimise Ruby. It automatically learns and understands the layout and types, or the shape, of your objects as your code is running and optimises code to work better with those shapes. As the community tries to make MRI faster, it could be time to adopt object shapes there as well. We’ll talk about what TruffleRuby does, how it does it, and the benefits it achieves in practice.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

The Future Shape of Ruby Objects

  1. 1. @ Kaigi The Future Shape of Ruby Objects 202:1
  2. 2. name: ‘Chris’ age: 36 company: ‘Shopify’ home: ‘Cheshire’
  3. 3. struct RObject { VALUE klass; int numiv; VALUE[] ivptr; }; struct RClass { struct st_table *iv_index_tbl; // Hash of Symbol -> index };
  4. 4. VALUE vm_getivar(VALUE obj, IVC inline_cache) { if (inline_cache && inline_cache->class_serial == obj->klass->class_serial) { int index = inline_cache->index; if (obj->type == T_OBJECT) && index < obj->numiv) { return obj->ivptr[index]; } else { // slow path } } else { // slow path } } VALUE vm_setivar(VALUE obj, VALUE val, IVC inline_cache) { if (inline_cache && inline_cache->class_serial == obj->klass->class_serial) { int index = inline_cache->index; if (obj->type == T_OBJECT) && index < obj->numiv) { obj->ivptr[index] = val; } else { // slow path } } else { // slow path } }
  5. 5. def vm_getivar(cache, obj) if cache.serial == obj.klass.serial && cache.index < obj.numiv obj.ivptr[cache.index] else # slow path end end def vm_setivar(cache, obj, val) if cache.serial == obj.klass.serial && cache.index < obj.numiv obj.ivptr[cache.index] = val else # slow path end end
  6. 6. class TwoVariablesObject { Object var0; Object var1; } def vm_getivar(cache, obj) if cache.id == obj.getMetaClass().getRealClass().id obj[cache.index] else # slow path end end
  7. 7. name: 0 home: 1 company: 2 age: 3 klass shape ‘Chris’ ‘Cheshire’ ‘Shopify’ 36
  8. 8. index = obj.shape[:name] obj[index]
  9. 9. index = obj.shape[:name] obj[index] = value
  10. 10. name: 0 home: 1 company: 2 age: 3 klass shape ‘Chris’ ‘Cheshire’ ‘Shopify’ 36 181 name: 0 home: 1 company: 2 age: 3 height: 4 + height
  11. 11. new_shape = obj.shape.transitions[:add_height] index = new_shape[:name] obj.shape = new_shape obj.resize obj[index] = value
  12. 12. slow_path unless obj.shape == 0x12345678 obj[4]
  13. 13. def getter @ivar End 0x1242ec600: cmp dword ptr [rax*8 + 0xc], expected_shape 0x1242ec60b: jne slow_path 0x1242ec611: mov r10d, dword ptr [rax*8 + index]
  14. 14. def setter(value) @ivar = value end 0x11fd28a00: cmp dword ptr [rsi*8 + 0xc], expected_shape 0x11fd28a0b: jne slow_path 0x11fd28a1f: mov qword ptr [rsi*8 + 0x20], r10
  15. 15. def add @a + @b end 0x12258d380: cmp dword ptr [rax*8 + 0xc], expected_shape 0x12258d38b: jne slow_path 0x12258d391: mov esi, dword ptr [rax*8 + index_a] 0x12258d398: mov eax, dword ptr [rax*8 + index_b] 0x12258d39f: mov r10d, eax 0x12258d3a2: add r10d, esi 0x12258d3a5: jo overflow
  16. 16. name: 0 home: 1 company: 2 age: 3 klass shape ‘Chris’ ‘Cheshire’ ‘Shopify’ 36 181 name: 0 home: 1 company: 2 age: 3 (frozen) freeze
  17. 17. 0x11fd28a00: cmp dword ptr [rsi*8 + 0xc], expected_shape 0x11fd28a0b: jne slow_path 0x11fd28a11: mov eax, dword ptr [rdx + index]
  18. 18. @chrisgseaton

TruffleRuby uses an optimisation called object shapes to optimise Ruby. It automatically learns and understands the layout and types, or the shape, of your objects as your code is running and optimises code to work better with those shapes. As the community tries to make MRI faster, it could be time to adopt object shapes there as well. We’ll talk about what TruffleRuby does, how it does it, and the benefits it achieves in practice.

Views

Total views

75

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

0

Shares

0

Comments

0

Likes

0

×