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 check_validator_fields_against_field_name by 18% #54

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 Jan 24, 2025

📄 18% (0.18x) speedup for check_validator_fields_against_field_name in pydantic/_internal/_generate_schema.py

⏱️ Runtime : 118 microseconds 100 microseconds (best of 494 runs)

📝 Explanation and details

Here's the rewritten version of the check_validator_fields_against_field_name function, optimized for faster execution.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 44 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests Details
from typing import List, Union

# imports
import pytest  # used for our unit tests
# function to test
from pydantic._internal._decorators import (FieldSerializerDecoratorInfo,
                                            FieldValidatorDecoratorInfo,
                                            ValidatorDecoratorInfo)
from pydantic._internal._generate_schema import \
    check_validator_fields_against_field_name

FieldDecoratorInfo = Union[ValidatorDecoratorInfo, FieldValidatorDecoratorInfo, FieldSerializerDecoratorInfo]
from pydantic._internal._generate_schema import \
    check_validator_fields_against_field_name


# Mock classes to simulate FieldDecoratorInfo types
class MockFieldDecoratorInfo:
    def __init__(self, fields: List[str]):
        self.fields = fields


# unit tests

def test_single_field_match():
    info = MockFieldDecoratorInfo(['field1'])
    codeflash_output = check_validator_fields_against_field_name(info, 'field1')

def test_single_field_no_match():
    info = MockFieldDecoratorInfo(['field1'])
    codeflash_output = check_validator_fields_against_field_name(info, 'field2')

def test_wildcard_present():
    info = MockFieldDecoratorInfo(['*'])
    codeflash_output = check_validator_fields_against_field_name(info, 'any_field')

def test_wildcard_absent():
    info = MockFieldDecoratorInfo(['field1', 'field2'])
    codeflash_output = check_validator_fields_against_field_name(info, 'any_field')

def test_empty_fields_list():
    info = MockFieldDecoratorInfo([])
    codeflash_output = check_validator_fields_against_field_name(info, 'any_field')

def test_case_sensitivity_match():
    info = MockFieldDecoratorInfo(['Field'])
    codeflash_output = check_validator_fields_against_field_name(info, 'Field')

def test_case_sensitivity_no_match():
    info = MockFieldDecoratorInfo(['Field'])
    codeflash_output = check_validator_fields_against_field_name(info, 'field')

def test_multiple_fields_exists():
    info = MockFieldDecoratorInfo(['field1', 'field2', 'field3'])
    codeflash_output = check_validator_fields_against_field_name(info, 'field2')

def test_multiple_fields_not_exists():
    info = MockFieldDecoratorInfo(['field1', 'field2', 'field3'])
    codeflash_output = check_validator_fields_against_field_name(info, 'field4')

def test_field_name_empty_string_match():
    info = MockFieldDecoratorInfo(['field1', ''])
    codeflash_output = check_validator_fields_against_field_name(info, '')

def test_field_name_empty_string_no_match():
    info = MockFieldDecoratorInfo(['field1'])
    codeflash_output = check_validator_fields_against_field_name(info, '')

def test_field_name_special_characters_match():
    info = MockFieldDecoratorInfo(['field1', 'field@2'])
    codeflash_output = check_validator_fields_against_field_name(info, 'field@2')

def test_field_name_special_characters_no_match():
    info = MockFieldDecoratorInfo(['field1', 'field@2'])
    codeflash_output = check_validator_fields_against_field_name(info, 'field#2')

def test_large_number_of_fields_exists():
    info = MockFieldDecoratorInfo(['field' + str(i) for i in range(1000)])
    codeflash_output = check_validator_fields_against_field_name(info, 'field9999')

def test_large_number_of_fields_not_exists():
    info = MockFieldDecoratorInfo(['field' + str(i) for i in range(1000)])
    codeflash_output = check_validator_fields_against_field_name(info, 'field10000')

def test_performance_with_large_inputs_exists():
    info = MockFieldDecoratorInfo(['field' + str(i) for i in range(1000)])
    codeflash_output = check_validator_fields_against_field_name(info, 'field999999')

def test_performance_with_large_inputs_not_exists():
    info = MockFieldDecoratorInfo(['field' + str(i) for i in range(1000)])
    codeflash_output = check_validator_fields_against_field_name(info, 'field1000000')

def test_mixed_types_no_match():
    info = MockFieldDecoratorInfo(['field1', 2, 'field3'])
    codeflash_output = check_validator_fields_against_field_name(info, '2')

