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

perf: cache TypeAdapters #1114

Merged
merged 7 commits into from
Mar 18, 2024
Merged

perf: cache TypeAdapters #1114

merged 7 commits into from
Mar 18, 2024

Conversation

vvanglro
Copy link
Contributor

@vvanglro vvanglro commented Feb 1, 2024

Simple test example:

from datetime import date, datetime
from timeit import timeit

from pydantic import TypeAdapter


def parse_obj(annot, value):
    ta = TypeAdapter(annot)

    result1 = timeit(lambda: ta.validate_python(value), number=10000)
    result2 = timeit(lambda: TypeAdapter(annot).validate_python(value), number=10000)

    print(f'{annot.__name__:<8} {result2 / result1:.2f}')


if __name__ == '__main__':
    for t, v in (
        (int, "0"),
        (float, '0.5'),
        (str, b"123"),
        (bool, 'true'),
        (date, '2024-01-01'),
        (datetime, '2024-01-01T00:00:00'),
    ):
        parse_obj(t, v)

output:

int      131.77
float    107.20
str      109.12
bool     128.88
date     283.73
datetime 272.90

Refer to: https://docs.pydantic.dev/latest/concepts/performance/#typeadapter-instantiated-once

@vvanglro vvanglro requested a review from a team as a code owner February 1, 2024 09:12
src/openai/_models.py Outdated Show resolved Hide resolved
@vvanglro
Copy link
Contributor Author

Sorry I'm late. Ci should be working now.

Reference about lru_cache and pyright:
pydantic/pydantic#5766
microsoft/pyright#5123

Comment on lines 368 to 374
from pydantic import TypeAdapter as PyTypeAdapter

def TypeAdapter(type_: type[_T]) -> PyTypeAdapter[_T]:
return PyTypeAdapter(type_)

if not TYPE_CHECKING:
TypeAdapter = lru_cache(TypeAdapter)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this conveys the intent of this code more clearly (and you can add the lru_cache import at the top of the file), thoughts?

Suggested change
from pydantic import TypeAdapter as PyTypeAdapter
def TypeAdapter(type_: type[_T]) -> PyTypeAdapter[_T]:
return PyTypeAdapter(type_)
if not TYPE_CHECKING:
TypeAdapter = lru_cache(TypeAdapter)
if TYPE_CHECKING:
from pydantic import TypeAdapter
else:
from pydantic import TypeAdapter as _TypeAdapter
TypeAdapter = lru_cache(TypeAdapter)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, It has been modified.

@RobertCraigie RobertCraigie changed the title perf: TypeAdapter instantiated once perf: cache TypeAdapters Mar 18, 2024
@RobertCraigie RobertCraigie changed the base branch from main to next March 18, 2024 20:46
Copy link
Collaborator

@RobertCraigie RobertCraigie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this and sorry for the delayed review!

@RobertCraigie RobertCraigie merged commit 41b6fee into openai:next Mar 18, 2024
1 check passed
@stainless-bot stainless-bot mentioned this pull request Mar 18, 2024
nknj pushed a commit that referenced this pull request Mar 19, 2024
* perf: cache TypeAdapters (#1114)

* perf: cache TypeAdapters (#1243)

* docs: fix typo in CONTRIBUTING.md (#1245)

* chore(internal): update generated pragma comment (#1247)

* docs: assistant improvements (#1249)

* release: 1.14.2

---------

Co-authored-by: vvanglro <947001731@qq.com>
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.

None yet

2 participants