J
John Carter
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 : (e-mail address removed)
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.
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 : (e-mail address removed)
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.