Skip to content

Speed up find_matching_direct_imports - #334

Merged
seddonym merged 2 commits into
python-grimp:mainfrom
Peter554:faster-find-matching-direct-imports
Nov 13, 2026
Merged

seddonym merged 2 commits into
python-grimp:mainfrom
Peter554:faster-find-matching-direct-imports

Conversation

@Peter554

Copy link
Copy Markdown
Collaborator

Motivation

Import Linter's ignore_imports calls find_matching_direct_imports once for each expression in a contract's ignore list. Each call checked every import in the graph against both expressions, so the cost of each call grew with the size of the graph, even though most expressions only match a handful of modules.

On a large code base (about 150k modules and 1M imports), resolving 1,262 expressions took about 19s. That is a large share of the time Import Linter spends on some contracts.

Changes

Two commits:

  • find_matching_modules: an expression with no wildcards can only match the module with that name, so look it up directly instead of checking every module.
  • find_matching_direct_imports: find the modules that match each expression, then only look at the imports of the modules that match the importer expression.

There are no API changes, and the results are the same.

Results

On the same graph:

Before After
1,262 expressions 19.3s 1.6s

The results were identical for every expression.

An expression with no wildcards can only match the module with that name.
Look that module up directly, instead of checking the expression against
the name of every module in the graph.

Assistant-model: Claude Code
Previously this checked every import in the graph against both
expressions, which made each call proportional to the size of the graph.
This is slow when it is called many times, as Import Linter does for
each of the expressions in a contract's ignore_imports.

Instead, find the modules that match each expression, then only look at
the imports of the modules that match the importer expression.

Assistant-model: Claude Code
@Peter554 Peter554 self-assigned this Nov 10, 2026
@codspeed

codspeed Bot commented Nov 10, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 57.75%

⚡ 1 improved benchmark
✅ 25 untouched benchmarks
⏩ 23 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_find_matching_direct_imports 27.6 ms 17.5 ms +57.75%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Peter554:faster-find-matching-direct-imports (fe2bcac) with main (3c4c7c9)

Open in CodSpeed

Footnotes

  1. 23 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@Peter554
Peter554 marked this pull request as ready for review November 10, 2026 16:31
@seddonym
seddonym requested a balanced review from Copilot November 12, 2026 18:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the module and direct-import matching optimizations.

No open issues 🎉

What changed in this PR

Speeds up module and direct-import matching in Grimp, reducing the work needed for Import Linter’s ignore expressions while preserving the public API and matching results.

Changes:

  • Uses direct module lookup for expressions without wildcards.
  • Restricts direct-import searches to matching importer modules and filters their imports against matching targets.
  • Adds Python and Rust regression cases and documents the optimizations.
File Description
tests/​unit/​application/​graph/​test_direct_imports.py Adds coverage for exact names, wildcards, missing modules, and empty results.
rust/​src/​graph/​hierarchy_queries.rs Adds direct lookup for wildcard-free expressions and associated tests.
rust/​src/​graph/​direct_import_queries.rs Limits import traversal to matching importers and adds regression tests.
CHANGELOG.rst Records both performance improvements under latest.

Review effort: Balanced

@seddonym
seddonym merged commit 185670e into python-grimp:main Nov 13, 2026
24 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.

3 participants