Permalink
Browse files

fixing performance regression from 2.3.5 -> 2.3.8

  • Loading branch information...
1 parent 257a29d commit 7b6383f2631aac1b83c5efbf3ffbcb90d3961023 @tenderlove tenderlove committed Jul 15, 2010
@@ -278,7 +278,7 @@ class Base
@@raise_delivery_errors = true
cattr_accessor :raise_delivery_errors
- superclass_delegating_accessor :delivery_method
+ class_attribute :delivery_method
self.delivery_method = :smtp
@@perform_deliveries = true
@@ -1,5 +1,6 @@
require 'yaml'
require 'set'
+require 'active_support/core_ext/class/attribute'
module ActiveRecord #:nodoc:
# Generic Active Record exception class.
@@ -515,7 +516,7 @@ def self.reset_subclasses #:nodoc:
@@timestamped_migrations = true
# Determine whether to store the full constant name including namespace when using STI
- superclass_delegating_accessor :store_full_sti_class
+ class_attribute :store_full_sti_class
self.store_full_sti_class = false
# Stores the default scope for the class
@@ -10,7 +10,7 @@ def initialize (config, adapter_method)
##
# :singleton-method:
# The connection handler
- superclass_delegating_accessor :connection_handler
+ class_attribute :connection_handler
self.connection_handler = ConnectionAdapters::ConnectionHandler.new
# Returns the connection currently associated with the class. This can
@@ -44,7 +44,7 @@ def self.included(base)
base.alias_method_chain :update, :dirty
base.alias_method_chain :reload, :dirty
- base.superclass_delegating_accessor :partial_updates
+ base.class_attribute :partial_updates
base.partial_updates = true
base.send(:extend, ClassMethods)
@@ -813,12 +813,12 @@ def self.included(base)
setup :setup_fixtures
teardown :teardown_fixtures
- superclass_delegating_accessor :fixture_path
- superclass_delegating_accessor :fixture_table_names
- superclass_delegating_accessor :fixture_class_names
- superclass_delegating_accessor :use_transactional_fixtures
- superclass_delegating_accessor :use_instantiated_fixtures # true, false, or :no_instances
- superclass_delegating_accessor :pre_loaded_fixtures
+ class_attribute :fixture_path
+ class_attribute :fixture_table_names
+ class_attribute :fixture_class_names
+ class_attribute :use_transactional_fixtures
+ class_attribute :use_instantiated_fixtures # true, false, or :no_instances
+ class_attribute :pre_loaded_fixtures
self.fixture_table_names = []
self.use_transactional_fixtures = false
@@ -2,3 +2,4 @@
require 'active_support/core_ext/class/inheritable_attributes'
require 'active_support/core_ext/class/removal'
require 'active_support/core_ext/class/delegating_attributes'
+require 'active_support/core_ext/class/attribute'
@@ -0,0 +1,67 @@
+require 'active_support/core_ext/kernel/singleton_class'
@rubys

rubys Jul 16, 2010

Contributor

I'm getting no such file to load -- active_support/core_ext/kernel/singleton_class (LoadError).

Perhaps you meant to require 'active_support/core_ext/object/singleton_class.rb' ?

@johannesh

johannesh Jul 16, 2010

Also see http://ruby-doc.org/core/classes/Kernel.html#M005941

If the file has the extension .rb, it is loaded as a source file; [..]
Otherwise, Ruby tries adding .rb, .so, and so on to the name.

@rubys

rubys Jul 16, 2010

Contributor

johannesh, the problem isn't the extension but the path.

$ git checkout master
Switched to branch 'master'
$ find . -name singleton_class.rb
./activesupport/lib/active_support/core_ext/kernel/singleton_class.rb
$ git checkout 2-3-stable
Switched to branch '2-3-stable'
$ find . -name singleton_class.rb
./activesupport/lib/active_support/core_ext/object/singleton_class.rb
@johannesh

johannesh Jul 16, 2010

I know. All I stated was that the extension is optional.

+require 'active_support/core_ext/module/remove_method'
@ehartmann

ehartmann Jul 16, 2010

This file seems to be missing from the commit active_support/core_ext/module/remove_method.rb

+
+class Class
+ # Declare a class-level attribute whose value is inheritable and
+ # overwritable by subclasses:
+ #
+ # class Base
+ # class_attribute :setting
+ # end
+ #
+ # class Subclass < Base
+ # end
+ #
+ # Base.setting = true
+ # Subclass.setting # => true
+ # Subclass.setting = false
+ # Subclass.setting # => false
+ # Base.setting # => true
+ #
+ # This matches normal Ruby method inheritance: think of writing an attribute
+ # on a subclass as overriding the reader method.
+ #
+ # For convenience, a query method is defined as well:
+ #
+ # Subclass.setting? # => false
+ #
+ # Instances may overwrite the class value in the same way:
+ #
+ # Base.setting = true
+ # object = Base.new
+ # object.setting # => true
+ # object.setting = false
+ # object.setting # => false
+ # Base.setting # => true
+ #
+ # To opt out of the instance writer method, pass :instance_writer => false.
+ #
+ # object.setting = false # => NoMethodError
+ def class_attribute(*attrs)
+ instance_writer = !attrs.last.is_a?(Hash) || attrs.pop[:instance_writer]
+
+ attrs.each do |name|
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def self.#{name}() nil end
+ def self.#{name}?() !!#{name} end
+
+ def self.#{name}=(val)
+ singleton_class.class_eval do
+ remove_possible_method(:#{name})
+ define_method(:#{name}) { val }
+ end
+ end
+
+ def #{name}
+ defined?(@#{name}) ? @#{name} : singleton_class.#{name}
+ end
+
+ def #{name}?
+ !!#{name}
+ end
+ RUBY
+
+ attr_writer name if instance_writer
+ end
+ end
+end
@@ -24,7 +24,7 @@ module Performance
end.freeze
def self.included(base)
- base.superclass_delegating_accessor :profile_options
+ base.class_attribute :profile_options
base.profile_options = DEFAULTS
end
@@ -5,7 +5,7 @@ class Parent
end
class Child < Parent
- superclass_delegating_accessor :some_attribute
+ class_attribute :some_attribute
end
class Mokopuna < Child
@@ -42,7 +42,7 @@ def test_simple_writer_declaration
end
def test_simple_accessor_declaration
- single_class.superclass_delegating_accessor :both
+ single_class.class_attribute :both, :instance_writer => false
# Class should have accessor and mutator
# the instance should have an accessor only
assert single_class.respond_to?(:both)
@@ -52,7 +52,7 @@ def test_simple_accessor_declaration
end
def test_working_with_simple_attributes
- single_class.superclass_delegating_accessor :both
+ single_class.class_attribute :both
single_class.both = "HMMM"
@@ -81,7 +81,7 @@ def test_working_with_simple_mutators
def test_child_class_delegates_to_parent_but_can_be_overridden
parent = Class.new
- parent.superclass_delegating_accessor :both
+ parent.class_attribute :both
child = Class.new(parent)
parent.both = "1"
assert_equal "1", child.both

1 comment on commit 7b6383f

Owner

tenderlove commented on 7b6383f Jul 16, 2010

Sorry about that guys. Rails was picking up files out of my installed gems when I ran the tests. I've backported those two files here: 4ae4828

Please sign in to comment.