Skip to content

Commit

Permalink
use instance_eval, reduce method calls.
Browse files Browse the repository at this point in the history
execution time in seconds for `rake test_sqlite3`:

ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
  - before: 52.771426, 52.159505, 52.937056
  - after: 51.452584, 51.897017, 51.584431

rubinius 1.0.1 (1.8.7 release 2010-06-03 JI) [x86_64-apple-darwin10.4.0]
  - before: 284.334076
  - after: 285.753028

ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
  - before: 75.811548, 75.533153, 75.353122
  - after: 75.244243, 75.19226, 75.256925
  • Loading branch information
stevegraham authored and josevalim committed Sep 20, 2010
1 parent dfac9b1 commit 83ecd03
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions activerecord/lib/active_record/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -884,21 +884,13 @@ def relation #:nodoc:
# single-table inheritance model that makes it possible to create # single-table inheritance model that makes it possible to create
# objects of different types from the same table. # objects of different types from the same table.
def instantiate(record) def instantiate(record)
object = find_sti_class(record[inheritance_column]).allocate find_sti_class(record[inheritance_column]).allocate.instance_eval do

@attributes, @attributes_cache, @previously_changed, @changed_attributes = record, {}, {}, {}
object.instance_variable_set(:@attributes, record) @new_record = @readonly = @destroyed = @marked_for_destruction = false
object.instance_variable_set(:@attributes_cache, {}) _run_find_callbacks
object.instance_variable_set(:@new_record, false) _run_initialize_callbacks
object.instance_variable_set(:@readonly, false) self
object.instance_variable_set(:@destroyed, false) end
object.instance_variable_set(:@marked_for_destruction, false)
object.instance_variable_set(:@previously_changed, {})
object.instance_variable_set(:@changed_attributes, {})

object.send(:_run_find_callbacks)
object.send(:_run_initialize_callbacks)

object
end end


def find_sti_class(type_name) def find_sti_class(type_name)
Expand Down

3 comments on commit 83ecd03

@maccman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff Steve!

@dmathieu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.

@stevegraham
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks chaps!

Please sign in to comment.