Skip to content

Replace ast with rust#213

Closed
seddonym wants to merge 8 commits intomasterfrom
replace-ast-with-rust
Closed

Replace ast with rust#213
seddonym wants to merge 8 commits intomasterfrom
replace-ast-with-rust

Conversation

@seddonym
Copy link
Collaborator

@seddonym seddonym commented May 2, 2025

Replaces the use of Python's built in ast module with a custom Rust alternative.

It is faster than ast primarily because it is not a fully implemented Python parser, but just pulls out the imports.

It's based on https://crates.io/crates/pyimportparse, but there were some aspects of it that were inconsistent with how Grimp currently behaves. So for the time being I've copied it in and adapted it.

It's likely that Astral will publish their Python parser as a crate - once that happens we may be able to move to that instead.

@codspeed-hq
Copy link

codspeed-hq bot commented May 2, 2025

CodSpeed Instrumentation Performance Report

Merging #213 will degrade performances by 14.95%

Comparing replace-ast-with-rust (a39e404) with master (89c661c)

Summary

⚡ 2 improvements
❌ 4 regressions
✅ 16 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
test_deep_layers_large_graph_kept 16.7 ms 19.7 ms -14.95%
test_deep_layers_large_graph_violated 10.4 ms 11.8 ms -12.04%
test_no_chain 1.1 ms 1.2 ms -11.05%
test_no_chains 1.1 ms 1.2 ms -11.06%
test_build_django_from_cache_a_few_misses[15] 380.9 ms 153.9 ms ×2.5
test_build_django_from_cache_a_few_misses[2] 166.6 ms 137 ms +21.61%

@seddonym seddonym force-pushed the replace-ast-with-rust branch from 420a424 to b856085 Compare May 2, 2025 15:12
seddonym added 4 commits May 2, 2025 16:17
Prior to this, the parsing behaved differently from the Python-based
parsing in three ways:

1. The line contents only included up to the last matched token, meaning
comments afterwards were excluded.
2. The type_checking conditional did not work for an alias of the
`typing` module, e.g. `if t.TYPE_CHECKING`.
3. Non-ascii Python identifiers weren't supported.

This commit fixes both of those.
@seddonym seddonym force-pushed the replace-ast-with-rust branch from b856085 to 567fd22 Compare May 2, 2025 15:17
@seddonym seddonym force-pushed the replace-ast-with-rust branch from 567fd22 to a39e404 Compare May 2, 2025 16:16
}

@pytest.mark.xfail(strict=True)
def test_multiline_comment_a(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Peter554/pyimportparse@e7d3d0d might be an option

Copy link
Collaborator

Choose a reason for hiding this comment

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

On reflection, I think this might be a better approach -> #214

pyimportparse = "0.2.1"
nom = "8.0.0"
nom_locate = "5.0.0"
nom-regex = "0.2.0"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nom-regex is not being used I think, so we could remove the dep?

@seddonym seddonym closed this May 5, 2025
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