⚡️ Speed up function is_documented_by by 7%#131
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function is_documented_by by 7%#131codeflash-ai[bot] wants to merge 1 commit intomainfrom
is_documented_by by 7%#131codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
This optimization achieves a **7% runtime improvement** by hoisting the docstring attribute lookup (`function_with_docstring.__doc__`) outside of the inner `wrapper` function and storing it in a closure variable `doc`. **Key changes:** - The docstring is now retrieved once when `is_documented_by` is called, rather than being accessed via attribute lookup each time the decorator is applied to a target function - The inner `wrapper` function now closes over a simple string reference (`doc`) instead of needing to access the `function_with_docstring` object's `__doc__` attribute **Why this improves runtime:** In Python, attribute access (`.` operator) incurs overhead for dictionary lookups in the object's `__dict__`. By caching `function_with_docstring.__doc__` into a local variable, we eliminate this repeated attribute lookup. When the decorator is applied to multiple functions (as shown in `test_large_scale_decorations_for_many_functions` and `test_decorator_does_not_modify_source_docstring`), this optimization compounds - each application of the wrapper saves an attribute access operation. **Performance characteristics:** The test results show consistent speedups across various use cases: - **Single applications**: 16-42% faster when decorating individual functions - **Multiple applications**: 19-42% faster when the same source docstring is reused across multiple targets (e.g., `test_decorator_does_not_modify_source_docstring`) - **Large-scale usage**: 4.4% faster in `test_large_scale_decorations_for_many_functions` with 500 function decorations **Impact on workloads:** While the provided `function_references` shows only one usage in `align_labels_with_mapping`, this optimization is particularly valuable in codebases that: 1. Use the decorator pattern extensively to share documentation across many functions 2. Define multiple decorated functions at module import time (decorator overhead affects startup time) 3. Apply the same source docstring to numerous target functions The optimization is most effective when `is_documented_by` is called multiple times with the same source function, as each wrapper application benefits from the cached docstring lookup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 7% (0.07x) speedup for
is_documented_byinsrc/datasets/utils/doc_utils.py⏱️ Runtime :
173 microseconds→161 microseconds(best of114runs)📝 Explanation and details
This optimization achieves a 7% runtime improvement by hoisting the docstring attribute lookup (
function_with_docstring.__doc__) outside of the innerwrapperfunction and storing it in a closure variabledoc.Key changes:
is_documented_byis called, rather than being accessed via attribute lookup each time the decorator is applied to a target functionwrapperfunction now closes over a simple string reference (doc) instead of needing to access thefunction_with_docstringobject's__doc__attributeWhy this improves runtime:
In Python, attribute access (
.operator) incurs overhead for dictionary lookups in the object's__dict__. By cachingfunction_with_docstring.__doc__into a local variable, we eliminate this repeated attribute lookup. When the decorator is applied to multiple functions (as shown intest_large_scale_decorations_for_many_functionsandtest_decorator_does_not_modify_source_docstring), this optimization compounds - each application of the wrapper saves an attribute access operation.Performance characteristics:
The test results show consistent speedups across various use cases:
test_decorator_does_not_modify_source_docstring)test_large_scale_decorations_for_many_functionswith 500 function decorationsImpact on workloads:
While the provided
function_referencesshows only one usage inalign_labels_with_mapping, this optimization is particularly valuable in codebases that:The optimization is most effective when
is_documented_byis called multiple times with the same source function, as each wrapper application benefits from the cached docstring lookup.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-is_documented_by-mlcx02wmand push.