1 bookmark for 2024-03-21

1202.

Interning strings in Go

commaok.xyz/post/intern-strings

In Go, a string is a (possibly empty) immutable sequence of bytes. The critical word here for our purposes is immutable. Because byte slices are mutable, converting between string and []byte generally requires an alloc and copy, which is expensive.

What if such conversions were cached? That would make comparing strings so much faster!: two integer comparisons (len and ptr). This article explains how this approach could be implemented.

A library implementing this:

It looks pretty small and simple. Despite having only 81 stars, it is used by 67.4k GitHub packages. Transitive dependencies. Found it at work accidentally. OpenSource woes.