-
Implementation:
pkg/tui/components/toolcommon/truncate.gofunc WrapLines(text string, width int) []string- Helper used:
takeRunesThatFit(...)(same file) - Uses
lipgloss.Width(...)for width checks and a rune-based splitting loop when wrapping is needed.
-
Benchmarks already exist:
BenchmarkWrapLinesinpkg/tui/components/toolcommon/common_test.go
From the current implementation, WrapLines includes two performance-oriented changes relative to a naive approach:
- Fast path: if
lipgloss.Width(inputLine) <= width, it appends the line without converting to[]rune. - Avoid repeated conversions / scanning: for wrapped lines, converts
inputLineto[]runeonce and then advancesstartusingtakeRunesThatFit, which scans forward and ensures progress.
To confirm what the last commit changed (and whether it actually touched WrapLines), run:
git log -n 20 -- pkg/tui/components/toolcommon/truncate.go pkg/tui/components/toolcommon/common_test.go
git show --stat HEAD
git show HEAD -- pkg/tui/components/toolcommon/truncate.goIf you need a focused diff against the previous commit:
git diff HEAD~1..HEAD -- pkg/tui/components/toolcommon/truncate.go pkg/tui/components/toolcommon/common_test.goThis will tell you whether the last commit modified WrapLines itself, its helpers (takeRunesThatFit, runeWidth), or only added/changed benchmarks.
The repo already has BenchmarkWrapLines. Use it to compare commits.
Run on current commit:
go test ./... -run '^$' -bench 'BenchmarkWrapLines' -benchmem -count 10Recommended: pin CPU and reduce noise (optional, if available on your system):
- Linux:
taskset -c 0 go test ./... -run '^$' -bench 'BenchmarkWrapLines' -benchmem -count 10
- macOS: close background apps; optionally use
GOMAXPROCS=1:GOMAXPROCS=1 go test ./... -run '^$' -bench 'BenchmarkWrapLines' -benchmem -count 10
Option 1: manual checkout + run + record output
# new
git checkout HEAD
go test ./... -run '^$' -bench 'BenchmarkWrapLines' -benchmem -count 10 | tee /tmp/bench_new.txt
# old
git checkout HEAD~1
go test ./... -run '^$' -bench 'BenchmarkWrapLines' -benchmem -count 10 | tee /tmp/bench_old.txtOption 2: use benchstat for an apples-to-apples comparison (recommended)
go install golang.org/x/perf/cmd/benchstat@latest
benchstat /tmp/bench_old.txt /tmp/bench_new.txtThe existing benchmark subcases are:
short_no_wrap,short_wrap,medium,long,multiline
If the reported perf gain/regression is unclear, expand benchmarks (future enhancement):
- Add a case with wide unicode (CJK/emoji) where
lipgloss.Width!= rune count. - Add a case with many lines (e.g., 500–5000 lines) to measure allocation behavior.
(If needed, we can add these benchmark cases later, but first run existing ones across commits.)
ns/op: primary latency metric.allocs/opandB/op: if changes reduced allocations (e.g., avoiding[]runeconversion for the fast path), you should see fewer allocs especially inshort_no_wrap/ lines that fit.- Regressions might show up in cases dominated by
lipgloss.Widthif it’s expensive; watchshort_no_wrapandmultiline.
- Produce:
bench_old.txt(previous commit)bench_new.txt(current commit)benchstatoutput showing the delta
Then decide: “WrapLines is faster/slower by X% in scenario Y”.