From: John Carter Date: 2006-03-28T06:18:18+09:00 Subject: New Optimization idea. I have a couple of classes like so... class Foo def step1 @mine = Hash.new # Perhaps stuff things into @mine end def step2 @mine.each_pair do |key,value| # Do stuff end end end Profiling with my "new" profiler shows that I'm creating many many Hash objects, probably way more than I need. In fact most of them are empty. Optimization trick... class Object FROZEN_EMPTY_HASH = Hash.new.freeze end class Foo @@hash_cache = Hash.new def step1 @mine = @@hash_cache # Perhaps stuff things into @mine if @mine.empty? @mine = FROZEN_EMPTY_HASH else @@hash_cache = Hash.new end end def step2 @mine.each_pair do |key,value| # Do stuff end end end John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand Carter's Clarification of Murphy's Law. "Things only ever go right so that they may go more spectacularly wrong later." From this principle, all of life and physics may be deduced.