This is a follow-up to to this answer, regarding ruby 1.8.7's Symbol#to_proc generating a new proc every invocation.
There seems to be more going on than the answers suggest.
Here's some sample code:
def ctob
h=Hash.new(0)
ObjectSpace.each_object(Object) {|e| h[e.class]+=1 }
h
end
r=(0...1000)
p ctob
r.map(&:to_i)
p ctob
This reveals that about a thousand arrays are being created. This reveals that about a thousand are empty:
c=0; ObjectSpace.each_object(Array){|e| c+=1 if e.empty? }
Another interesting thing is that only one Proc object exists. This suggests that to_proc
is only called once. (Maybe another one would get created if I called map
with a symbol a second time.)
If I change the map call to use a block, these arrays are not created. This also might explain why Andrew Grimm's caching didn't help the benchmarks. Why are these arrays being created?
UPDATE
Apparently a proc created from a Symbol creates an empty array every time it's called.
If I replace the map
line above with
pr=:to_i.to_proc; r.map(&pr)
causes the arrays to be created, but this
pr=proc{|e|e.to_i}; r.map(&pr)
does not. Similar thing happens if I just do pr.call(value).
(When is a proc not a proc?)