Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 23, 2025

📄 24% (0.24x) speedup for __parse_auth_credentials in weaviate/connect/helpers.py

⏱️ Runtime : 1.09 milliseconds 877 microseconds (best of 57 runs)

📝 Explanation and details

The optimization reorders the conditional checks in __parse_auth_credentials() to prioritize the most common case first.

Key Change: The creds is None check is moved from the third position to the first position in the if-elif chain.

Why this improves performance:

  • Early exit optimization: When credentials are None (a common case in authentication flows), the function now returns immediately without performing any expensive isinstance() type checks
  • Reduced branching overhead: The original code performed type checking on strings first, then complex isinstance() checks, before finally checking for None. The optimized version eliminates unnecessary work for the None case

Performance gains by test case type:

  • Massive improvement for None inputs: 96-110% faster when creds is None, as seen in the large-scale None tests (229μs → 117μs)
  • Slight regression for string inputs: 6-25% slower for API key strings since they now require an additional branch check, but this is minimal compared to the None case gains
  • Error cases remain similar: Invalid type handling shows mixed but minor changes

This optimization is particularly effective for applications where authentication credentials are frequently optional or not provided, making the None case a hot path that benefits significantly from early exit.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 4113 Passed
⏪ Replay Tests 1 Passed
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 85.7%
🌀 Generated Regression Tests and Runtime
from typing import Optional, Union

# imports
import pytest
from weaviate.connect.helpers import __parse_auth_credentials

# --- Mocks for weaviate.auth and credentials classes ---
# These are minimal implementations to simulate the actual classes.

class _APIKey:
    def __init__(self, api_key: str):
        self.api_key = api_key
    def __eq__(self, other):
        return isinstance(other, _APIKey) and self.api_key == other.api_key

class _BearerToken:
    def __init__(self, token: str):
        self.token = token
    def __eq__(self, other):
        return isinstance(other, _BearerToken) and self.token == other.token

class _ClientCredentials:
    def __init__(self, client_id: str, client_secret: str):
        self.client_id = client_id
        self.client_secret = client_secret
    def __eq__(self, other):
        return (
            isinstance(other, _ClientCredentials)
            and self.client_id == other.client_id
            and self.client_secret == other.client_secret
        )

class _ClientPassword:
    def __init__(self, username: str, password: str):
        self.username = username
        self.password = password
    def __eq__(self, other):
        return (
            isinstance(other, _ClientPassword)
            and self.username == other.username
            and self.password == other.password
        )

AuthCredentials = (_BearerToken, _ClientPassword, _ClientCredentials, _APIKey)
from weaviate.connect.helpers import __parse_auth_credentials

# --- Unit Tests ---

# ------------------- Basic Test Cases -------------------

def test_parse_api_key_string_returns_api_key_object():
    """Test that a string input returns an _APIKey object with correct value."""
    api_key = "my-secret-key"
    codeflash_output = __parse_auth_credentials(api_key); result = codeflash_output # 1.59μs -> 2.12μs (25.0% slower)





def test_parse_none_returns_none():
    """Test that passing None returns None."""
    codeflash_output = __parse_auth_credentials(None); result = codeflash_output # 836ns -> 399ns (110% faster)

# ------------------- Edge Test Cases -------------------

def test_parse_empty_string_api_key():
    """Test that an empty string is treated as a valid API key."""
    codeflash_output = __parse_auth_credentials(""); result = codeflash_output # 1.30μs -> 1.54μs (15.3% slower)

def test_parse_whitespace_string_api_key():
    """Test that a whitespace string is treated as a valid API key."""
    codeflash_output = __parse_auth_credentials("   "); result = codeflash_output # 866ns -> 1.01μs (14.1% slower)




def test_parse_integer_raises_value_error():
    """Test that passing an integer raises ValueError."""
    with pytest.raises(ValueError):
        __parse_auth_credentials(12345) # 1.32μs -> 1.26μs (4.44% faster)

def test_parse_float_raises_value_error():
    """Test that passing a float raises ValueError."""
    with pytest.raises(ValueError):
        __parse_auth_credentials(3.14) # 1.15μs -> 1.25μs (8.62% slower)

