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

Ticket #10677 (closed enhancement: fixed)

Opened 7 months ago

Last modified 7 months ago

[PATCH] Performance improvements for classic fixtures with HABTM data

Reported by: nwilmes Assigned to: bitsweat
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: fixture, habtm, performance
Cc: hasmanyjosh

Description

The following two patches greatly speed up the process of loading join tables used for HABTM relationships, which do not have model classes associated with them.

The original behavior failed in this regard in two ways.

First off, the model_class method of the Fixtures class, which is used over and over again during the fixture loading process, is written to look up the class associated with the fixture file and cache it so that the method can be called cheaply. Unfortunately, it relied on a ||= mechanism that caused the look up to be retried for fixtures without model classes.

Another performance suck was in Fixture.new. Although the model class is calculated in the Fixtures class, only the class name was passed down to the individual Fixture class, and then the model class was re-determined. This is unnecessary if the model class is passed down to the Fixture class instead of the class name.

I was able to cut the test suite running time for the project that exposed these issues from 128 seconds to 47 seconds, using these two fixes.

One thing to note about the second change is that it changes the API for Fixture.new. As a result, plugins that rely on the old API for Fixture.new will no longer work. We could add some protection so that Fixture.new will accept either a model_class or class_name.. in fact, the current Fixture.new shows signs that it did this very thing. I chose not to do this for simplicity's sake.

Attachments

fixture_model_class_improved_caching.diff (0.7 kB) - added by nwilmes on 01/04/08 02:24:02.
instantiate_fixture_with_class_not_name.diff (2.8 kB) - added by nwilmes on 01/04/08 02:24:42.

Change History

01/02/08 23:22:15 changed by bitsweat

  • owner changed from core to bitsweat.

For model_class, you can use defined?(@model_class) for a nil-safe check.

For Fixture.new, backward-compatibility is one line, couldn't hurt.

Thanks for investigating :)

01/03/08 01:25:47 changed by hasmanyjosh

  • cc set to hasmanyjosh.

01/04/08 02:24:02 changed by nwilmes

  • attachment fixture_model_class_improved_caching.diff added.

01/04/08 02:24:42 changed by nwilmes

  • attachment instantiate_fixture_with_class_not_name.diff added.

01/04/08 02:25:58 changed by nwilmes

Got the changes in and resubmitted, yay!

01/05/08 02:20:29 changed by bitsweat

(In [8560]) Cache fixture model class. References #10677 [nwilmes]

01/05/08 02:21:00 changed by bitsweat

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

(In [8561]) Prefer to instantiate fixtures with model classes instead of their names, avoiding excess constant lookups. Closes #10677 [nwilmes]