perf: reduce traffic formatter allocations (#6790)

Co-authored-by: Javier Guerrero <javier.guerrero@github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Javier Guerrero
2026-10-12 02:05:40 -07:00
committed by GitHub
parent d43e0c7ead
commit c321933a54
2 changed files with 8 additions and 4 deletions
+2 -4
View File
@@ -1,8 +1,6 @@
package common package common
import ( import "strconv"
"fmt"
)
// FormatTraffic formats traffic bytes into human-readable units (B, KB, MB, GB, TB, PB). // FormatTraffic formats traffic bytes into human-readable units (B, KB, MB, GB, TB, PB).
func FormatTraffic(trafficBytes int64) string { func FormatTraffic(trafficBytes int64) string {
@@ -14,5 +12,5 @@ func FormatTraffic(trafficBytes int64) string {
size /= 1024 size /= 1024
unitIndex++ unitIndex++
} }
return fmt.Sprintf("%.2f%s", size, units[unitIndex]) return strconv.FormatFloat(size, 'f', 2, 64) + units[unitIndex]
} }
+6
View File
@@ -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)
}
}