's an improved version of the patch.
This should be considerably faster than the previous version. It now requires only Θ(h) queries of the collections database, where the previous one required O(n). (where h=the height of the "collections tree" rooted at the given collection, and n=the number of leaf nodes of the tree).
It won't get any faster now
Let me know...