Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11108 (closed enhancement: fixed)

Opened 5 months ago

Last modified 4 months ago

[PATCH] Improve associations performance by using symbol callbacks instead of string callbacks

Reported by: adymo Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

String callbacks (before_save, after_save, before_destroy, etc.) may significantly slow down the Rails application. The problem is that each such callback is evaluated in the context of AR object and because of that each time the proper binding (Binding object) should be created and passed to the eval method. That is not only slow, but takes a lot of memory leading to more garbage collection calls.

Rails creates quite a lot of such string callbacks for associations. Attached patch rewrites all such string callbacks into method callbacks (defined using define_method). This is not only a lot faster but also consistent with other places in associations.rb where define_method is used.

In my case when creating 100 AR objects (each with 6 associations) that saved 52 Megabytes of memory and 700ms.

Please see my article for details: http://blog.pluron.com/2008/02/rails-faster-as.html

Attachments

no_string_callbacks_in_associations.patch (10.5 kB) - added by adymo on 02/13/08 20:08:08.

Change History

02/13/08 20:08:08 changed by adymo

  • attachment no_string_callbacks_in_associations.patch added.

02/13/08 21:20:45 changed by juanjo.bazan

+1 Nice!

02/14/08 07:53:40 changed by rick

  • status changed from new to closed.
  • resolution set to fixed.

(In [8867]) Improve associations performance by using symbol callbacks instead of string callbacks. Closes #11108 [adymo]

03/17/08 22:16:20 changed by orangechicken

Any chance of having adymo's Rails 1.2 patch added to the 1.2-Stable branch?