[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Transparent Caching Idiom

beng

12/8/2005 7:05:00 PM

I've made good use of this idiom recently, and wanted to share it.

Once this code is active, any method named with a "_c" suffix is
automatically cached. The method is assumed to follow a simple
contract: It performs no side-effects / mutations, and it's arguments
properly implement "Object.hash".

This is specifically useful for expensive but functional calculations.
Enjoy!

--Ben (b@gimpert.com)

# <code>
class Class

alias old_new new
def new(*args, &block)
Class.defc_wrap(old_new(*args, &block))
end

def Class.defc_wrap(obj)
klass = obj.class
defc_methods = obj.methods.select { |n| n =~ /_c$/ }
defc_methods.each do |name|
meth = obj.method(name)
wrapped_name = Class.defc_next_name(klass)
cache = klass.class_eval("@#{wrapped_name} = {}")
wrapper = Proc.new do |*args_a|
arg_hash = args_a.hash
if cache.has_key?(arg_hash)
cache[arg_hash]
else
cache[arg_hash] = meth.call(*args_a)
end
end
klass.send(:alias_method, wrapped_name, name)
klass.send(:define_method, name, wrapper)
end
obj
end

def Class.defc_next_name(obj)
klass = obj.class
i = 1
i += 1 while obj.respond_to?("_defc_#{i}") ||
obj.instance_variables.member?("@_defc_#{i}")
"_defc_#{i}"
end

end
</code>

4 Answers

Daniel Schierbeck

12/8/2005 8:14:00 PM

0

beng wrote:
> I've made good use of this idiom recently, and wanted to share it.
>
> Once this code is active, any method named with a "_c" suffix is
> automatically cached. The method is assumed to follow a simple
> contract: It performs no side-effects / mutations, and it's arguments
> properly implement "Object.hash".
>
> This is specifically useful for expensive but functional calculations.
> Enjoy!
>
> --Ben (b@gimpert.com)
>
> # <code>
> class Class
>
> alias old_new new
> def new(*args, &block)
> Class.defc_wrap(old_new(*args, &block))
> end
>
> def Class.defc_wrap(obj)
> klass = obj.class
> defc_methods = obj.methods.select { |n| n =~ /_c$/ }
> defc_methods.each do |name|
> meth = obj.method(name)
> wrapped_name = Class.defc_next_name(klass)
> cache = klass.class_eval("@#{wrapped_name} = {}")
> wrapper = Proc.new do |*args_a|
> arg_hash = args_a.hash
> if cache.has_key?(arg_hash)
> cache[arg_hash]
> else
> cache[arg_hash] = meth.call(*args_a)
> end
> end
> klass.send(:alias_method, wrapped_name, name)
> klass.send(:define_method, name, wrapper)
> end
> obj
> end
>
> def Class.defc_next_name(obj)
> klass = obj.class
> i = 1
> i += 1 while obj.respond_to?("_defc_#{i}") ||
> obj.instance_variables.member?("@_defc_#{i}")
> "_defc_#{i}"
> end
>
> end
> </code>
>

Neat. Though it would be cool if the methods that were to be cached were
chosen through a class method rather than a method name suffix.

class Klass
def foo; end
def bar; end
cached_method :foo, :bar
end


Cheers,
Daniel

Gene Tani

12/9/2005 3:26:00 AM

0


Daniel Schierbeck wrote:
> beng wrote:
> > I've made good use of this idiom recently, and wanted to share it.
> >
> > Once this code is active, any method named with a "_c" suffix is
> > automatically cached. The method is assumed to follow a simple
> > contract: It performs no side-effects / mutations, and it's arguments
> > properly implement "Object.hash".
> >
> > This is specifically useful for expensive but functional calculations.
> > Enjoy!
> >
> > --Ben (b@gimpert.com)
> >
> > # <code>
> > class Class
> >
> > alias old_new new
> > def new(*args, &block)
> > Class.defc_wrap(old_new(*args, &block))
> > end
> >
> > def Class.defc_wrap(obj)
> > klass = obj.class
> > defc_methods = obj.methods.select { |n| n =~ /_c$/ }
> > defc_methods.each do |name|
> > meth = obj.method(name)
> > wrapped_name = Class.defc_next_name(klass)
> > cache = klass.class_eval("@#{wrapped_name} = {}")
> > wrapper = Proc.new do |*args_a|
> > arg_hash = args_a.hash
> > if cache.has_key?(arg_hash)
> > cache[arg_hash]
> > else
> > cache[arg_hash] = meth.call(*args_a)
> > end
> > end
> > klass.send(:alias_method, wrapped_name, name)
> > klass.send(:define_method, name, wrapper)
> > end
> > obj
> > end
> >
> > def Class.defc_next_name(obj)
> > klass = obj.class
> > i = 1
> > i += 1 while obj.respond_to?("_defc_#{i}") ||
> > obj.instance_variables.member?("@_defc_#{i}")
> > "_defc_#{i}"
> > end
> >
> > end
> > </code>
> >
>
> Neat. Though it would be cool if the methods that were to be cached were
> chosen through a class method rather than a method name suffix.
>
> class Klass
> def foo; end
> def bar; end
> cached_method :foo, :bar
> end
>
>
> Cheers,
> Daniel
I got this bookmarked, When i get time, i'll have to compare to prior
thread on memozing
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

Robert Klemme

12/9/2005 10:56:00 AM

0

Gene Tani wrote:
> Daniel Schierbeck wrote:
>> beng wrote:
>>> I've made good use of this idiom recently, and wanted to share it.
>>>
>>> Once this code is active, any method named with a "_c" suffix is
>>> automatically cached. The method is assumed to follow a simple
>>> contract: It performs no side-effects / mutations, and it's
>>> arguments properly implement "Object.hash".
>>>
>>> This is specifically useful for expensive but functional
>>> calculations. Enjoy!
>>>
>>> --Ben (b@gimpert.com)
>>>
>>> # <code>
>>> class Class
>>>
>>> alias old_new new
>>> def new(*args, &block)
>>> Class.defc_wrap(old_new(*args, &block))
>>> end
>>>
>>> def Class.defc_wrap(obj)
>>> klass = obj.class
>>> defc_methods = obj.methods.select { |n| n =~ /_c$/ }
>>> defc_methods.each do |name|
>>> meth = obj.method(name)
>>> wrapped_name = Class.defc_next_name(klass)
>>> cache = klass.class_eval("@#{wrapped_name} = {}")
>>> wrapper = Proc.new do |*args_a|
>>> arg_hash = args_a.hash
>>> if cache.has_key?(arg_hash)
>>> cache[arg_hash]
>>> else
>>> cache[arg_hash] = meth.call(*args_a)
>>> end
>>> end
>>> klass.send(:alias_method, wrapped_name, name)
>>> klass.send(:define_method, name, wrapper)
>>> end
>>> obj
>>> end
>>>
>>> def Class.defc_next_name(obj)
>>> klass = obj.class
>>> i = 1
>>> i += 1 while obj.respond_to?("_defc_#{i}") ||
>>> obj.instance_variables.member?("@_defc_#{i}")
>>> "_defc_#{i}"
>>> end
>>>
>>> end
>>> </code>
>>>
>>
>> Neat. Though it would be cool if the methods that were to be cached
>> were chosen through a class method rather than a method name suffix.
>>
>> class Klass
>> def foo; end
>> def bar; end
>> cached_method :foo, :bar
>> end
>>
>>
>> Cheers,
>> Daniel
> I got this bookmarked, When i get time, i'll have to compare to prior
> thread on memozing
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

See also:
http://raa.ruby-lang.org/projec...

Kind regards

robert

Daniel Schierbeck

12/9/2005 6:56:00 PM

0

beng wrote:
> I've made good use of this idiom recently, and wanted to share it.
>
> Once this code is active, any method named with a "_c" suffix is
> automatically cached. The method is assumed to follow a simple
> contract: It performs no side-effects / mutations, and it's arguments
> properly implement "Object.hash".
>
> This is specifically useful for expensive but functional calculations.
> Enjoy!
>
> --Ben (b@gimpert.com)
>
> # <code>
> class Class
>
> alias old_new new
> def new(*args, &block)
> Class.defc_wrap(old_new(*args, &block))
> end
>
> def Class.defc_wrap(obj)
> klass = obj.class
> defc_methods = obj.methods.select { |n| n =~ /_c$/ }
> defc_methods.each do |name|
> meth = obj.method(name)
> wrapped_name = Class.defc_next_name(klass)
> cache = klass.class_eval("@#{wrapped_name} = {}")
> wrapper = Proc.new do |*args_a|
> arg_hash = args_a.hash
> if cache.has_key?(arg_hash)
> cache[arg_hash]
> else
> cache[arg_hash] = meth.call(*args_a)
> end
> end
> klass.send(:alias_method, wrapped_name, name)
> klass.send(:define_method, name, wrapper)
> end
> obj
> end
>
> def Class.defc_next_name(obj)
> klass = obj.class
> i = 1
> i += 1 while obj.respond_to?("_defc_#{i}") ||
> obj.instance_variables.member?("@_defc_#{i}")
> "_defc_#{i}"
> end
>
> end
> </code>

This is a touched-up version of your code.

class Class
alias_method :__new__, :new

def cached_method(*methods)
@cached_methods ||= []
@cached_methods += methods
@cached_methods.uniq!
end

def new(*args, &block)
obj = __new__(*args, &block)
klass = obj.class
@cached_methods ||= []
@cached_methods.each do |name|
meth = obj.method(name)
i = 1
while klass.instance_variables.member?("@_defc_#{i}")
i += 1
end
wrapped_name = "_defc_#{i}"
cache = klass.class_eval("@#{wrapped_name} = {}")
wrapper = Proc.new do |*args_a|
arg_hash = args_a.hash
if cache.has_key?(arg_hash)
cache[arg_hash]
else
cache[arg_hash] = meth.call(*args_a)
end
end
klass.send(:alias_method, wrapped_name, name)
klass.send(:define_method, name, wrapper)
end

return obj
end
end

class Klass
def foo; end
def bar; end
cached_method :foo, :bar
end