From 213b2fbf40f6a1ce8381749bd5ba734f20bd4b21 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 14 Feb 2014 14:23:04 -0800 Subject: [PATCH] make a singleton for AssociationScope AssociationScope no longer maintains state, so we're safe to keep a singleton and save on GC time --- activerecord/lib/active_record/associations/association.rb | 2 +- .../lib/active_record/associations/association_scope.rb | 6 ++++++ .../test/cases/associations/association_scope_test.rb | 3 +-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 2dba33898c..4e46256862 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -94,7 +94,7 @@ module ActiveRecord # 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 diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index cd2911e0a4..1bc998d20c 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -1,6 +1,12 @@ module ActiveRecord module Associations class AssociationScope #:nodoc: + INSTANCE = new + + def self.scope(association, connection) + INSTANCE.scope association, connection + end + def scope(association, connection) klass = association.klass reflection = association.reflection diff --git a/activerecord/test/cases/associations/association_scope_test.rb b/activerecord/test/cases/associations/association_scope_test.rb index f9793277e5..c78b036f53 100644 --- a/activerecord/test/cases/associations/association_scope_test.rb +++ b/activerecord/test/cases/associations/association_scope_test.rb @@ -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