Issue #2065 has been updated by Simon Chiang.

File cache_benchmark.rb added

Ok, I attached a benchmark that is designed to measure the time required for generating the ancestors array. It does so by comparing 'klass.ancestors.each' vs caching 'klass.ancestor' and iterating 'cache.each' (ie in the second case the ancestors array is only generated once).

Running the attached script I get results like the following:

  % ruby cache_benchmark.rb 
  Benchmark without cache
                            user     system      total        real
  A.value(:one)         0.300000   0.000000   0.300000 (  0.307086)
  B.value(:two)         0.310000   0.000000   0.310000 (  0.305512)
  C.value(:three)       0.310000   0.000000   0.310000 (  0.307264)
 
  Benchmark with cache
                          user     system      total        real
  A.value(:one)         0.240000   0.000000   0.240000 (  0.242152)
  B.value(:two)         0.240000   0.000000   0.240000 (  0.244592)
  C.value(:three)       0.250000   0.000000   0.250000 (  0.243842)
  Array.new             0.050000   0.000000   0.050000 (  0.058072)

This is for 100k iterations, so no it's not a bottleneck but likewise that's not why I make this request. I make the request because it would be significantly faster to not generate the ancestors array each time (~20% in this case, which illustrates what is probably the maximum increase for getting an each_ancestor iterator). Notice that the increase in performance corresponds neatly with the time it take to generate 100k arrays.

I imagine somewhere the in-memory ancestry is iterated to make the ancestors array, right? If so I think exposing that iterator would be helpful to DSLs that implement method-like inheritance.

Also I agree that adding each_* methods for every method that return an array would be ridiculous but I'm only asking for a specific one with a specific purpose ;) 

The module include method may work to the same end -- I need to explore further -- but I still make this request because it would be less technical to use an each_ancestor iterator, and ultimately less error-prone given all the things that ruby can do with including, extending, undefining and redefining constants. Thank you for the suggestion, and for looking at this.



----------------------------------------
http://redmine.ruby-lang.org/issues/show/2065

----------------------------------------
http://redmine.ruby-lang.org