I use all over my code std::string objects, most of them stack allocated. I replaced default new/delete operators to check the average memory allocation size and I was surprised to see that most allocations come from std::string and, this is very important, they are between 12 and 32 bytes! To optimize this(large number of small strings allocated from the free storage) I came with a simple solution:
- implement custom string class
- add predefined buffer in it with fixed size, for example 64 bytes(I can adjust this size after profiling)
- use predefined buffer when the length of the string is less than 64 bytes or allocate new buffer from the free storage for larger strings.
With this implementation I will add 64 bytes memory overhead to all my strings with length > 64 add also (64-lenth) overhead to strings shorter than 64 bytes, but I will prevent all the allocations for small strings.
Did you tried such approach? Is it resonable to add this memory overhead, most of the time on the stack to save allocation time/memory fragmentation?
Is this approach better than custom str::string allocators?