S2, "2dsphere", indexes generate keys usingĀ a vector of KeyString::HeapBuilder. Unfortunately, this type is very large (608 bytes), as compared to the previously used BSONObj (16 bytes). This type is large because of the StackBufBuilder used for the TypeBits member, which when not allocated on the stack, adds an additional 512 bytes to the size of this type. Most of this space is unused.
This causes problems for 2dsphere indexes, which when building a compound key where the first field is an array, need to allocate as many KeyString::HeapBuilders as there are elements in the array. If the array is very large, say 1500000 elements, then the minimum memory requirement to generate the keys is close to 900MB.
We should find a way to use a smaller type in this vector, KeyString::Value (24 bytes) for example, and resort to copying it as necessary.
An alternative would be to make TypeBits support the BufBuilder instead of the StackBufBuilder, but that would only reduce the HeapBuilder type to around 96 bytes.