def test_mixed_types_match():
    info = MockFieldDecoratorInfo(['field1', 2, 'field3'])
    codeflash_output = check_validator_fields_against_field_name(info, 'field3')

def test_non_string_field_names_no_match():
    info = MockFieldDecoratorInfo([1, 2, 3])
    codeflash_output = check_validator_fields_against_field_name(info, '1')

def test_non_string_field_names_match():
    info = MockFieldDecoratorInfo([1, 2, 3])
    codeflash_output = check_validator_fields_against_field_name(info, 2)
# 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 Union

# imports
import pytest  # used for our unit tests
from pydantic._internal._decorators import (FieldSerializerDecoratorInfo,
                                            FieldValidatorDecoratorInfo,
                                            ValidatorDecoratorInfo)
from pydantic._internal._generate_schema import \
    check_validator_fields_against_field_name

# function to test
FieldDecoratorInfo = Union[ValidatorDecoratorInfo, FieldValidatorDecoratorInfo, FieldSerializerDecoratorInfo]
from pydantic._internal._generate_schema import \
    check_validator_fields_against_field_name

# unit tests

class MockFieldDecoratorInfo:
    def __init__(self, fields):
        self.fields = fields

def test_field_name_explicitly_listed():
    info = MockFieldDecoratorInfo(["field1", "field2"])
    codeflash_output = check_validator_fields_against_field_name(info, "field1")
    codeflash_output = check_validator_fields_against_field_name(info, "field2")

def test_field_name_not_listed():
    info = MockFieldDecoratorInfo(["field1", "field2"])
    codeflash_output = check_validator_fields_against_field_name(info, "field3")
    codeflash_output = check_validator_fields_against_field_name(info, "field4")

def test_wildcard_present():
    info = MockFieldDecoratorInfo(["*"])
    codeflash_output = check_validator_fields_against_field_name(info, "anyfield")
    info = MockFieldDecoratorInfo(["*", "field1"])
    codeflash_output = check_validator_fields_against_field_name(info, "field2")

def test_empty_info_fields():
    info = MockFieldDecoratorInfo([])
    codeflash_output = check_validator_fields_against_field_name(info, "field1")
    codeflash_output = check_validator_fields_against_field_name(info, "")

def test_empty_field_string():
    info = MockFieldDecoratorInfo(["field1", "field2"])
    codeflash_output = check_validator_fields_against_field_name(info, "")
    info = MockFieldDecoratorInfo(["*"])
    codeflash_output = check_validator_fields_against_field_name(info, "")

def test_case_sensitivity():
    info = MockFieldDecoratorInfo(["Field1", "field2"])
    codeflash_output = check_validator_fields_against_field_name(info, "field1")
    codeflash_output = check_validator_fields_against_field_name(info, "Field1")

def test_large_number_of_fields():
    info = MockFieldDecoratorInfo(["field" + str(i) for i in range(1000)])
    codeflash_output = check_validator_fields_against_field_name(info, "field500")
    codeflash_output = check_validator_fields_against_field_name(info, "field1001")

def test_mixed_types_in_info_fields():
    info = MockFieldDecoratorInfo(["field1", 2, None])
    codeflash_output = check_validator_fields_against_field_name(info, "field1")
    codeflash_output = check_validator_fields_against_field_name(info, "2")

def test_special_characters():
    info = MockFieldDecoratorInfo(["field-1", "field_2"])
    codeflash_output = check_validator_fields_against_field_name(info, "field-1")
    codeflash_output = check_validator_fields_against_field_name(info, "field@2")

def test_overlapping_field_names():
    info = MockFieldDecoratorInfo(["field", "field1"])
    codeflash_output = check_validator_fields_against_field_name(info, "field")
    codeflash_output = check_validator_fields_against_field_name(info, "field1")
    codeflash_output = check_validator_fields_against_field_name(info, "field2")

def test_performance_with_large_data_samples():
    info = MockFieldDecoratorInfo(["field" + str(i) for i in range(1000)])
    codeflash_output = check_validator_fields_against_field_name(info, "field999999")
    codeflash_output = check_validator_fields_against_field_name(info, "field1000000")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

📢 Feedback on this optimization? Discord

Here's the rewritten version of the `check_validator_fields_against_field_name` function, optimized for faster execution.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Jan 24, 2025
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 January 24, 2025 10:07
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 relnotes-fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants