An unordered group_by implementation that is a lot faster than the ordered version used in ActiveSupport version 2.2.x. This becomes apparent when used with large data sets.
When you have setup gemcutter.org as a gem source, you can install it with:
sudo gem install fast_group_by
When you have setup gems.github.com as a gem source, you can install it with:
sudo gem install Narnach-fast_group_by
If you want tests, benchmarks and the project's history, you can clone the git repo and build the gem yourself:
git clone git://github.com/Narnach/fast_group_by.git cd fast_group_by rake install
ActiveSupport uses OrderedHash in its Enumerable#group_by. In version 2.2.x, it uses nested Arrays to store data. Array#assoc is used to find if keys are already defined. For small Arrays, Array#assoc's linear search is about as fast as Hash#' tree search, but for larger data sets, the difference in lookup speed becomes noticeable. In version 2.3.x, there is a new implementation of OrderedHash that uses Hash instead of Array and is thus a lot faster. Using fast_group_by with ActiveSupport 2.3.x does not make much sense.
The reason why I wrote this is that the OrderedHash version took 66 seconds to group a collection of 50k items. The Hash version took less than 1 second.
Copyright © 2009 Wes Oldenbeuving
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.