⚡️ Speed up function _to_table by 10%
#21
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.
📄 10% (0.10x) speedup for
_to_tableinframework/py/flwr/cli/ls.py⏱️ Runtime :
2.13 milliseconds→1.94 milliseconds(best of79runs)📝 Explanation and details
The optimized code achieves a 9% speedup through several micro-optimizations that reduce function call overhead and improve string parsing efficiency:
Key Optimizations:
Method Reference Caching: The code caches
table.add_columnandtable.add_rowasadd_colandappend_rowrespectively. This eliminates repeated attribute lookups during the loop, which is particularly beneficial when processing large run lists (200+ items in the test cases).Constant Pre-loading:
SubStatus.COMPLETEDandSubStatus.FAILEDare stored in local variables (COMPLETED,FAILED) before the loop. This avoids repeated attribute access on theSubStatusclass during status comparisons.Improved String Parsing: Replaced
rsplit(":", maxsplit=1)[-1]withrpartition(":")to extract the sub-status. Therpartitionmethod is more efficient as it returns a 3-tuple directly without creating an intermediate list and indexing operation.Streamlined Tuple Unpacking: The original code unpacked the row tuple across multiple lines and then created an intermediate
formatted_rowtuple. The optimized version unpacks directly in one line and passes arguments directly toappend_row(), eliminating the intermediate tuple creation.Performance Impact:
The line profiler shows the most significant improvement in the
table.add_rowcall (69.1% → 78.1% of total time), indicating that while this operation still dominates execution time, the reduced overhead from method caching and eliminating intermediate variables provides measurable gains. These optimizations are especially effective for the large-scale test cases with 200+ runs, where the cumulative effect of reduced function call overhead becomes substantial.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_to_table-mh166eq4and push.