lotus/model — pull request #52 by splattael (2014-08-05 13:21:46+00:00)
Title: Avoid array allocations for generated entity code

Before this commit the generated entity constructor looked like:

def initialize(attributes = {})
@id, @name, @age, = *attributes.values_at(:id, :name, :age)
end

After this commit the generated code remains its previous semantics:

def initialize(attributes = {})
@id, @name, @age = attributes.values_at(:id, :name, :age)
end

Despite being shorter this generated code also has some performance improvements:

require 'benchmark/ips'

HASH = { :id => 1, :name => "foo", :age => 23 }
id, name, age = nil

Benchmark.ips do |x|
x.report "simple" do
id, name, age = HASH.values_at(:id, :name, :age)
end
x.report "rest" do
id, name, age, = HASH.values_at(:id, :name, :age)
end
x.report "splat + rest" do
id, name, age, = *HASH.values_at(:id, :name, :age)
end
end

=begin

Calculating -------------------------------------
simple 105210 i/100ms
rest 105217 i/100ms
splat + rest 95173 i/100ms
-------------------------------------------------
simple 3235210.9 (±0.8%) i/s - 16202340 in 5.008459s
rest 3224124.5 (±0.7%) i/s - 16203418 in 5.025926s
splat + rest 2729041.1 (±0.7%) i/s - 13704912 in 5.022124s

=end