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 method GenerateSchema._apply_single_annotation by 4,513% in pydantic/_internal/_generate_schema.py #19

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 Jul 18, 2024

📄 GenerateSchema._apply_single_annotation() in pydantic/_internal/_generate_schema.py

📈 Performance improved by 4,513% (45.13x faster)

⏱️ Runtime went down from 8.80 milliseconds to 191 microseconds

Explanation and details

Certainly! Here’s an optimized version of your Python program. To improve performance, I've employed a few optimization techniques.

  1. Avoid duplicate code by removing the redundant definition of the _apply_single_annotation method.
  2. Minimize operations within loops and conditionals.
  3. Reduce number of copy() calls by only copying when necessary.

Here's the optimized version.

The changes implemented above enhance efficiency by reducing redundant operations and ensuring that code isn't executed multiple times unnecessarily.

  1. Removed the redundant definition of the _apply_single_annotation function.
  2. Avoided unconditional copying of the schema by only copying when necessary.
  3. Prepared default values like core_schema.any_schema() outside conditionals to reduce repeated accesses.
  4. Streamlined some checks into a single conditional block where logic can be consolidated.

These improvements contribute to faster execution while maintaining the functionality of the original script.

Correctness verification

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

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 11 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
from typing import Any, Dict
from unittest.mock import MagicMock, patch

import pytest  # used for our unit tests


# Mocking core_schema and other dependencies
class core_schema:
    class CoreSchema(Dict[str, Any]):
        pass

    @staticmethod
    def any_schema():
        return core_schema.CoreSchema()

class ConfigWrapper:
    pass

class ConfigWrapperStack:
    def __init__(self, config_wrapper):
        pass

class TypesNamespaceStack:
    def __init__(self, types_namespace):
        pass

class _FieldNameStack:
    pass

class _ModelTypeStack:
    pass

class _Definitions:
    def __init__(self):
        self.definitions = {}

class _known_annotated_metadata:
    @staticmethod
    def apply_known_metadata(metadata, schema):
        if isinstance(metadata, str) and metadata == "known":
            schema['known'] = True
            return schema
        return None

class _discriminated_union:
    class MissingDefinitionForUnionRef(Exception):
        pass

    @staticmethod
    def apply_discriminator(schema, discriminator):
        schema['discriminator'] = discriminator
        return schema

    @staticmethod
    def set_discriminator_in_metadata(schema, discriminator):
        schema['discriminator'] = discriminator

# unit tests
@pytest.fixture
def generate_schema():
    return GenerateSchema(ConfigWrapper(), {}, {})

def test_basic_metadata_application(generate_schema):
    schema = core_schema.CoreSchema({"type": "string"})
    metadata = "known"
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema["known"] is True

def test_non_fieldinfo_metadata(generate_schema):
    schema = core_schema.CoreSchema({"type": "string"})
    metadata = {"example": "value"}
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema == schema

def test_nested_fieldinfo_metadata(generate_schema):
    schema = core_schema.CoreSchema({"type": "string"})
    metadata = FieldInfo([FieldInfo(["nested"])])
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema == schema

def test_nullable_schema(generate_schema):
    schema = core_schema.CoreSchema({"type": "nullable", "schema": {"type": "string"}})
    metadata = "known"
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema["schema"]["known"] is True

def test_schema_with_reference(generate_schema):
    schema = core_schema.CoreSchema({"type": "string", "ref": "original"})
    metadata = "known"
    generate_schema.defs.definitions["original_known"] = {"type": "string", "known": True}
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema["known"] is True

def test_definition_reference(generate_schema):
    schema = core_schema.CoreSchema({"type": "definition-ref", "schema_ref": "original"})
    metadata = "known"
    generate_schema.defs.definitions["original"] = {"type": "string"}
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema["ref"] == "original_known"

def test_known_metadata_application(generate_schema):
    schema = core_schema.CoreSchema({"type": "string"})
    metadata = "known"
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema["known"] is True

def test_empty_metadata(generate_schema):
    schema = core_schema.CoreSchema({"type": "string"})
    metadata = None
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema == schema

def test_invalid_metadata(generate_schema):
    schema = core_schema.CoreSchema({"type": "string"})
    metadata = 12345
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema == schema

def test_empty_schema(generate_schema):
    schema = core_schema.CoreSchema({})
    metadata = "known"
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema == schema

def test_large_metadata_list(generate_schema):
    schema = core_schema.CoreSchema({"type": "string"})
    metadata = FieldInfo([FieldInfo([FieldInfo(["deep_nested"]) for _ in range(100)])])
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema == schema

def test_complex_schema(generate_schema):
    schema = core_schema.CoreSchema({"type": "object", "properties": {f"field{i}": {"type": "string"} for i in range(100)}})
    metadata = "known"
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema == schema

def test_high_volume_data(generate_schema):
    schema = core_schema.CoreSchema({"type": "object", "properties": {f"field{i}": {"type": "string"} for i in range(1000)}})
    metadata = FieldInfo([FieldInfo([FieldInfo(["deep_nested"]) for _ in range(100)])])
    updated_schema = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema == schema

def test_deterministic_behavior(generate_schema):
    schema = core_schema.CoreSchema({"type": "string"})
    metadata = "known"
    updated_schema1 = generate_schema._apply_single_annotation(schema, metadata)
    updated_schema2 = generate_schema._apply_single_annotation(schema, metadata)
    assert updated_schema1 == updated_schema2

✅ 1 Passed − ⏪ Replay Tests

Certainly! Here’s an optimized version of your Python program. To improve performance, I've employed a few optimization techniques.
1. Avoid duplicate code by removing the redundant definition of the `_apply_single_annotation` method.
2. Minimize operations within loops and conditionals.
3. Reduce number of `copy()` calls by only copying when necessary.

Here's the optimized version.



The changes implemented above enhance efficiency by reducing redundant operations and ensuring that code isn't executed multiple times unnecessarily. 

1. Removed the redundant definition of the `_apply_single_annotation` function.
2. Avoided unconditional copying of the schema by only copying when necessary.
3. Prepared default values like `core_schema.any_schema()` outside conditionals to reduce repeated accesses.
4. Streamlined some checks into a single conditional block where logic can be consolidated.

These improvements contribute to faster execution while maintaining the functionality of the original script.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Jul 18, 2024
@iusedmyimagination
Copy link

Might be legitimate. Did we copy the method twice in the snippet?

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