Skip to content

⚡️ Speed up function is_documented_by by 7%#131

Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
codeflash/optimize-is_documented_by-mlcx02wm
Open

⚡️ Speed up function is_documented_by by 7%#131
codeflash-ai[bot] wants to merge 1 commit intomainfrom
codeflash/optimize-is_documented_by-mlcx02wm

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Feb 7, 2026

📄 7% (0.07x) speedup for is_documented_by in src/datasets/utils/doc_utils.py

⏱️ Runtime : 173 microseconds 161 microseconds (best of 114 runs)

📝 Explanation and details

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.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 746 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Click to see Generated Regression Tests
import inspect  # used to introspect functions and signatures
# function to test
from typing import Callable

# imports
import pytest  # used for our unit tests
from src.datasets.utils.doc_utils import is_documented_by

def test_basic_decorator_copies_docstring_and_preserves_identity_and_behavior():
    # Define a source function that holds the desired docstring.
    def source():
        """Source docstring: basic usage."""
        return "source"

    # Define a target function that has different behavior and its own docstring.
    def target():
        """Original target docstring."""
        return "target"

    # Apply the decorator manually (as the decorator would).
    codeflash_output = is_documented_by(source); wrapper = codeflash_output # 790ns -> 861ns (8.25% slower)

    decorated = wrapper(target)  # decorate the target function

def test_decorator_used_with_syntax_sets_docstring_on_definition_site():
    # Source function with a distinctive docstring (multi-line to be sure).
    def doc_source():
        """Multi-line
        source docstring with special characters: ' " \\ and unicode \u2603
        End."""
        pass

    # Use decorator syntax to apply the docstring at definition time.
    @is_documented_by(doc_source)
    def new_target():
        return "ok"

def test_decorator_clears_docstring_when_source_has_none_docstring():
    # Source defined without a docstring -> __doc__ is None.
    def docless_source():
        pass

    # Target starts with a non-empty docstring.
    def target_with_doc():
        """I will be cleared."""
        return 42

    # Apply decorator - should set target.__doc__ to None.
    decorated = is_documented_by(docless_source)(target_with_doc) # 612ns -> 468ns (30.8% faster)

def test_decorator_with_multiline_and_special_characters_preserves_exact_text():
    # Create a complex docstring with quotes, newlines, and unicode to ensure exact preservation.
    def complex_source():
        """Line1: "quote"
Line2: 'single-quote'
Line3: backslash \\
Line4: unicode snowman: \u2603
End."""
        pass

    def some_target():
        """placeholder"""
        return None

    decorated = is_documented_by(complex_source)(some_target) # 598ns -> 497ns (20.3% faster)

def test_decorator_works_on_lambda_and_preserves_callable_behavior():
    # Source docstring to apply.
    def source():
        """Lambda source doc."""
        pass

    # Create a lambda function (lambdas can have attributes assigned).
    target_lambda = lambda x: x + 1  # no docstring originally

    # Decorate the lambda.
    decorated_lambda = is_documented_by(source)(target_lambda) # 574ns -> 486ns (18.1% faster)

def test_decorator_on_instance_method_transfers_docstring_to_method_object():
    # Source docstring to be shared with methods.
    def method_source():
        """Shared method docstring."""
        pass

    class C:
        # Apply decorator to an instance method at class definition time.
        @is_documented_by(method_source)
        def my_method(self):
            return "method-result"

    c = C()

def test_decorator_does_not_modify_source_docstring():
    # Ensure that applying the decorator to many targets does not alter the source.
    def source():
        """Immutable source docstring."""
        pass

    def t1():
        pass

    def t2():
        pass

    is_documented_by(source)(t1) # 640ns -> 452ns (41.6% faster)
    is_documented_by(source)(t2) # 283ns -> 237ns (19.4% faster)

def test_wrapper_returns_callable_and_preserves_signature():
    # Ensure the wrapper returned by is_documented_by retains the target function's signature.
    def source():
        """sig source"""
        pass

    def target(a, b=2):
        """target doc"""
        return a + b

    codeflash_output = is_documented_by(source); wrapper = codeflash_output # 764ns -> 858ns (11.0% slower)

    decorated = wrapper(target)

