⚡️ Speed up function format_docstring by 5%
#177
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
format_docstringinmlflow/utils/docstring_utils.py⏱️ Runtime :
47.8 microseconds→45.4 microseconds(best of78runs)📝 Explanation and details
The optimized code applies two key performance optimizations:
1. Avoid redundant ParamDocs construction: The original code unconditionally wraps
param_docsinParamDocs(), even if it's already aParamDocsinstance. The optimization adds a type checkif type(param_docs) is not ParamDocs:to only construct a new instance when needed. Usingtype() is notis faster thanisinstance()for single-type checks since it avoids method dispatch overhead.2. Skip processing for None docstrings: The optimization adds a null check
if doc is not None:before callingformat_docstring(). This avoids unnecessary string processing when functions have no docstring, which is common in Python codebases.Performance impact: These optimizations show a 5% speedup (47.8μs → 45.4μs). The line profiler reveals that while the type check adds some overhead (35.2% of time), it prevents the more expensive
ParamDocs()construction in cases where it's not needed. The null check for docstrings is essentially free but saves processing time when applicable.Test case benefits: The optimizations particularly benefit scenarios with:
test_many_params_replacementtest_none_docstringtest_large_docstring_no_placeholdersThis decorator is likely used extensively in MLflow's API documentation generation, making these micro-optimizations valuable for reducing overall import and initialization time across the codebase.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-format_docstring-mhx7vs5rand push.