ruby 1.8.7 to_proc creates empty arrays

这一生的挚爱 提交于 2019-12-08 10:01:00

问题


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?)


回答1:


I think I found the answer.

I looked at activesupport 2.2 and found this as the body of Symbol#to_proc:

Proc.new { |*args| args.shift.__send__(self, *args) }

args is the array. Since each member of the range is passed as a single arg, it gets converted to an array of 1 element. That one element is shifted off, leaving an empty array. So it's not creating empty arrays, it's just leaving them behind after processing the args.

I've also done a test using a 2-arg proc:

[1,2,3,4].inject(&:+)

This leaves behind arrays of 1 element (the original first element is the current sum).

My assumption is that 1.8.7 does something similar. I'm curious to know how 1.9 does it differently.



来源:https://stackoverflow.com/questions/11781800/ruby-1-8-7-to-proc-creates-empty-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!