From ca79c19711bb6cf67bb95dbdbb4fcdc5f253aa1d Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Mon, 20 Oct 2025 23:06:53 +0000 Subject: [PATCH] Optimize convert_bytes The optimization replaces the slower `"{:.1f} {}".format(num, unit)` string formatting method with the faster f-string syntax `f"{num:.1f} {unit}"`. This single change results in a 7% overall speedup. **Key Performance Improvement:** - F-strings are significantly faster than `.format()` method calls because they're evaluated at compile-time rather than runtime, avoiding the overhead of method dispatch and argument parsing that `.format()` requires. **Why This Works:** The line profiler shows that the string formatting line (line with the return statement) drops from 497.8ns per hit to 452.3ns per hit - a 9% improvement on that specific operation. Since this line is executed 12,317 times in the profiling run, this micro-optimization compounds to meaningful performance gains. **Test Case Performance:** The optimization shows consistent improvements across all test scenarios: - **Best gains** on simple conversions like single bytes and boolean inputs (up to 42% faster for `True` input) - **Consistent gains** across different unit scales (KB, MB, GB, TB, PB) averaging 5-15% improvement - **Batch operations** benefit significantly, with the large-scale test showing 7.6% improvement when processing 1000 values - Even **edge cases** like negative values and large floats see 3-12% improvements The f-string syntax maintains identical functionality and output format while providing this performance boost through more efficient string interpolation. --- src/together/utils/tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/together/utils/tools.py b/src/together/utils/tools.py index 2e84307a..9c67691b 100644 --- a/src/together/utils/tools.py +++ b/src/together/utils/tools.py @@ -101,7 +101,7 @@ def convert_bytes(num: float) -> str | None: """ for unit in ["B", "KB", "MB", "GB", "TB", "PB"]: if num < 1024.0: - return "{:.1f} {}".format(num, unit) + return f"{num:.1f} {unit}" num /= 1024.0 return None