-
Notifications
You must be signed in to change notification settings - Fork 7
β Why keywords arguments will create less garbage collected objects after 2.2?
Benoit Tigeot edited this page Jan 18, 2018
·
1 revision
I asked Akira Matsuda after watching Ruby 2 in Ruby on Rails - RedDotRubyConf 2017 about something he mentions:
Ruby 2 kwargs ... create less garbage collected objects (maybe)
His answer is a gist with an allocation_tracer measure between ruby 2.1.10 and ruby 2.2.7 on different methods with different "type" of params in method.
require 'allocation_tracer'
require 'active_support/core_ext/array/extract_options'
require 'pp'
# Active Support
def foo1(options = {})
options[:x] || 1
end
# Ruby
def foo2(x: 1)
x
end
# Active Support, varargs
def bar1(*args)
options = args.extract_options!
options[:y] || 1
end
# Ruby, varargs
def bar2(*args, y: 1)
y
end
ObjectSpace::AllocationTracer.setup(%i{path line type})
foo1 x: 2
foo2 x: 2
bar1 y: 2
bar2 y: 2
pp activesupport_1: ObjectSpace::AllocationTracer.trace {
foo1 x: 2
}
pp kwargs_1: ObjectSpace::AllocationTracer.trace {
foo2 x: 2
}
pp activesupport_2: ObjectSpace::AllocationTracer.trace {
bar1 y: 2
}
pp kwargs_2: ObjectSpace::AllocationTracer.trace {
bar2 y: 2
}That returns
# ruby 2.1.10p492 (2016-04-01 revision 54464) [x86_64-darwin15.0]
{:activesupport_1=>{["kwargs_allocation.rb", 31, :T_HASH]=>[1, 0, 0, 0, 0, 0]}}
{:kwargs_1=>{["kwargs_allocation.rb", 34, :T_HASH]=>[2, 0, 0, 0, 0, 0]}}
{:activesupport_2=>
{["kwargs_allocation.rb", 38, :T_HASH]=>[1, 0, 0, 0, 0, 0],
["kwargs_allocation.rb", 38, :T_ARRAY]=>[1, 0, 0, 0, 0, 0]}}
{:kwargs_2=>
{["kwargs_allocation.rb", 41, :T_HASH]=>[2, 0, 0, 0, 0, 0],
["kwargs_allocation.rb", 41, :T_ARRAY]=>[1, 0, 0, 0, 0, 0]}}
# ruby 2.2.7p470 (2017-03-28 revision 58194) [x86_64-darwin15]..ruby 2.5.0dev (2017-05-19 trunk 58790) [x86_64-darwin15]
{:activesupport_1=>{["kwargs_allocation.rb", 31, :T_HASH]=>[1, 0, 0, 0, 0, 0]}}
{:kwargs_1=>{}}
{:activesupport_2=>
{["kwargs_allocation.rb", 38, :T_HASH]=>[1, 0, 0, 0, 0, 0],
["kwargs_allocation.rb", 38, :T_ARRAY]=>[2, 0, 0, 0, 0, 0]}}
{:kwargs_2=>{["kwargs_allocation.rb", 41, :T_ARRAY]=>[1, 0, 0, 0, 0, 0]}}