The code I initially wrote was on the pre-flex code base. So I took that code and made it work for the trunk. So someone should definitely check it out if all the changes I made are the right changes.
I tested this patch out on my local machine and when doing a search (q=:) on an index that holds 10M documents, the searchtime was around 300 ms whereas the same query without the code changes had a searchtime of around 2.8 seconds. So that is +/- 9 times faster. These numbers are based on a basic search, so no facets or highlighting etc.
I found out that the following piece of code took relatively a lot time to execute (if it was executed millions and millions of times, you started to notice):
This fragment is used in the TopGroupCollector and Phase2GroupCollector. I put some code in front of it the easily exclude documents that are not competitive. This code in both classes is cheaper then using the fragment above.
Since I ported the code from pre-flex code I needed to make some changes to it and support grouping by function. The code I initially wrote only needed to support grouping on a field. Since the trunk also supports grouping by function query, I added two methods to DocValues and implemented these methods in three subclasses. I don't know if this particular change is good, but it works. I think that it would be really helpful is someone can give feedback on this particular change.