ruby/ruby — pull request #444 by HonoreDB (2013-11-12 21:49:37+00:00)
Title: Feature/enum to a using size

`Enumerable#to_a` works by creating an empty array with small capacity, then populating it and expanding the capacity as it goes. For large enumerables, this causes several resizes, which can hurt performance. When an enumerable exposes a `size` method, we can guess that the resulting array's size will usually be equal to the enumerable's size. If we're right, we only have to set capacity once, and if we're wrong, we don't lose anything.

This PR adjusts enum.c's to_a method to take advantage of the size method when it's there. In my tests this makes `Range#to_a` about 10% faster, and doesn't have any significant effect on a vanilla enum with no `size` method. I couldn't find any existing benchmark that this consistently made better or worse.

If you like this idea, this could also be done in other classes with custom `to_a`, like Hash.

(Sorry if this isn't a proper way to submit a patch, by the way. I wasn't sure.)