def test_parse_bool_raises_value_error():
    """Test that passing a boolean raises ValueError."""
    with pytest.raises(ValueError):
        __parse_auth_credentials(True) # 1.32μs -> 1.35μs (2.14% slower)

def test_parse_list_raises_value_error():
    """Test that passing a list raises ValueError."""
    with pytest.raises(ValueError):
        __parse_auth_credentials(["api_key"]) # 1.12μs -> 1.16μs (3.95% slower)

def test_parse_dict_raises_value_error():
    """Test that passing a dict raises ValueError."""
    with pytest.raises(ValueError):
        __parse_auth_credentials({"api_key": "value"}) # 999ns -> 1.08μs (7.24% slower)

def test_parse_object_of_wrong_type_raises_value_error():
    """Test that passing an unrelated object raises ValueError."""
    class NotACredential:
        pass
    with pytest.raises(ValueError):
        __parse_auth_credentials(NotACredential()) # 1.15μs -> 1.25μs (7.97% slower)

def test_parse_bytes_raises_value_error():
    """Test that passing bytes raises ValueError."""
    with pytest.raises(ValueError):
        __parse_auth_credentials(b"api_key") # 999ns -> 1.02μs (1.96% slower)

def test_parse_tuple_raises_value_error():
    """Test that passing a tuple raises ValueError."""
    with pytest.raises(ValueError):
        __parse_auth_credentials(("api_key",)) # 1.13μs -> 1.13μs (0.000% faster)

# ------------------- Large Scale Test Cases -------------------

def test_parse_many_unique_api_key_strings():
    """Test parsing a large number of unique API key strings."""
    for i in range(1000):
        key = f"key_{i}"
        codeflash_output = __parse_auth_credentials(key); result = codeflash_output # 282μs -> 288μs (2.09% slower)





def test_parse_many_none_values():
    """Test parsing None repeatedly."""
    for _ in range(1000):
        codeflash_output = __parse_auth_credentials(None); result = codeflash_output # 228μs -> 116μs (96.1% faster)

def test_parse_many_invalid_types():
    """Test that ValueError is raised for many invalid types."""
    invalids = [123, 3.14, True, ["a"], {"b": "c"}, b"bytes", (1,), object()]
    for invalid in invalids:
        for _ in range(10):  # repeat to check determinism
            with pytest.raises(ValueError):
                __parse_auth_credentials(invalid)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from typing import Optional, Union

# imports
import pytest
from weaviate.connect.helpers import __parse_auth_credentials

# --- Minimal stubs for classes referenced in the function ---
# These are necessary to run the tests, as they are referenced in the function

class AuthCredentials:
    pass

class _APIKey(AuthCredentials):
    def __init__(self, api_key: str):
        self.api_key = api_key

class _BearerToken(AuthCredentials):
    def __init__(self, token: str):
        self.token = token

class _ClientCredentials(AuthCredentials):
    def __init__(self, client_id: str, client_secret: str):
        self.client_id = client_id
        self.client_secret = client_secret

class _ClientPassword(AuthCredentials):
    def __init__(self, username: str, password: str):
        self.username = username
        self.password = password
from weaviate.connect.helpers import __parse_auth_credentials

# --- Unit tests ---

# 1. Basic Test Cases

def test_api_key_string_returns_api_key_object():
    # Should convert string to _APIKey
    api_key = "my-secret-key"
    codeflash_output = __parse_auth_credentials(api_key); result = codeflash_output # 1.39μs -> 1.48μs (6.01% slower)





def test_none_returns_none():
    # Should return None if creds is None
    codeflash_output = __parse_auth_credentials(None); result = codeflash_output # 861ns -> 448ns (92.2% faster)

# 2. Edge Test Cases

def test_empty_string_as_api_key():
    # Should accept empty string and wrap in _APIKey
    codeflash_output = __parse_auth_credentials(""); result = codeflash_output # 1.34μs -> 1.52μs (11.7% slower)

def test_string_with_special_characters_as_api_key():
    # Should accept string with special characters
    key = "!@#$%^&*()_+-=[]{}|;':,.<>/?`~"
    codeflash_output = __parse_auth_credentials(key); result = codeflash_output # 863ns -> 985ns (12.4% slower)

