From: Robert Feldt Date: 2000-11-02T23:39:58+09:00 Subject: [ruby-talk:5992] Mem overhead of singleton (ProgRuby chapt 19 q) Hi, The "Programming Ruby" book finally arrived here in Sweden yesterday and its a great book. Thanks a lot Dave and Andy! I sincerely hope that this will be the start of the Ruby world domination era (well... :-)). I've been working with class decoration a bit and find your piece on "Class and Module Definitions" interesting. However, my q to you and (possibly other Ruby gurus?) are what effect the "performance enhancement based on Tadayoshi Funaba's data module" has on the mem usage? ie.: class Module private def id_or_name_to_name(idOrName) begin idOrName = idOrName.id2name rescue NameError end idOrName end # Note: Not thread safe?! def eval_once_then_cache(*methods) begin eotc_counter = module_eval "@@__eotc_counter__" rescue NameError eotc_counter = module_eval("@@__eotc_counter__ = 0") end for method in methods method = id_or_name_to_name(method) old_method = "__eotc_oldmethod_#{(eotc_counter += 1)}__" module_eval <<-"end_eval" alias_method :#{old_method}, :#{method} def #{method}(*args, &block) def self.#{method}(*args, &block) @#{old_method} end @#{old_method} = #{old_method}(*args, &block) end end_eval end module_eval "@@__eotc_counter__ = #{eotc_counter}" end end I guess the effect of having a class C with methods "declared" eval_once_then_cache is that each instance will contain their own singleton method returning the cached value => performance enhancement but extra mem used. Is the extra mem significant or will it only be noticed if huge amounts of objects of class C are floating around? Basically this boils down to how many bytes are needed for a (small) singleton method like the one above? What kind of overhead are involved? Regards, Robert