Skip to content
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

Store token trees in contiguous Vec instead of as a tree #18327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ChayimFriedman2
Copy link
Contributor

As part of my attempts to optimize #18074, I noticed that token trees contain a significant portion of our memory usage (around 200mb out of 1650mb). As such, I went to optimize them.

TokenTree is quite large (64 bytes), and a significant part of this is spans. I came up an idea to shrink spans, but it will be easier to do if token trees were contiguous in memory. Given that this seemed like something worth doing perf-wise anyway, I went to do this :)

I expected this to be faster (due to less allocations and better cache locality), but benchmarked it is not (neither it is slower). I guess tt construction is just not hot. Memory usage, however, drops by ~50mb (of analysis-stats .), probably due to TokenTree shrinking to 48 bytes.

Some workflows are more easily expressed with a flat tt, while some are better expressed with a tree. With the right helpers, though (which was mostly a matter of trial and error), even the worst workflows become very easy indeed.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 18, 2024
@Veykril
Copy link
Member

Veykril commented Oct 18, 2024

Related https://rust-lang.zulipchat.com/#narrow/channel/185405-t-compiler.2Frust-analyzer/topic/mbe.20transcription I tried this before (i also saw no perf impact like you are, with a slight memory usage decrease iirc but the branch seems to be lost now), for some reason I was unable to actually integrate it without rewriting the mbe crate at the time though. Cant recall why that was. But neat either way. Though I guess this will heavily clash with #17830 now 😬 (wonder when Ill be able to get back to that PR, probably not this year)

@ChayimFriedman2
Copy link
Contributor Author

for some reason I was unable to actually integrate it without rewriting the mbe crate at the time though. Cant recall why that was

Perhaps that was the hardness of traversing the tree correctly? It was very ugly in my early attempts, but when I discovered the secret sauce (a TtIter that yields nested TtIters, and heavy usage of it) it became just neat, and as a bonus Cursor became significantly easier :)

@Veykril
Copy link
Member

Veykril commented Oct 18, 2024

Might very well have been the traversal yes

@bors
Copy link
Contributor

bors commented Oct 21, 2024

☔ The latest upstream changes (presumably #17954) made this pull request unmergeable. Please resolve the merge conflicts.

I expected this to be faster (due to less allocations and better cache locality), but benchmarked it is not (neither it is slower). Memory usage, however, drops by ~50mb (of `analysis-stats .`). I guess tt construction is just not hot.

This also simplifies using even less memory for token trees by compressing equal span, which I plan to do right after.

Some workflows are more easily expressed with a flat tt, while some are better expressed with a tree. With the right helpers, though (which was mostly a matter of trial and error), even the worst workflows become very easy indeed.
@bors
Copy link
Contributor

bors commented Oct 22, 2024

☔ The latest upstream changes (presumably #18366) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants