Is there a container that uses a local buffer for a small number of elements, and uses a heap allocation only when the number of elements exceeds a certain limit? Similar to what most std::string
implementations do.
Background
The container is used in the following (simplified) context:
Foo foo; // some data
vector<HandlerPtr> tagged; // receives "tagged" items
// first pass: over all items in someList
for each(HandlerPtr h in someList)
{
h->HandleFoo(foo); // foo may become tagged or untagged here
if (foo.Tagged())
tagged.push_back(h);
}
for(auto itr=tagged.rbegin(); itr!=tagged.end(); ++itr)
{
// ...
}
This code part has high call frequency, but tagging an item is rather rare, number of items in someContainer
is usually low but unbound. I can't use a preallocated "more global" buffer easily. The goal is to avoid the frequent allocation.
Call Frequency
- Common: no item becomes tagged. std::vector is fine
- Common: only one of a few items become tagged. causes high frequency allocation I want to avoid
- Very rare, but must be supported: someList grows during first pass, number of items not predictable but still low