def test_large_scale_decorations_for_many_functions():
    # Large-scale test: decorate many functions (below 1000 to respect constraints).
    N = 500  # sizeable but kept well under the 1000-step recommendation
    def shared_source():
        """shared-large-scale-docstring"""
        pass

    # Build and decorate many distinct functions; ensure docstring assignment and behavior for each.
    targets = []
    for i in range(N):
        # Factory creates a distinct function capturing i.
        def make_target(i):
            def target():
                return i
            return target
        t = make_target(i)
        decorated = is_documented_by(shared_source)(t) # 103μs -> 98.9μs (4.41% faster)
        targets.append(t)

    # Sanity: all decorated functions share the same docstring object/value.
    for t in targets:
        pass
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import inspect
from unittest.mock import MagicMock, Mock

# imports
import pytest
from src.datasets.utils.doc_utils import is_documented_by

class TestBasicFunctionality:
    """Test basic functionality of is_documented_by decorator."""

    def test_copies_docstring_from_source_to_target(self):
        """Test that the decorator copies docstring from source function to target."""
        # Define a function with a docstring
        def source_func():
            """This is the source docstring."""
            pass

        # Define a function without a docstring
        def target_func():
            pass

        # Apply the decorator
        decorated = is_documented_by(source_func)(target_func) # 629ns -> 490ns (28.4% faster)

    def test_decorator_returns_target_function(self):
        """Test that the decorator returns the target function (identity preservation)."""
        def source_func():
            """Source docstring."""
            pass

        def target_func():
            pass

        # Apply the decorator
        decorated = is_documented_by(source_func)(target_func) # 635ns -> 496ns (28.0% faster)

    def test_target_function_behavior_unchanged(self):
        """Test that the decorator does not change the target function's behavior."""
        def source_func():
            """Source docstring."""
            pass

        def target_func(x):
            """Original target docstring."""
            return x * 2

        decorated = is_documented_by(source_func)(target_func) # 565ns -> 520ns (8.65% faster)

    def test_with_multiline_docstring(self):
        """Test that multiline docstrings are copied correctly."""
        def source_func():
            """First line of docstring.
            
            Second line with more details.
            Third line with even more details.
            """
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 658ns -> 527ns (24.9% faster)

    def test_decorator_preserves_function_name(self):
        """Test that the decorator preserves the target function's name."""
        def source_func():
            """Source docstring."""
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 632ns -> 517ns (22.2% faster)

    def test_with_empty_source_docstring(self):
        """Test decorator behavior when source function has empty string docstring."""
        def source_func():
            ""
            pass

        def target_func():
            """Target has docstring."""
            pass

        decorated = is_documented_by(source_func)(target_func) # 609ns -> 502ns (21.3% faster)

    def test_with_source_function_returning_value(self):
        """Test decorator with source function that has return statements."""
        def source_func():
            """Returns nothing."""
            return None

        def target_func(a, b):
            return a + b

        decorated = is_documented_by(source_func)(target_func) # 597ns -> 486ns (22.8% faster)

    def test_decorator_with_lambda_as_target(self):
        """Test that decorator works with lambda functions as target."""
        def source_func():
            """Lambda docstring."""
            pass

        target_lambda = lambda x: x + 1

        decorated = is_documented_by(source_func)(target_lambda) # 601ns -> 518ns (16.0% faster)

