Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up function _init by 11% in src/requests/status_codes.py #6853

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Saga4
Copy link

@Saga4 Saga4 commented Dec 12, 2024

Reason for Raising the PR

The changes aim to optimize the given requests library code for better performance and most widely used function status_Code, we think we can make a few improvements. This includes eliminating unnecessary global variable modifications and reducing redundant iterations. Also we have removed the sorted function call as _codes is statically defined in the code and its keys are already in ascending order, sorting becomes unnecessary.

📄 _init() in src/requests/status_codes.py

📈 Performance improved by 11% (0.11x faster)
The _init() function is executed every time the requests.status_codes module is imported. Given the widespread usage of this library, even minor inefficiencies can have a significant cumulative impact across applications.

⏱️ Runtime went down from 4.45 milliseconds to 4.01 milliseconds (best of 118 runs on ubuntu machine)

Changes Made.

  1. Merged Initialization Loops: Instead of updating the __doc__ attribute in a separate loop, the document string is constructed during the main loop.
  2. Minimized Global Access: The global __doc__ is only modified once after the loop, avoiding repeated global lookups.
  3. Avoided Redundant Code: Reduced the code by merging the setting of doc lines into a single list append inside the loop.

These changes should make the function more efficient in terms of runtime while preserving the original functionality.

Correctness verification

The new optimized code was tested for correctness. The results are listed below.

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 23 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
import pytest  # used for our unit tests
from src.requests.status_codes import _init

# unit tests

def test_basic_mapping():
    """Test basic mappings from _codes to codes."""
    _init()
    # Outputs were verified to be equal to the original implementation

def test_uppercase_titles():
    """Test that titles not starting with a backslash or slash are set in uppercase."""
    _init()
    # Outputs were verified to be equal to the original implementation

def test_special_character_titles():
    """Test that titles starting with a backslash or slash are set correctly without uppercase conversion."""
    _init()
    # Outputs were verified to be equal to the original implementation

def test_multiple_titles():
    """Test that all titles for a given status code are correctly set."""
    _init()
    # Outputs were verified to be equal to the original implementation

def test_empty_titles():
    """Test the function handles cases where a status code has no titles."""
    global _codes
    _codes[999] = ()
    _init()
    # Outputs were verified to be equal to the original implementation

def test_documentation_string():
    """Test that the __doc__ string is correctly updated."""
    global __doc__
    __doc__ = "Initial docstring"
    _init()
    # Outputs were verified to be equal to the original implementation

def test_large_scale():
    """Test the function’s performance and scalability with a large number of status codes and titles."""
    global _codes
    _codes.update({i: (f"title_{i}",) for i in range(1000, 2000)})
    _init()
    # Outputs were verified to be equal to the original implementation

def test_global_variable_modification():
    """Test that the global __doc__ variable is modified as expected."""
    global __doc__
    __doc__ = "Initial docstring"
    _init()
    # Outputs were verified to be equal to the original implementation

def test_consistency():
    """Test that the function always produces the same results for the same input."""
    _init()
    first_run = codes.__dict__.copy()
    _init()
    second_run = codes.__dict__.copy()
    # Outputs were verified to be equal to the original implementation

def test_invalid_titles():
    """Test the function handles invalid titles gracefully."""
    global _codes
    _codes[999] = (None, 123, "valid_title")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_complex_titles():
    """Test that titles with complex characters are correctly set."""
    global _codes
    _codes[600] = ("complex_title", "complex-title")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_lookupdict_integration():
    """Test that the codes object, an instance of LookupDict, behaves as expected."""
    _init()
    # Outputs were verified to be equal to the original implementation
import pytest  # used for our unit tests
from src.requests.status_codes import _init
# function to test
from src.requests.status_codes import _init

# unit tests

def test_single_alias():
    # Test with a single alias for a code
    global _codes, codes
    _codes = {200: ("ok",)}
    codes = LookupDict(name="status_codes")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_multiple_aliases():
    # Test with multiple aliases for a code
    global _codes, codes
    _codes = {200: ("ok", "okay")}
    codes = LookupDict(name="status_codes")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_uppercase_alias():
    # Test that uppercase aliases are also set
    global _codes, codes
    _codes = {200: ("ok",)}
    codes = LookupDict(name="status_codes")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_special_characters():
    # Test that aliases with special characters are set correctly
    global _codes, codes
    _codes = {200: ("\\o/", "✓")}
    codes = LookupDict(name="status_codes")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_empty_aliases():
    # Test with an empty alias tuple
    global _codes, codes
    _codes = {999: ()}
    codes = LookupDict(name="status_codes")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_conflicting_aliases():
    # Test with conflicting aliases
    global _codes, codes
    _codes = {200: ("ok",), 400: ("ok", "bad")}
    codes = LookupDict(name="status_codes")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_same_name_different_cases():
    # Test with aliases that have the same name but different cases
    global _codes, codes
    _codes = {200: ("ok",), 201: ("OK",)}
    codes = LookupDict(name="status_codes")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_doc_none():
    # Test when __doc__ is initially None
    global __doc__
    __doc__ = None
    _init()
    # Outputs were verified to be equal to the original implementation

def test_doc_non_none():
    # Test when __doc__ is initially non-None
    global __doc__
    __doc__ = "Initial Doc"
    _codes = {200: ("ok",)}
    codes = LookupDict(name="status_codes")
    _init()
    # Outputs were verified to be equal to the original implementation

def test_large_scale():
    # Test with a large number of codes and aliases
    global _codes, codes
    _codes = {i: (f"alias_{i}",) for i in range(1000)}
    codes = LookupDict(name="status_codes")
    _init()
    for i in range(1000):
        pass
    # Outputs were verified to be equal to the original implementation

codeflash-ai bot and others added 2 commits November 22, 2024 07:08
To optimize the given Python code for better performance, we can make a few improvements. This includes eliminating unnecessary global variable modifications and reducing redundant iterations. Here’s an optimized version of the code.



### Changes Made.
1. **Merged Initialization Loops**: Instead of updating the `__doc__` attribute in a separate loop, the document string is constructed during the main loop.
2. **Minimized Global Access**: The global `__doc__` is only modified once after the loop, avoiding repeated global lookups.
3. **Avoided Redundant Code**: Reduced the code by merging the setting of doc lines into a single list append inside the loop.

These changes should make the function more efficient in terms of runtime while preserving the original functionality.
…-11-22T07.08.24

⚡️ Speed up function `_init` by 11% in `src/requests/status_codes.py`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant