Skip to content

Replace the method sorter with a direct DFS - #3142

Merged
soutaro merged 2 commits into
masterfrom
method-builder-dfs
Sep 2, 2026
Merged

Replace the method sorter with a direct DFS#3142
soutaro merged 2 commits into
masterfrom
method-builder-dfs

Conversation

@soutaro

@soutaro soutaro commented Sep 2, 2026

Copy link
Copy Markdown
Member

MethodBuilder::Methods#each sorted the methods topologically with
TSort so that the original of an alias is yielded before the alias,
building the graph of all the methods on every call, although most
types have no alias at all.

each now yields the methods in definition order when there is no
alias, and otherwise walks the alias chain of each method with a small
DFS that yields the originals first and raises
RecursiveAliasDefinitionError on a cycle, as the sorter did. Building
the definitions of all 1,806 types of Steep's environment goes from
2.5s to 1.9s.

`MethodBuilder::Methods#each` sorted the methods of every type with
TSort, although the sort exists only to yield the original method of an
alias before the alias, and to detect recursive aliases. Without an
alias member every method is its own SCC and the sort yields them in
insertion order, so check for aliases in one pass and iterate the table
directly when there is none.

The sort was one of the two hotspots of building the definitions of
every type in an environment, next to the variance validation --
19% of a whole-environment warmup of a large Rails application, whose
methods mostly come from generated RBS files without aliases.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@soutaro
soutaro enabled auto-merge September 2, 2026 13:04
`MethodBuilder::Methods#each` sorted the methods with the generic TSort
when the type has an alias, paying for the enumerator and block
machinery of the library on every method. The graph is trivial -- each
method has at most one edge, to the original of an alias -- so a direct
DFS yields the same order: originals before their aliases, everything
else in insertion order.

The recursive alias detection is preserved, including yielding a
self-alias as is, which forms a size-1 SCC that the sorter never
reported as recursive.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@soutaro
soutaro added this pull request to the merge queue Sep 2, 2026
Merged via the queue into master with commit e96867d Sep 2, 2026
24 checks passed
@soutaro
soutaro deleted the method-builder-dfs branch September 2, 2026 13:13
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.

2 participants