1. 31

    1. 2

      I prefer to use string interning.

      1. 3

        String interning has a very different set of tradeoffs and is often not what you need.

        1. You need some form of garbage collection if you don’t want your intern table to grow forever.
        2. Interning doesn’t help when your strings are often different (for example taking substrings).
        3. Interning still requires allocations for small strings.
        4. The cost of interning a string is typically much more expensive than SSO or referencing a substring. (But usually cheaper than an allocation.)

        For things that I typically do interning provides little benefit, but SSO and substring sharing can help a lot.

        Interning does have advantages:

        1. Comparing interned strings is very cheap.
        2. For code where the same string is common from different places it can reduce memory and allocations. (Maybe parsing objects of same JSON schema over and over again? But even then borrowing will be better if the lifetime is short lived.)
    2. 2

      Neat! I love how the integer endianness is exploited to be able to store a small string contiguously. I wonder, would swapping the order of prt and len be enough for this trick to also work on big endian architectures?