Skip to content

Instantly share code, notes, and snippets.

@jpbetz
Last active March 6, 2026 22:51
Show Gist options
  • Select an option

  • Save jpbetz/def850c9b42dd7b94a76157cbff12dbf to your computer and use it in GitHub Desktop.

Select an option

Save jpbetz/def850c9b42dd7b94a76157cbff12dbf to your computer and use it in GitHub Desktop.
Optimizing Kubernetes API `DeepEquals` with Go String Interning

Optimizing Kubernetes API DeepEquals with Go String Interning

Code used: jpbetz/kubernetes@intern-bench

Methodology

This measures performance impact of interning strings (via Go's unique.Make) on DeepEquals.

For each API type, the test:

  1. Fuzzes an object, populating it with strings of a specific length.
  2. Compares the performance of DeepEquals on objects with distinct string allocations versus the exact same objects with interned strings.

Results

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%

String Equality

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 String
s1 == s2
127.7000 Standard byte-by-byte comparison overhead.
Interned String
h1.Value() == h2.Value()
0.9098 Comparing strings generated by unique.Make. Go checks string memory pointers first, skipping the expensive byte scan.
Interned Handle
h1 == h2
0.2440 Comparing unique.Handle[string] types directly. This is the fastest approach, bypassing string header unpacking completely.

Conclusion

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment