From c321933a548360124b2f48f6a0feee020b9119cc Mon Sep 17 00:00:00 2001 From: Javier Guerrero <254279148+javi0guerrero4-cloud@users.noreply.github.com> Date: Mon, 12 Oct 2026 02:05:40 -0700 Subject: [PATCH] perf: reduce traffic formatter allocations (#6790) Co-authored-by: Javier Guerrero Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- internal/util/common/format.go | 6 ++---- internal/util/common/format_test.go | 6 ++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/util/common/format.go b/internal/util/common/format.go index c40bd3dca..f75c90bf8 100644 --- a/internal/util/common/format.go +++ b/internal/util/common/format.go @@ -1,8 +1,6 @@ package common -import ( - "fmt" -) +import "strconv" // FormatTraffic formats traffic bytes into human-readable units (B, KB, MB, GB, TB, PB). func FormatTraffic(trafficBytes int64) string { @@ -14,5 +12,5 @@ func FormatTraffic(trafficBytes int64) string { size /= 1024 unitIndex++ } - return fmt.Sprintf("%.2f%s", size, units[unitIndex]) + return strconv.FormatFloat(size, 'f', 2, 64) + units[unitIndex] } diff --git a/internal/util/common/format_test.go b/internal/util/common/format_test.go index d0176e7b8..cc954f6c8 100644 --- a/internal/util/common/format_test.go +++ b/internal/util/common/format_test.go @@ -26,3 +26,9 @@ func TestFormatTraffic(t *testing.T) { }) } } + +func BenchmarkFormatTraffic(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = FormatTraffic(1536 + int64(i%64)*1024) + } +}