-
-
Save adelcambre/48064 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "rubygems" | |
| require "rbench" | |
| RBench.run(500) do | |
| report("memoed") do | |
| Foo.memoed("hello") | |
| end | |
| report("unmemoed") do | |
| Foo.unmemoed("hello") | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Foo | |
| class << self | |
| memoize do | |
| def memoed(baz) | |
| baz * 1000 | |
| end | |
| end | |
| def unmemoed(baz) | |
| baz * 1000 | |
| end | |
| end | |
| memoize do | |
| def memoed(baz) | |
| baz * 1000 | |
| end | |
| end | |
| def unmemoed(baz) | |
| baz * 1000 | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Memoizer | |
| def method_added(meth) | |
| @_methods_to_memoize ||= [] | |
| @_methods_to_memoize << meth | |
| end | |
| def methods_to_memoize | |
| @_methods_to_memoize | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Memoizable | |
| def self.extended(klass) | |
| klass.class_eval do | |
| def clear_memo!(name, args = nil) | |
| ivar = "@_memoized_#{name}" | |
| if args == nil | |
| instance_variable_set("@_#{name}_is_memoized", false) | |
| instance_variable_set(ivar, nil) | |
| else | |
| instance_variable_get(ivar).delete(args) | |
| end | |
| end | |
| end | |
| end | |
| def memoize(kind = Hash, &blk) | |
| memoizer = Module.new do | |
| extend Memoizer | |
| end | |
| memoizer.class_eval(&blk) | |
| self.send(:include, memoizer) | |
| memoizer.methods_to_memoize.each do |meth| | |
| arity = self.instance_method(meth).arity | |
| ivar = "@_memoized_#{meth}" | |
| if arity == 0 | |
| self.class_eval <<-RUBY | |
| def #{meth} # def formats | |
| return #{ivar} if @_#{meth}_is_memoized # return @_memoized_formats if @_formats_is_memoized | |
| #{ivar} = super # @_memoized_formats = super | |
| @_#{meth}_is_memoized = true # @_formats_is_memoized = true | |
| end # end | |
| RUBY | |
| else | |
| args = arity == 1 ? "args" : "*args" | |
| self.class_eval <<-RUBY | |
| def #{meth}(#{args}) # def find_layout(*args) | |
| #{ivar} ||= #{kind}.new # @_memoized_find_layout ||= Hash.new | |
| if memo = #{ivar}[args] # if memo = @_memoized_find_layout[args] | |
| return memo # return memo | |
| end # end | |
| #{ivar}[args] = super # @_memoized_find_layout[args] = super | |
| end # end | |
| RUBY | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment