Re: [Rails-core] ActiveRecord setter performance

47 views
Skip to first unread message

Vipul Amler

unread,
Oct 27, 2012, 3:54:09 PM10/27/12
to rubyonra...@googlegroups.com
On Sat, Oct 27, 2012 at 9:53 PM, Yves Senn <yves...@gmail.com> wrote:
> I recently did a batch-processing operation, which simulated a calculation
> with roughly 80'000 ActiveRecord objects. Since it was just a simulation
> none of the records were actually persisted to the database. The performance
> of the operation was pretty bad and I investigated a bit. I could resolve
> most of the performance issues by replacing the use of ActiveRecord setters
> with instance variables. I was a bit confused though that AR-setters have
> such a huge impact on performance. I created a gist with a test-case that
> replicated my findings: https://gist.github.com/3965012
>

The continuing update of an attribute seems to slow down the actions performed.
A single "data.big_bank_balance += i" leads to calls to
read_attribute[1], write_attribute[2],
column casting{both for read and write}, amongst other things, even if
the value wasn't being persisted.
Since the attribute was being rewritten again and again, it was also
being deleted from the attributes cache.

> I don't know the ActiveRecord source that well and wanted to ask what the
> primary reason for the bad performance is. Is there a way to optimize it?
>

Just a thought, but shouldn't ideally batch actions/ or similar ones
be isolated from "ActiveRecord" , and performed
outside instead of polluting this pattern?

> Cheers,
> -- Yves
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-core/-/jRICuERsao0J.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-co...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-core?hl=en.

[1] https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods/read.rb#L82
[2] https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods/write.rb#L30
Reply all
Reply to author
Forward
0 new messages