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

Performance: Add static TypeAdapters and reuse them. #15047

Open
aaazzam opened this issue Aug 22, 2024 · 0 comments
Open

Performance: Add static TypeAdapters and reuse them. #15047

aaazzam opened this issue Aug 22, 2024 · 0 comments
Labels
enhancement An improvement of an existing feature performance Related to an optimization or performance improvement

Comments

@aaazzam
Copy link
Collaborator

aaazzam commented Aug 22, 2024

Describe the current behavior

Today Prefect liberally uses Pydantic's TypeAdapter to validate models of type Generic[BaseModel], e.g.

async def read_variables(self, limit: Optional[int] = None) -> List[Variable]:
    """Reads all variables."""
    response = await self._client.post("/variables/filter", json={"limit": limit})
    return pydantic.TypeAdapter(List[Variable]).validate_python(response.json())

Morally, TypeAdapter under the hood is a lower-level create_model which has a non-trivial cost to create.

Pydantic
When creating an instance of TypeAdapter, the provided type must be analyzed and converted into a pydantic-core schema. This comes with some non-trivial overhead, so it is recommended to create a TypeAdapter for a given type just once and reuse it in loops or other performance-critical code.

This means today Prefect is paying that overhead in almost every client call that fetches a list.

Describe the proposed behavior

For each relevant Prefect BaseModel, e.g. Variable create a

_VariableListAdapter = TypeAdapter(List[Variable])

alongside that BaseModel for reuse. Note with our switch to defer_build, we need to explicitly turn on defer_build for each TypeAdapter so that we don't force the creation of a serializer accidentally.

https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.experimental_defer_build_mode

Example Use

No response

Additional context

No response

@aaazzam aaazzam added the enhancement An improvement of an existing feature label Aug 22, 2024
@desertaxle desertaxle added the performance Related to an optimization or performance improvement label Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement of an existing feature performance Related to an optimization or performance improvement
Projects
None yet
Development

No branches or pull requests

2 participants