[performance-profiler] Optimize UTF16ToUTF8Bytes ASCII branch via io.ByteWriter fast path
- Dominant language
- Go
- Stars
- 12.7k
- Forks
- 5k
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 364
Description
## Hot Path
`libbeat/common/bytes.go:82-124` (`UTF16ToUTF8Bytes`) is a hot conversion path. In the ASCII branch, writing `out.Write(in[i:i+1])` incurs per-call overhead and allocations in benchmarked usage.
## Profiling Data
**Before:**
```text
go test -run '^$' -bench '^BenchmarkUTF16ToUTF8$' -benchmem -count=3 ./libbeat/common
BenchmarkUTF16ToUTF8-4 5500544 217.6 ns/op 4 B/op 1 allocs/op
BenchmarkUTF16ToUTF8-4 5243319 219.7 ns/op 4 B/op 1 allocs/op
BenchmarkUTF16ToUTF8-4 5571883 219.7 ns/op 4 B/op 1 allocs/op
```
## Proposed Change
Add an ASCII fast path that prefers `io.ByteWriter` when available and only allocates/encodes runes for non-ASCII input.
```diff
diff --git a/libbeat/common/bytes.go b/libbeat/common/bytes.go
@@
- var runeBuf [4]byte
+ bw, hasByteWriter := out.(io.ByteWriter)
@@
+ if v1 < utf8.RuneSelf {
+ if hasByteWriter {
+ _ = bw.WriteByte(byte(v1))
+ } else {
+ out.Write(in[i : i+1])
+ }
+ continue
+ }
+
+ var runeBuf [4]byte
```
## Results
**After:**
```text
go test -run '^$' -bench '^BenchmarkUTF16ToUTF8$' -benchmem -count=3 ./libbeat/common
BenchmarkUTF16ToUTF8-4 8334075 148.6 ns/op 0 B/op 0 allocs/op
BenchmarkUTF16ToUTF8-4 8336353 147.4 ns/op 0 B/op 0 allocs/op
BenchmarkUTF16ToUTF8-4 8359370 147.0 ns/op 0 B/op 0 allocs/op
```
**Improvement:**
- Time: ~32.9% faster (mean 219.0 ns/op -> 147.7 ns/op)
- Memory: 100% reduction (4 B/op -> 0 B/op)
- Allocations: 100% reduction (1 alloc/op -> 0 allocs/op)
## Verification
- `go test ./libbeat/common` passed.
- Behavior is preserved: only ASCII write path changes; existing non-ASCII and surrogate handling remains in place.
## Evidence
- Benchmark command was identical before/after and run against different code states.
- File references:
- Hot path: `libbeat/common/bytes.go:82-124`
- ASCII fast path location: `libbeat/common/bytes.go:95-102`
Duplicate check:
- `is:issue state:open UTF16ToUTF8Bytes libbeat/common/bytes.go` returned no open issues.
- Checked `/tmp/previous-findings.json`: related UTF16 issues are closed, no matching open issue for this optimization.
---
[What is this?](https://ela.st/github-ai-tools) | [From workflow: Performance Profiler](https://github.com/elastic/beats/actions/runs/26295242174)
Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.
> - [x] expires on May 29, 2026, 3:14 PM UTC
Contributor guide
Assessment
This issue has not been assessed yet.