public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Some performance tweaks to ActiveSupport::Memoizable

Signed-off-by: Joshua Peek <josh@joshpeek.com>
Tarmo Tänav (author)
Mon Jul 14 18:23:23 -0700 2008
josh (committer)
Mon Jul 14 18:23:23 -0700 2008
commit  911c2c381347ffb04615896ee6afe45277eeb103
tree    999da917148986101f8063b9109275535e138563
parent  001c8beb4d0999a858a8b52ad511ee1251cc3517
...
7
8
9
 
 
10
11
12
13
 
14
15
16
...
22
23
24
25
 
26
27
28
...
7
8
9
10
11
12
13
14
 
15
16
17
18
...
24
25
26
 
27
28
29
30
0
@@ -7,10 +7,12 @@ module ActiveSupport
0
     module ClassMethods
0
       def memoize(symbol)
0
         original_method = "_unmemoized_#{symbol}"
0
+        raise "Already memoized #{symbol}" if instance_methods.map(&:to_s).include?(original_method)
0
+
0
         alias_method original_method, symbol
0
         class_eval <<-EOS, __FILE__, __LINE__
0
           def #{symbol}
0
-            if instance_variable_defined?(:@#{symbol})
0
+            if defined? @#{symbol}
0
               @#{symbol}
0
             else
0
               @#{symbol} = #{original_method}
0
@@ -22,7 +24,7 @@ module ActiveSupport
0
 
0
     def freeze
0
       methods.each do |method|
0
-        if m = method.to_s.match(/^_unmemoized_(.*)/)
2
+        if m = method.to_s.match(/\A_unmemoized_(.*)/)
0
           send(m[1]).freeze
0
         end
0
       end
...
41
42
43
 
 
 
 
44
45
...
41
42
43
44
45
46
47
48
49
0
@@ -41,5 +41,9 @@ uses_mocha 'Memoizable' do
0
       person.freeze
0
       assert_equal nil, person.age
0
     end
0
+
0
+    def test_double_memoization
0
+      assert_raise(RuntimeError) { Person.memoize :name }
0
+    end
0
   end
0
 end

Comments

jdance Mon Jul 14 19:24:35 -0700 2008 at activesupport/lib/active_support/memoizable.rb L25

If the goal is performance, you could eliminate the Regexp all together:

if method.to_s[0, 12] == '_unmemoized_' send(method[12, m.length]).freeze end

It’d make a much bigger difference then changing ^ to \A.

jdance Mon Jul 14 19:25:40 -0700 2008 at activesupport/lib/active_support/memoizable.rb L25

Err, m.length should have been method.length.

josh Tue Jul 15 08:30:18 -0700 2008

Whip that up and pull request me