Skip to content

Instantly share code, notes, and snippets.

@amatsuda
Last active June 27, 2017 22:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amatsuda/5a9091052ac0625bc85d8a2c67162ed4 to your computer and use it in GitHub Desktop.
Save amatsuda/5a9091052ac0625bc85d8a2c67162ed4 to your computer and use it in GitHub Desktop.
Object Allocation in Ruby kwargs
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
}
# 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]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment