Skip to content

Fix cubic-time from_type() on abstract class hierarchies#4749

Merged
Zac-HD merged 1 commit into
HypothesisWorks:masterfrom
Zac-HD:claude/quirky-pascal-SyLp3
May 27, 2026
Merged

Fix cubic-time from_type() on abstract class hierarchies#4749
Zac-HD merged 1 commit into
HypothesisWorks:masterfrom
Zac-HD:claude/quirky-pascal-SyLp3

Conversation

@Zac-HD
Copy link
Copy Markdown
Member

@Zac-HD Zac-HD commented May 27, 2026

Resolving an abstract type whose subclasses reference the base class in their own annotations re-resolved the entire hierarchy once per reference, taking time cubic in the number of subclasses. Guard against re-entry so each type is resolved only once: if we're already resolving an abstract type higher up the stack, return the cached from_type() strategy (sharing one object lets the recursion in e.g. is_empty checks terminate) instead of re-resolving it. We reuse the existing _recurse_guard, which also catches references that reach us as a union arg rather than a field.

Also filter the registered-subtype lookup before sorting it, so the expensive repr-based sort only runs on the (usually empty) matching set rather than the whole global type lookup on every resolution.

Fixes #4729.

@Zac-HD Zac-HD force-pushed the claude/quirky-pascal-SyLp3 branch from ed312aa to 1a65386 Compare May 27, 2026 07:16
Resolving an abstract type whose subclasses reference the base class in
their own annotations re-resolved the entire hierarchy once per reference,
taking time cubic in the number of subclasses. Track the abstract types
currently being resolved in a guard, and defer any recursive reference
back to them by returning the cached from_type() strategy (sharing one
object lets the recursion in e.g. is_empty checks terminate), so each type
is resolved only once. This guard is kept separate from _recurse_guard so
that it catches references arriving by any route (e.g. as a union arg) and
doesn't make from_type_guarded treat a subclass's required field of the
abstract type as unresolvable.

Also filter the registered-subtype lookup before sorting it, so the
expensive repr-based sort only runs on the (usually empty) matching set
rather than the whole global type lookup on every resolution.

https://claude.ai/code/session_01MdLX8p4tdAUaDjSHBnveVy
@Zac-HD Zac-HD force-pushed the claude/quirky-pascal-SyLp3 branch from 1a65386 to 34ef684 Compare May 27, 2026 07:35
@Zac-HD Zac-HD enabled auto-merge May 27, 2026 15:49
@Zac-HD Zac-HD closed this May 27, 2026
auto-merge was automatically disabled May 27, 2026 16:11

Pull request was closed

@Zac-HD Zac-HD reopened this May 27, 2026
@Zac-HD Zac-HD merged commit 84a15f8 into HypothesisWorks:master May 27, 2026
7 of 8 checks passed
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.

Time blowup for from_type with certain abstract classes

3 participants