⚡️ Speed up method UploadStats.summarize by 9%
#53
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 9% (0.09x) speedup for
UploadStats.summarizeingoogle/cloud/aiplatform/tensorboard/upload_tracker.py⏱️ Runtime :
169 microseconds→155 microseconds(best of514runs)📝 Explanation and details
The optimized code achieves a 9% speedup through several key optimizations:
1. Precomputed Constants in
readable_bytes_stringThe original code repeatedly computed
2**10,2**20, andfloat()conversions on every call. The optimized version precomputes these as module-level constants (_KB = 1024,_MB = 1024 * 1024,_KB_f,_MB_f), eliminating expensive power operations and float conversions. This optimization is particularly effective sincereadable_bytes_stringis called frequently during summarization.2. Reduced Function Call Overhead in
summarizeThe original code used a conditional expression
self._skipped_summary() if self._skipped_any() else Nonethat always called_skipped_any()and potentially_skipped_summary(). The optimized version inlines the skipped check (if self._num_tensors_skipped or self._num_blobs_skipped) and only calls_skipped_summary()when needed, eliminating one function call per summarize operation.3. Improved Data Structure Construction
Instead of creating an empty list and repeatedly calling
append(), the optimized version constructs thestring_pieceslist directly using a list literal with conditional expressions. This reduces list resize operations and method call overhead.4. Variable Access Optimization
The optimized code uses a local variable
b = bytesinreadable_bytes_stringand moves all computation calculations to the top ofsummarize(), improving CPU cache locality and reducing repeated attribute access.These optimizations show consistent improvements across all test cases, with the most significant gains (15-25%) in simple cases with fewer bytes formatting operations, and smaller but meaningful gains (2-12%) in complex cases with multiple data types and skipped items. The optimizations are particularly effective for high-frequency logging scenarios where
summarize()is called repeatedly.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-UploadStats.summarize-mglqouosand push.