String pooling/interning

Does Swift intern its native String storage?

String literals are emitted into the __DATA segment of the binary, dynamically created Strings shorter than 16 bytes are stored inline, Strings that the optimizer can prove don't escape the stack frame they're created in are on the stack, all other Strings are on the heap.

5 Likes

It does not, in the sense you likely mean (it merely deduplicates compile-time constants, like any compiled language).

There are a handful of 3rd party packages for doing so, e.g. StringIntern.

1 Like