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

Optimize recursion detection by stopping on the second visit for the … #7160

Merged
merged 1 commit into from Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions pydantic/_internal/_core_utils.py
Expand Up @@ -467,8 +467,12 @@ def count_refs(s: core_schema.CoreSchema, recurse: Recurse) -> core_schema.CoreS
return recurse(s, count_refs)
ref = s['schema_ref']
ref_counts[ref] += 1
if current_recursion_ref_count[ref] != 0:
involved_in_recursion[ref] = True

if ref_counts[ref] >= 2:
# If this model is involved in a recursion this should be detected
# on its second encounter, we can safely stop the walk here.
if current_recursion_ref_count[ref] != 0:
involved_in_recursion[ref] = True
return s

current_recursion_ref_count[ref] += 1
Expand Down