Code used: jpbetz/kubernetes@intern-bench
This measures performance impact of interning strings (via Go's unique.Make) on DeepEquals.
For each API type, the test:
- Fuzzes an object, populating it with strings of a specific length.
- Compares the performance of
DeepEqualson objects with distinct string allocations versus the exact same objects with interned strings.
| String Length | Distinct (ns/op) | Interned (ns/op) | Speedup |
|---|---|---|---|
| Default (~10B) | ~3,740K | ~3,780K | ~0% |
| 100B | ~3,860K | ~3,700K | ~4% |
| 1KB | ~4,220K | ~3,630K | ~14% |
| 10KB | ~8,590K | ~3,690K | ~57% |
Mostly for my own benefit as a first time user of unique.Make, I wanted to see how Go handles string equality.
This is for 10kb strings:
| Comparison Type | Time (ns/op) | Explanation |
|---|---|---|
Normal Strings1 == s2 |
127.7000 |
Standard byte-by-byte comparison overhead. |
Interned Stringh1.Value() == h2.Value() |
0.9098 |
Comparing strings generated by unique.Make. Go checks string memory pointers first, skipping the expensive byte scan. |
Interned Handleh1 == h2 |
0.2440 |
Comparing unique.Handle[string] types directly. This is the fastest approach, bypassing string header unpacking completely. |
String interning provides no measurable benefit for very short strings, but dramatically accelerates DeepEquals comparisons (up to 57%) for objects containing large, repetitive strings. Where applicable, comparing raw unique.Handle types is the absolute fastest approach.