def test_invalid_type_raises_value_error():
    # Should raise ValueError for unsupported types (e.g. int, float, list, dict, tuple, set, bool, bytes)
    for invalid in [123, 45.6, [1,2,3], {"a":1}, (1,2), {1,2}, True, b"bytes"]:
        with pytest.raises(ValueError):
            __parse_auth_credentials(invalid)

def test_unrelated_object_raises_value_error():
    # Should raise ValueError for objects not in the allowed types
    class Unrelated:
        pass
    with pytest.raises(ValueError):
        __parse_auth_credentials(Unrelated()) # 1.08μs -> 1.15μs (6.43% slower)

def test_subclass_of_auth_credentials_not_in_tuple_raises_value_error():
    # Should raise ValueError for subclass of AuthCredentials not explicitly listed
    class CustomCred(AuthCredentials):
        pass
    with pytest.raises(ValueError):
        __parse_auth_credentials(CustomCred()) # 1.26μs -> 1.27μs (0.785% slower)

def test_auth_credentials_base_class_raises_value_error():
    # Should raise ValueError for AuthCredentials base class instance
    with pytest.raises(ValueError):
        __parse_auth_credentials(AuthCredentials()) # 861ns -> 1.09μs (21.2% slower)

def test_falsey_non_none_values_raise():
    # Should raise ValueError for falsey values except None and ""
    for falsey in [0, [], {}, set(), ()]:
        with pytest.raises(ValueError):
            __parse_auth_credentials(falsey)

# 3. Large Scale Test Cases

def test_large_api_key_string():
    # Should handle a very large API key string
    key = "A" * 1000
    codeflash_output = __parse_auth_credentials(key); result = codeflash_output # 1.24μs -> 1.29μs (4.17% slower)

def test_large_list_of_api_key_strings():
    # Should handle many different API key strings
    keys = [f"key_{i}" for i in range(1000)]
    for k in keys:
        codeflash_output = __parse_auth_credentials(k); result = codeflash_output # 283μs -> 289μs (1.87% slower)


def test_large_number_of_none_inputs():
    # Should handle many None inputs
    for _ in range(1000):
        codeflash_output = __parse_auth_credentials(None) # 229μs -> 117μs (96.1% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from weaviate.auth import _APIKey
from weaviate.connect.helpers import __parse_auth_credentials

def test___parse_auth_credentials():
    __parse_auth_credentials(None)

def test___parse_auth_credentials_2():
    __parse_auth_credentials('')

def test___parse_auth_credentials_3():
    __parse_auth_credentials(_APIKey(''))

Timer unit: 1e-09 s
⏪ Replay Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_pytest_testcollectiontest_batch_py_testcollectiontest_classes_generative_py_testcollectiontest_confi__replay_test_0.py::test_weaviate_connect_helpers___parse_auth_credentials 1.44μs 845ns 70.7%✅

To edit these changes git checkout codeflash/optimize-__parse_auth_credentials-mh39pilt and push.

Codeflash

The optimization reorders the conditional checks in `__parse_auth_credentials()` to prioritize the most common case first. 

**Key Change:** The `creds is None` check is moved from the third position to the first position in the if-elif chain.

**Why this improves performance:**
- **Early exit optimization**: When credentials are `None` (a common case in authentication flows), the function now returns immediately without performing any expensive `isinstance()` type checks
- **Reduced branching overhead**: The original code performed type checking on strings first, then complex `isinstance()` checks, before finally checking for `None`. The optimized version eliminates unnecessary work for the `None` case

**Performance gains by test case type:**
- **Massive improvement for None inputs**: 96-110% faster when `creds` is `None`, as seen in the large-scale None tests (229μs → 117μs)
- **Slight regression for string inputs**: 6-25% slower for API key strings since they now require an additional branch check, but this is minimal compared to the None case gains
- **Error cases remain similar**: Invalid type handling shows mixed but minor changes

This optimization is particularly effective for applications where authentication credentials are frequently optional or not provided, making the `None` case a hot path that benefits significantly from early exit.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 23, 2025 10:17
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 23, 2025
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant