diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-08-06 20:21:15 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-08-06 20:21:15 -0500 |
commit | be0d235a3b82e72de49592913753a1f291a98f86 (patch) | |
tree | 658965f439ce04ec01fbe12cfc7b195af7a0bbc3 /activesupport | |
parent | b2504f8ba0f9baadb9298647fd58ef2c136f9aae (diff) | |
download | rails-be0d235a3b82e72de49592913753a1f291a98f86.tar.gz rails-be0d235a3b82e72de49592913753a1f291a98f86.tar.bz2 rails-be0d235a3b82e72de49592913753a1f291a98f86.zip |
Optimize memoized method if there are no arguments
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/memoizable.rb | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index 23dd96e4df..7e2e28712c 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -29,14 +29,24 @@ module ActiveSupport raise "Already memoized #{symbol}" if method_defined?(:#{original_method}) alias #{original_method} #{symbol} - def #{symbol}(*args) - #{memoized_ivar} ||= {} - reload = args.pop if args.last == true || args.last == :reload + if instance_method(:#{symbol}).arity == 0 + def #{symbol}(reload = false) + if !reload && defined? #{memoized_ivar} + #{memoized_ivar} + else + #{memoized_ivar} = #{original_method}.freeze + end + end + else + def #{symbol}(*args) + #{memoized_ivar} ||= {} + reload = args.pop if args.last == true || args.last == :reload - if !reload && #{memoized_ivar} && #{memoized_ivar}.has_key?(args) - #{memoized_ivar}[args] - else - #{memoized_ivar}[args] = #{original_method}(*args).freeze + if !reload && #{memoized_ivar} && #{memoized_ivar}.has_key?(args) + #{memoized_ivar}[args] + else + #{memoized_ivar}[args] = #{original_method}(*args).freeze + end end end EOS |