c++stringfolly

What does "in-situ without memory allocation" mean for folly implementation of String?


From Folly documentation

Small strings (<= 23 chars) are stored in-situ without memory allocation.
Medium strings (24 - 255 chars) are stored in malloc-allocated memory and copied eagerly.
Large strings (> 255 chars) are stored in malloc-allocated memory and copied lazily.

Where are these "Small String" stored ?


Solution

  • This means that the cost of std::string already includes 23 characters. Anything more requires additional allocations.

    This means internally the structure looks approximately like:

    struct string {
      // ...
      char internal[23];
      char* external;
    };
    

    This is presumably to make copying short strings very cheap as no heap operations have to be performed, plus it doesn't require any delete calls to clear.