class TestEdgeCases:
    """Test edge cases and unusual scenarios."""

    def test_source_with_none_docstring(self):
        """Test decorator when source function has None as docstring."""
        def source_func():
            pass

        def target_func():
            """Target docstring."""
            pass

        decorated = is_documented_by(source_func)(target_func) # 620ns -> 503ns (23.3% faster)

    def test_both_functions_with_none_docstring(self):
        """Test decorator when both source and target have None docstring."""
        def source_func():
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 581ns -> 505ns (15.0% faster)

    def test_docstring_with_special_characters(self):
        """Test decorator with docstrings containing special characters."""
        def source_func():
            """Docstring with special chars: !@#$%^&*()_+-=[]{}|;:',.<>?/~`"""
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 641ns -> 494ns (29.8% faster)

    def test_docstring_with_newlines_and_indentation(self):
        """Test decorator with docstrings containing various whitespace."""
        def source_func():
            """First line.
            
                Indented line.
            
            Normal line."""
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 612ns -> 495ns (23.6% faster)

    def test_docstring_with_unicode_characters(self):
        """Test decorator with unicode characters in docstring."""
        def source_func():
            """Unicode test: \u2764 \U0001F600 \u00e9 \u00fc"""
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 634ns -> 508ns (24.8% faster)

    def test_decorator_chaining(self):
        """Test that decorator can be chained or applied multiple times."""
        def source1():
            """First docstring."""
            pass

        def source2():
            """Second docstring."""
            pass

        def target():
            pass

        # Apply first decorator
        decorated_once = is_documented_by(source1)(target) # 629ns -> 527ns (19.4% faster)

        # Apply decorator again to already decorated function
        decorated_twice = is_documented_by(source2)(decorated_once) # 270ns -> 247ns (9.31% faster)

    def test_with_complex_function_signature(self):
        """Test decorator with target function having complex signature."""
        def source_func():
            """Source doc."""
            pass

        def target_func(a, b=10, *args, c=20, **kwargs):
            return a + b + c + sum(args) + sum(kwargs.values())

        decorated = is_documented_by(source_func)(target_func) # 624ns -> 511ns (22.1% faster)

    def test_with_default_arguments(self):
        """Test decorator preserves function with default arguments."""
        def source_func():
            """Doc."""
            pass

        def target_func(x=5, y=10):
            return x + y

        decorated = is_documented_by(source_func)(target_func) # 626ns -> 502ns (24.7% faster)

    def test_with_keyword_only_arguments(self):
        """Test decorator with keyword-only arguments."""
        def source_func():
            """Source documentation."""
            pass

        def target_func(a, *, b=5):
            return a + b

        decorated = is_documented_by(source_func)(target_func) # 619ns -> 528ns (17.2% faster)

    def test_docstring_with_quotes(self):
        """Test decorator with docstrings containing quote characters."""
        def source_func():
            """This docstring has 'single' and "double" quotes."""
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 607ns -> 487ns (24.6% faster)

    def test_very_long_docstring(self):
        """Test decorator with extremely long docstring."""
        long_doc = "X" * 10000
        
        def source_func():
            pass
        source_func.__doc__ = long_doc

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 613ns -> 482ns (27.2% faster)

    def test_docstring_only_whitespace(self):
        """Test decorator with docstring containing only whitespace."""
        def source_func():
            """   
            
            """
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 606ns -> 499ns (21.4% faster)

    def test_with_builtin_function_docstring(self):
        """Test decorator copying from a builtin function."""
        def target_func():
            pass

        # Use a real builtin function's docstring
        decorated = is_documented_by(len)(target_func) # 1.44μs -> 485ns (196% faster)

    def test_decorator_with_method_as_target(self):
        """Test decorator when target is a method."""
        def source_func():
            """Method doc."""
            pass

        class MyClass:
            def target_method(self):
                return "result"

        # Decorate the method
        MyClass.target_method = is_documented_by(source_func)(MyClass.target_method) # 645ns -> 524ns (23.1% faster)

        # Create instance and test
        instance = MyClass()

    def test_docstring_with_trailing_whitespace(self):
        """Test decorator preserves trailing whitespace in docstring."""
        def source_func():
            """Doc with trailing spaces   """
            pass

        def target_func():
            pass

        decorated = is_documented_by(source_func)(target_func) # 615ns -> 500ns (23.0% faster)

    

To edit these changes git checkout codeflash/optimize-is_documented_by-mlcx02wm and push.

Codeflash Static Badge

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.
@codeflash-ai codeflash-ai bot requested a review from aseembits93 February 7, 2026 22:57
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Feb 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants