⚡️ Speed up function indent by 5%
#380
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.
📄 5% (0.05x) speedup for
indentinpandas/util/_decorators.py⏱️ Runtime :
202 microseconds→192 microseconds(best of250runs)📝 Explanation and details
The optimization improves the
indentfunction through two key changes that reduce computational overhead:1. Efficient string concatenation: Replaced the list construction
"".join(["\n"] + [" "] * indents)with direct string concatenation'\n' + " " * indents. This eliminates the overhead of creating an intermediate list and reduces memory allocations.2. Early return for single-line strings: Added a check
if "\n" not in text: return textto bypass the expensivesplit("\n")andjoin()operations when the input contains no newlines. Single-line strings can be returned immediately without any processing.Why this leads to speedup: The original code always performed string splitting and joining operations regardless of input complexity. The optimization avoids these operations for the common case of single-line strings and uses more efficient string operations for multi-line cases.
Performance impact by use case:
Context relevance: The function is used in pandas' decorator infrastructure (
_decorators.py), particularly for formatting addendum text in docstring decorators. Given that docstring formatting is likely called during module initialization or help text generation, even small improvements can accumulate across pandas' large codebase.The optimization is particularly effective for the common case where addendum text is a single line, which appears to be frequent based on the test results showing the highest speedups for single-line inputs.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-indent-miihwnmzand push.