ActiveRecord setter performance

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:

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?

-- 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
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at