8000 Add caches for collecting definitions and invalid schemas from a CoreSchema by adriangb · Pull Request #7527 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@adriangb
Copy link
Member
@adriangb adriangb commented Sep 20, 2023

Selected Reviewer: @hramezani

@cloudflare-workers-and-pages
Copy link
cloudflare-workers-and-pages bot commented Sep 20, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: f0487f6
Status: ✅  Deploy successful!
Preview URL: https://cb9889cc.pydantic-docs2.pages.dev
Branch Preview URL: https://collect-defs-cache.pydantic-docs2.pages.dev

View logs

Comment on lines 164 to 180
def collect_invalid_schemas(schema: core_schema.CoreSchema) -> list[core_schema.CoreSchema]:
invalid_schemas: list[core_schema.CoreSchema] = []
child_invalid = False # track recursion

def _is_schema_valid(s: core_schema.CoreSchema, recurse: Recurse) -> core_schema.CoreSchema:
if s.get('metadata', {}).get('invalid'):
nonlocal child_invalid
metadata = s.setdefault('metadata', {})
invalid = metadata.get('invalid', None)
if invalid is True:
invalid_schemas.append(s)
return recurse(s, _is_schema_valid)
elif invalid is False:
return s
s = recurse(s, _is_schema_valid)
if invalid is True:
child_invalid = True
metadata['invalid'] = child_invalid
return s
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on the motivation of this change? Maybe needs a comment, it's quite winding logic.

Copy link
Member Author

Choose a reason for hiding this comment

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

The invalid thing already existed. What this is doing is marking a schema as "there are no invalid schemas in this subtree" (which is the most common case) so that we can skip recursing into it after the first time we recurse through it.

Copy link
Member Author

Choose a reason for hiding this comment

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

But now that I think of it we should be able to do something similar to https://github.com/pydantic/pydantic/pull/7529/files#diff-2ebfa892ec1d09440eefd4b22f488ec38c1cde365394e5a4cea6edb484827ba5 and do this from within GenerateSchema during our first recursion through the CoreSchema (we build it recursively).

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll wait to make that change until #7529 is merged so I can re-use post_process_schema and such

@adriangb
Copy link
Member Author

please review

@adriangb adriangb enabled auto-merge (squash) September 20, 2023 15:32
@adriangb adriangb merged commit 2d36952 into main Sep 20, 2023
@adriangb adriangb deleted the collect-defs-cache branch September 20, 2023 15:43
@davidhewitt davidhewitt added the relnotes-performance Used for performance improvements. label Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for review relnotes-performance Used for performance improvements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0