Skip to content

perf(python): index importlib alias rebind checks instead of rescanning - #848

Merged
rowkav09 merged 1 commit into
mainfrom
perf/python-importlib-alias-index
Oct 5, 2026
Merged

rowkav09 merged 1 commit into
mainfrom
perf/python-importlib-alias-index

Conversation

@rowkav09

@rowkav09 rowkav09 commented Oct 5, 2026

Copy link
Copy Markdown
Member

The importlib alias check compared every distinct alias against every statement in the file, building regexes each time, so the cost grew with aliases times statements. A file with many import importlib as ilN lines took 102ms at 100 aliases, 1.5s at 500 and 5.3s at 1000, about 26KB and well under the size cap.

I now index identifiers to statements once, and each alias only checks the statements that mention it. The same checks run on those statements, so which aliases get dropped is unchanged. 1000 aliases take about 20ms now.

The new test builds 1000 aliases, checks the right dynamic imports are credited and a rebound alias is still dropped, and has a 1.5s budget. On main it took 8.5s on my run and failed; with the change the Python adapter suite passes, 274/274.

Fixes #847.

importlibAliases checked every distinct alias against every statement in
the file, building regexes per alias and statement, so the cost grew with
aliases times statements. Repeated `import importlib as ilN` took 102ms at
100 aliases, 1.5s at 500 and 5.3s at 1000 (about 26KB, far under the
size cap).

A statement can only rebind a name it mentions, so index identifiers to
statements once and check each alias against only the statements that
contain it. The same checks run on those statements, so which aliases are
dropped is unchanged. 1000 aliases now take about 20ms.

The new test fails before this change (5.8s against a 1.5s budget) and also
checks that a rebound alias is still dropped.

Fixes #847
@rowkav09
rowkav09 force-pushed the perf/python-importlib-alias-index branch from 5dd35cc to 76a3fca Compare October 5, 2026 06:10
@rowkav09
rowkav09 merged commit bab7689 into main Oct 5, 2026
14 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.

Python: importlib alias rebind check is quadratic in alias count

1 participant