Permalink
Browse files

speeding up object instantiation by eliminating instance_eval

  • Loading branch information...
1 parent 2221b70 commit ea6305411fa49c0fef831d9f0620dd1b6de7cb7d @tenderlove tenderlove committed Sep 30, 2010
Showing with 21 additions and 15 deletions.
  1. +21 −15 activerecord/lib/active_record/base.rb
@@ -905,21 +905,9 @@ def relation #:nodoc:
# single-table inheritance model that makes it possible to create
# objects of different types from the same table.
def instantiate(record)
- object = find_sti_class(record[inheritance_column]).allocate
-
- object.instance_variable_set(:@attributes, record)
- object.instance_variable_set(:@attributes_cache, {})
- object.instance_variable_set(:@new_record, false)
- object.instance_variable_set(:@readonly, false)
- object.instance_variable_set(:@destroyed, false)
- 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
+ model = find_sti_class(record[inheritance_column]).allocate
+ model.init_with('attributes' => record)
+ model
end
def find_sti_class(type_name)
@@ -1445,6 +1433,24 @@ def initialize_copy(other)
populate_with_current_scope_attributes
end
+ # Initialize an empty model object from +coder+. +coder+ must contain
+ # the attributes necessary for initializing an empty model object. For
+ # example:
+ #
+ # class Post < ActiveRecord::Base
+ # end
+ #
+ # post = Post.allocate
+ # post.init_with('attributes' => { 'title' => 'hello world' })
+ # post.title # => 'hello world'
+ def init_with(coder)
+ @attributes = coder['attributes']
+ @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
+ @new_record = @readonly = @destroyed = @marked_for_destruction = false
+ _run_find_callbacks
+ _run_initialize_callbacks
+ end
+
# Returns a String, which Action Pack uses for constructing an URL to this
# object. The default implementation returns this record's id as a String,
# or nil if this record's unsaved.

0 comments on commit ea63054

Please sign in to comment.