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 get_ref by 10% in pydantic/_internal/_core_utils.py #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Nov 22, 2024

📄 get_ref() in pydantic/_internal/_core_utils.py

📈 Performance improved by 10% (0.10x faster)

⏱️ Runtime went down from 26.7 microseconds to 24.2 microseconds (best of 316 runs)

Explanation and details

Here is a more optimized version of the given program by removing the unnecessary None as the default value for s.get('ref'), because the get method already returns None if the key does not exist.

This maintains the functionality and type hinting, while being slightly more concise and efficient in terms of performance.

Correctness verification

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

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 39 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# function to test
from __future__ import annotations

import pytest  # used for our unit tests
from pydantic._internal._core_utils import get_ref
from pydantic_core import core_schema

# unit tests

def test_basic_functionality_with_ref():
    # Schema with 'ref' key present
    schema = {'ref': 'example_ref'}
    codeflash_output = get_ref(schema)

    schema = {'ref': 'another_ref', 'type': 'string'}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_basic_functionality_without_ref():
    # Schema without 'ref' key
    schema = {'type': 'string'}
    codeflash_output = get_ref(schema)

    schema = {'type': 'integer', 'description': 'An integer value'}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_edge_case_empty_schema():
    # Empty schema
    schema = {}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_edge_case_none_ref():
    # Schema with `None` value for 'ref' key
    schema = {'ref': None}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_edge_case_empty_string_ref():
    # Schema with 'ref' key but empty string as value
    schema = {'ref': ''}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_unusual_but_valid_inputs():
    # Schema with additional nested structures
    schema = {'ref': 'nested_ref', 'properties': {'nested_key': {'type': 'string'}}}
    codeflash_output = get_ref(schema)

    # Schema with non-string 'ref' value
    schema = {'ref': 12345}
    codeflash_output = get_ref(schema)

    schema = {'ref': ['list', 'of', 'values']}
    codeflash_output = get_ref(schema)

    # Schema with boolean 'ref' value
    schema = {'ref': True}
    codeflash_output = get_ref(schema)

    schema = {'ref': False}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_invalid_inputs():
    # Schema as `None`
    schema = None
    with pytest.raises(AttributeError):
        get_ref(schema)

    # Schema as a non-dictionary type
    schema = []
    with pytest.raises(AttributeError):
        get_ref(schema)

    schema = ['ref', 'value']
    with pytest.raises(AttributeError):
        get_ref(schema)

    schema = 42
    with pytest.raises(AttributeError):
        get_ref(schema)

    schema = "string"
    with pytest.raises(AttributeError):
        get_ref(schema)

    schema = 3.14
    with pytest.raises(AttributeError):
        get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_large_scale_with_ref():
    # Schema with a large number of keys, including 'ref'
    schema = {f'key_{i}': i for i in range(1000)}
    schema['ref'] = 'large_ref'
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_large_scale_without_ref():
    # Schema with a large number of keys, excluding 'ref'
    schema = {f'key_{i}': i for i in range(1000)}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_performance_and_scalability():
    # Schema with deeply nested structures
    schema = {'level1': {'level2': {'level3': {'ref': 'deep_ref'}}}}
    codeflash_output = get_ref(schema['level1']['level2']['level3'])

    # Schema with large nested lists and dictionaries
    schema = {'ref': 'complex_ref', 'items': [{'key': i} for i in range(1000)]}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation
# function to test
from __future__ import annotations

import pytest  # used for our unit tests
from pydantic._internal._core_utils import get_ref
from pydantic_core import core_schema

# unit tests

def test_basic_functionality_with_ref():
    # Schema with `ref` key present
    schema = {"ref": "some_reference"}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_basic_functionality_without_ref():
    # Schema without `ref` key
    schema = {"name": "example_schema"}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_edge_case_empty_schema():
    # Empty Schema
    schema = {}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_edge_case_ref_key_with_none_value():
    # Schema with `ref` key but `None` value
    schema = {"ref": None}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_edge_case_ref_key_with_empty_string():
    # Schema with `ref` key but empty string value
    schema = {"ref": ""}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_complex_schema_with_nested_dictionaries():
    # Schema with nested dictionaries
    schema = {"properties": {"nested": {"ref": "nested_reference"}}}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_complex_schema_with_multiple_keys():
    # Schema with multiple keys including `ref`
    schema = {"ref": "some_reference", "name": "example_schema", "type": "object"}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_invalid_input_list():
    # Schema with non-dictionary type (list)
    schema = []
    with pytest.raises(AttributeError):
        get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_invalid_input_none():
    # Schema with non-dictionary type (None)
    schema = None
    with pytest.raises(AttributeError):
        get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_invalid_input_string():
    # Schema with non-dictionary type (string)
    schema = "string"
    with pytest.raises(AttributeError):
        get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_invalid_input_integer():
    # Schema with non-dictionary type (integer)
    schema = 123
    with pytest.raises(AttributeError):
        get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_large_scale_schema_with_ref():
    # Large schema with many keys including `ref`
    schema = {f"key_{i}": f"value_{i}" for i in range(1000)}
    schema["ref"] = "large_scale_reference"
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_large_scale_schema_without_ref():
    # Large schema without `ref` key
    schema = {f"key_{i}": f"value_{i}" for i in range(1000)}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_performance_large_deeply_nested_schema():
    # Very large schema with deeply nested dictionaries
    schema = {"level1": {"level2": {"level3": {"ref": "deep_reference"}}}}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_special_characters_in_keys():
    # Schema with special characters in keys
    schema = {"r@f": "special_key", "ref": "correct_reference"}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_mixed_data_types_in_values():
    # Schema with mixed data types in values
    schema = {"ref": "some_reference", "count": 10, "is_valid": True, "data": [1, 2, 3]}
    codeflash_output = get_ref(schema)
    # Outputs were verified to be equal to the original implementation

def test_no_side_effects():
    # Ensure that the function does not modify the input schema
    schema = {"ref": "some_reference"}
    original_schema = schema.copy()
    get_ref(schema)
    # Outputs were verified to be equal to the original implementation

🔘 (none found) − ⏪ Replay Tests

Here is a more optimized version of the given program by removing the unnecessary `None` as the default value for `s.get('ref')`, because the `get` method already returns `None` if the key does not exist.



This maintains the functionality and type hinting, while being slightly more concise and efficient in terms of performance.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Nov 22, 2024
@codeflash-ai codeflash-ai bot requested a review from alvin-r November 22, 2024 02:23
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.

0 participants