⚡️ Speed up function undo by 13%
#43
Open
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.
📄 13% (0.13x) speedup for
undoingradio/themes/builder_app.py⏱️ Runtime :
78.3 microseconds→69.0 microseconds(best of76runs)📝 Explanation and details
The optimization replaces
list(old[1])with*old[1]in the return statement of theundofunction, achieving a 13% speedup.Key change: The original code used
+ list(old[1])to concatenate the tuple elements, while the optimized version uses*old[1](unpacking operator) to expand the tuple elements directly into the list.Why this is faster: The
list()constructor creates an intermediate list object from the tuple, which then gets concatenated with the existing list. The unpacking operator*old[1]directly expands the tuple elements into the list literal[history_var, old[0], *old[1]], eliminating the need for:+)Performance impact: Line profiler shows the optimized return statement takes ~881.8 ns per hit vs ~1276.3 ns in the original (31% faster for that specific line). This optimization is particularly effective for larger tuples, as evidenced by test cases with large data showing 50%+ speedups.
When this optimization shines: The test results show the greatest improvements (20-56%) occur with larger tuples or complex data structures, making this optimization valuable for applications that process substantial amounts of historical data through the undo functionality.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-undo-mhlhp9j1and push.