cli-runtime/printers: optimize table printing for large result sets
Reduce allocations and CPU time in printTable by: 1. Buffering each row into a reusable []byte slice and writing once, eliminating ~3 fmt.Fprint calls per cell (tab, value, truncation). 2. Adding a fast path in WriteEscaped that skips the strings.Replacer when the string contains no characters needing escaping (>99% of cells). 3. Flushing the tabwriter every 100 rows. With RememberWidths enabled, the tabwriter retains column widths across flushes, producing consistently aligned output while bounding memory from O(rows) to O(1). Benchmark results (AMD Ryzen 9 7950X3D, go1.24): name old time/op new time/op delta TablePrinter_10kRows-32 7.71ms 2.57ms -66.7% TablePrinter_100kRows-32 77.4ms 25.6ms -66.9% TablePrinter_500kRows-32 389ms 129ms -66.9% name old allocs/op new allocs/op delta TablePrinter_10kRows-32 110,066 136 -99.88% TablePrinter_100kRows-32 1,100,091 136 -99.988% TablePrinter_500kRows-32 5,500,110 138 -99.997% name old bytes/op new bytes/op delta TablePrinter_10kRows-32 7,134,294 44,868 -99.37% TablePrinter_100kRows-32 75,338,014 44,913 -99.94% TablePrinter_500kRows-32 411,596,740 45,153 -99.989% Kubernetes-commit: 50945f3595cc9a4f4ab2820343f2d3c97ce4abaf
D
David Flanagan committed
3eed901c582d90e044f513894849c92c00d74e39
Parent: f1c4b24
Committed by Kubernetes Publisher <k8s-publishing-bot@users.noreply.github.com>
on 3/25/2026, 5:55:19 PM