Skip to content

Proper handling $crate and local_inner_macros #7133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 2, 2021

Conversation

edwin0cheng
Copy link
Member

@edwin0cheng edwin0cheng commented Jan 2, 2021

This PR introduces HygineFrames to store the macro definition/call site hierarchy in hyginee and when resolving local_inner_macros and $crate, we use the token to look up the corresponding frame and return the correct value.

See also: https://rustc-dev-guide.rust-lang.org/macro-expansion.html#hygiene-and-hierarchies

fixe #6890 and #6788

r? @jonas-schievink

Copy link
Contributor

@jonas-schievink jonas-schievink left a comment

Choose a reason for hiding this comment

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

Great!

@jonas-schievink
Copy link
Contributor

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 2, 2021

@bors bors bot merged commit a88d4f8 into rust-lang:master Jan 2, 2021
@lnicola
Copy link
Member

lnicola commented Jan 2, 2021

This has substantially slowed down the analysis:

image

Confirmed locally:

Database loaded 517.72ms, 284mi
Crates in this dir: 36
Total modules found: 576
Total declarations: 11153
Total functions: 8715
Item Collection: 9.38s, 93528mi
Total expressions: 239953                                                                                                                          
Expressions of unknown type: 2908 (1%)
Expressions of partially unknown type: 2578 (1%)
Type mismatches: 1200
Inference: 27.92s, 252342mi
Total: 37.30s, 345870mi

Database loaded 521.35ms, 284mi
Crates in this dir: 36
Total modules found: 576
Total declarations: 11146
Total functions: 8710
Item Collection: 7.08s, 74434mi
Total expressions: 238134                                                                                                                          
Expressions of unknown type: 2981 (1%)
Expressions of partially unknown type: 2640 (1%)
Type mismatches: 1193
Inference: 12.29s, 99177mi
Total: 19.37s, 173611mi

@edwin0cheng edwin0cheng deleted the hygiene-frame branch January 2, 2021 19:07
@edwin0cheng
Copy link
Member Author

Maybe we could store HygineFrames in salsa ?

@jonas-schievink
Copy link
Contributor

Unfortunately ExpansionInfo contains AST nodes, so persisting that in the database doesn't work (and would be a bad idea, since they're pretty heavy).

I think we should consider reverting this until we find a solution.

@edwin0cheng
Copy link
Member Author

edwin0cheng commented Jan 3, 2021

The original rustc implementation used TLS to store the hygiene information and I did try it and it improves speed a lot. But I don't feel it is the right solution for us.

I opened https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Hygiene.20Frames.20Information for the discussion.

bors bot added a commit that referenced this pull request Jan 3, 2021
7137: Revert "Proper handling $crate and local_inner_macros" r=jonas-schievink a=jonas-schievink

Reverts #7133

It caused a fairly significant performance regression.

bors r+

Co-authored-by: Jonas Schievink <[email protected]>
bors bot added a commit that referenced this pull request Jan 8, 2021
7145: Proper handling $crate Take 2 [DO NOT MERGE] r=edwin0cheng a=edwin0cheng

Similar to previous PR (#7133) , but improved the following things :

1. Instead of storing the whole `ExpansionInfo`, we store a similar but stripped version `HygieneInfo`.
2. Instread of storing the `SyntaxNode` (because every token we are interested are IDENT), we store the `TextRange` only.
3. Because of 2, we now can put it in Salsa.
4. And most important improvement: Instead of computing the whole frames every single time, we compute it recursively through salsa: (Such that in the best scenario, we only need to compute the first layer of frame)

```rust
        let def_site = db.hygiene_frame(info.def.file_id);
        let call_site = db.hygiene_frame(info.arg.file_id);

        HygieneFrame { expansion: Some(info), local_inner, krate, call_site, def_site }
```

The overall speed compared to previous PR is much faster (65s vs 45s) :
```
[WITH old PR]
Database loaded 644.86ms, 284mi
Crates in this dir: 36
Total modules found: 576
Total declarations: 11153
Total functions: 8715
Item Collection: 15.78s, 91562mi
Total expressions: 240721
Expressions of unknown type: 2635 (1%)
Expressions of partially unknown type: 2064 (0%)
Type mismatches: 865
Inference: 49.84s, 250747mi
Total: 65.62s, 342310mi
rust-analyzer -q analysis-stats .  66.72s user 0.57s system 99% cpu 1:07.40 total

[WITH this PR]
Database loaded 665.83ms, 284mi
Crates in this dir: 36
Total modules found: 577
Total declarations: 11188
Total functions: 8743
Item Collection: 15.28s, 84919mi
Total expressions: 241229
Expressions of unknown type: 2637 (1%)
Expressions of partially unknown type: 2064 (0%)
Type mismatches: 868
Inference: 30.15s, 135293mi
Total: 45.43s, 220213mi   
rust-analyzer -q analysis-stats .  46.26s user 0.74s system 99% cpu 47.294 total
```

*HOWEVER*,  it is still a perf regression (35s vs 45s):
```
[WITHOUT this PR]
Database loaded 657.42ms, 284mi
Crates in this dir: 36
Total modules found: 577
Total declarations: 11177
Total functions: 8735
Item Collection: 12.87s, 72407mi
Total expressions: 239380
Expressions of unknown type: 2643 (1%)
Expressions of partially unknown type: 2064 (0%)
Type mismatches: 868
Inference: 22.88s, 97889mi
Total: 35.74s, 170297mi
rust-analyzer -q analysis-stats .  36.71s user 0.63s system 99% cpu 37.498 total
```



Co-authored-by: Edwin Cheng <[email protected]>
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.

None yet

3 participants