0

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 ?

Amber
  • 1,195
  • 8
  • 26
  • Can't say for certain, but most likely they mean they use the guts of the object itself, like a union of a actual string members and a char array, and they use the char array until the string grows and then switches to use the string members and allocating. – NathanOliver May 26 '20 at 03:05
  • 1
    Sounds like Folly's version of the Small String Optimization (aka SSO): https://stackoverflow.com/questions/10315041/meaning-of-acronym-sso-in-the-context-of-stdstring/10319672#10319672 – Jeremy Friesner May 26 '20 at 03:09

1 Answers1

1

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.

tadman
  • 194,930
  • 21
  • 217
  • 240