Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8957

Show
Ignore:
Timestamp:
02/29/08 23:16:53 (6 months ago)
Author:
nzkoz
Message:

Improve performance by avoiding named block arguments. Closes #11109 [adymo]

Reapplies [8865] with some fixes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r8922 r8957  
    11*SVN* 
     2 
     3* Perf fix: Avoid the use of named block arguments.  Closes #11109 [adymo] 
    24 
    35* PostgreSQL: support server versions 7.4 through 8.0 and the ruby-pg driver.  #11127 [jdavis] 
  • trunk/activerecord/lib/active_record/associations/association_collection.rb

    r8874 r8957  
    4444 
    4545      # Calculate sum using SQL, not Enumerable 
    46       def sum(*args, &block) 
    47         calculate(:sum, *args, &block) 
     46      def sum(*args) 
     47        if block_given? 
     48          calculate(:sum, *args) { |*block_args| yield(*block_args) } 
     49        else 
     50          calculate(:sum, *args) 
     51        end 
    4852      end 
    4953 
     
    122126      end 
    123127 
    124       def any?(&block) 
     128      def any? 
    125129        if block_given? 
    126           method_missing(:any?, &block) 
     130          method_missing(:any?) { |*block_args| yield(*block_args) } 
    127131        else 
    128132          !empty? 
     
    158162 
    159163      protected 
    160         def method_missing(method, *args, &block
     164        def method_missing(method, *args
    161165          if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) 
    162             super 
     166            if block_given? 
     167              super { |*block_args| yield(*block_args) } 
     168            else 
     169              super 
     170            end 
    163171          else 
    164             @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) } 
     172            @reflection.klass.send(:with_scope, construct_scope) do 
     173              if block_given? 
     174                @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } 
     175              else 
     176                @reflection.klass.send(method, *args) 
     177              end 
     178            end 
    165179          end 
    166180        end 
     
    188202      private 
    189203 
    190         def create_record(attrs, &block
     204        def create_record(attrs
    191205          ensure_owner_is_not_new 
    192206          record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) } 
    193           add_record_to_target_with_callbacks(record, &block) 
    194         end 
    195  
    196         def build_record(attrs, &block) 
     207          if block_given? 
     208            add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } 
     209          else 
     210            add_record_to_target_with_callbacks(record) 
     211          end 
     212        end 
     213 
     214        def build_record(attrs) 
    197215          record = @reflection.klass.new(attrs) 
    198           add_record_to_target_with_callbacks(record, &block) 
     216          if block_given? 
     217            add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } 
     218          else 
     219            add_record_to_target_with_callbacks(record) 
     220          end 
    199221        end 
    200222 
  • trunk/activerecord/lib/active_record/associations/association_proxy.rb

    r8874 r8957  
    121121 
    122122      private 
    123         def method_missing(method, *args, &block
     123        def method_missing(method, *args
    124124          if load_target 
    125             @target.send(method, *args, &block) 
     125            if block_given? 
     126              @target.send(method, *args)  { |*block_args| yield(*block_args) } 
     127            else 
     128              @target.send(method, *args) 
     129            end 
    126130          end 
    127131        end 
  • trunk/activerecord/lib/active_record/associations/has_many_through_association.rb

    r8932 r8957  
    114114 
    115115      # Calculate sum using SQL, not Enumerable 
    116       def sum(*args, &block) 
    117         calculate(:sum, *args, &block) 
     116      def sum(*args) 
     117        if block_given? 
     118          calculate(:sum, *args) { |*block_args| yield(*block_args) } 
     119        else 
     120          calculate(:sum, *args) 
     121        end 
    118122      end 
    119123       
     
    129133 
    130134      protected 
    131         def method_missing(method, *args, &block
     135        def method_missing(method, *args
    132136          if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) 
    133             super 
    134           else 
    135             @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) } 
     137            if block_given? 
     138              super { |*block_args| yield(*block_args) } 
     139            else 
     140              super 
     141            end 
     142          else 
     143            @reflection.klass.send(:with_scope, construct_scope) do 
     144              if block_given? 
     145                @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } 
     146              else 
     147                @reflection.klass.send(method, *args) 
     148              end 
     149            end 
    136150          end 
    137151        end 
  • trunk/activerecord/test/cases/associations/join_model_test.rb

    r8933 r8957  
    291291  end 
    292292 
     293  def test_has_many_array_methods_called_by_method_missing 
     294    assert true, authors(:david).categories.any? { |category| category.name == 'General' } 
     295    assert_nothing_raised { authors(:david).categories.sort } 
     296  end 
     297 
    293298  def test_has_many_going_through_join_model_with_custom_foreign_key 
    294299    assert_equal [], posts(:thinking).authors