Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mass Assignment is slow #134

Open
dnagir opened this issue Sep 18, 2014 · 2 comments
Open

Mass Assignment is slow #134

dnagir opened this issue Sep 18, 2014 · 2 comments

Comments

@dnagir
Copy link

dnagir commented Sep 18, 2014

The mass assignment is almost 3x slower than the "semi-manual" assignment using send.

This becomes a bigger issue as more attributes are added.
We have a model with ~30 attributes which is used very often and becomes a real bottleneck in the app.

The only workaround is not to use the mass-assignment.

Some benchmarks:

                       user     system      total        real
mass assignement   0.890000   0.000000   0.890000 (  0.888365)
assign manually    0.350000   0.000000   0.350000 (  0.350524)

the BM code:

require 'active_attr'
require 'benchmark'

class Info
  include ActiveAttr::Model

  10.times do |n|
    attribute "str#{n}", type: String
  end

  10.times do |n|
    attribute "int#{n}", type: Integer
  end

  10.times do |n|
    attribute "attr#{n}"
  end
end



hash_values = {}
10.times do |n|
  hash_values["str#{n}"] = "string #{n}"
  hash_values["int#{n}"] = n
  hash_values["attr#{n}"] = "another value #{n}"
end



def mass_assign(values)
  Info.new(values)
end

def assign_manually(values)
  i = Info.new
  values.each do |k, v|
    i.send("#{k}=", v)
  end
end


iterations = 5000
Benchmark.bm(16) do |bm|
  bm.report('mass assignement') { iterations.times { mass_assign(hash_values) } }
  bm.report('assign manually')  { iterations.times { assign_manually(hash_values) } }
end
@dnagir
Copy link
Author

dnagir commented Sep 18, 2014

The profiling data should help identifying a possible bottleneck.

screen shot 2014-09-18 at 5 23 38 pm

@dnagir
Copy link
Author

dnagir commented Sep 18, 2014

A bit more info. The performance is even worse when the number of non-assignable attributes is large.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant