Permalink
Browse files

make a singleton for AssociationScope

AssociationScope no longer maintains state, so we're safe to keep a
singleton and save on GC time
  • Loading branch information...
1 parent 8e6ef92 commit 213b2fbf40f6a1ce8381749bd5ba734f20bd4b21 @tenderlove tenderlove committed Feb 14, 2014
@@ -94,7 +94,7 @@ def scope
# actually gets built.
def association_scope
if klass
- @association_scope ||= AssociationScope.new.scope(self, klass.connection)
+ @association_scope ||= AssociationScope.scope(self, klass.connection)
end
end
@@ -1,6 +1,12 @@
module ActiveRecord
module Associations
class AssociationScope #:nodoc:
+ INSTANCE = new
+
+ def self.scope(association, connection)
@arthurnn
arthurnn Feb 14, 2014 Member

maybe we need a mutex around this?

+ INSTANCE.scope association, connection
+ end
+
def scope(association, connection)
klass = association.klass
reflection = association.reflection
@@ -6,8 +6,7 @@ module ActiveRecord
module Associations
class AssociationScopeTest < ActiveRecord::TestCase
test 'does not duplicate conditions' do
- association_scope = AssociationScope.new
- scope = association_scope.scope(Author.new.association(:welcome_posts),
+ scope = AssociationScope.scope(Author.new.association(:welcome_posts),
Author.connection)
wheres = scope.where_values.map(&:right)
assert_equal wheres.uniq, wheres

0 comments on commit 213b2fb

Please sign in to comment.