release-22.2: util: avoid allocations when escaping multibyte characters #91873
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport 1/1 commits from #88671 on behalf of @HonoreDB.
/cc @cockroachdb/release
EncodeEscapedChar (which is called in EncodeSQLStringWithFlags) is pretty optimized, but for escaping a multibyte character it was using fmt.FPrintf, which means every multibyte character ended up on the heap due to golang/go#8618. This had a noticeable impact in changefeed benchmarking.
This commit just hand-compiles the two formatting strings that were being used into reasonably efficient go, eliminating the allocs.
Benchmark encoding the first 10000 runes shows a 4x speedup:
Before: BenchmarkEncodeNonASCIISQLString-16 944 1216130 ns/op
After: BenchmarkEncodeNonASCIISQLString-16 3468 300777 ns/op
Release note: None
Release justification: Low-impact backwards-compatible performance tweak.