Stream: t-compiler/wg-rls-2.0

Topic: ItemTree uses too much memory


Jonas Schievink (Jul 15 2020 at 18:41, on Zulip):

I've spent some time investigating the pretty big memory usage of ItemTrees today, and tried to optimize the ItemTree representation, but without much success. I collected some data via analysis-bench --memory-usage --highlight src/lib.rs in the rust-analyzer crate:

To help with the latter point, I tried storing all items in a shared arena by converting everything that's stored in the tree to an enum, but this initially just used even more memory due to the enum being as large as the largest contained type. I then used Boxes inside the enum, which helped a bit, but still saved almost nothing.

I think to improve the memory efficiency here we need to do some larger-scale changes. Maybe we need to avoid creating so many small macro-derived item trees in the first place, by side-stepping the item tree in the right places – I briefly chatted with @matklad about this. I'm not sure where these "right places" are though, is the def collector alone enough? And how do we reduce the memory used by non-macro trees? Rely on periodic GC to clean them up? Open for ideas here :)

matklad (Jul 15 2020 at 18:46, on Zulip):

One idea I had is to have a global arena for items

matklad (Jul 15 2020 at 18:47, on Zulip):

I don't think we can implement it without salsa, but it might be a good idea to prototype, to get a rough memory usage

matklad (Jul 15 2020 at 18:48, on Zulip):

Like, literarrly just sticking an Arc<Mutext(Arena<Struct>, Arena<Enum>, ...)> into a DB, and store only ids in ItemTree

matklad (Jul 15 2020 at 18:48, on Zulip):

That should be horribly slow, but should give us a fair assesment of overhead of many arenas

matklad (Jul 15 2020 at 18:49, on Zulip):

It also seems that ItemTrees, while being the heaviset thing, are still just 15% of overall memory usage...

matklad (Jul 15 2020 at 18:49, on Zulip):

I wonder if that means that some wide-reaching optimizations are more important, like interning of names

Jonas Schievink (Jul 15 2020 at 18:50, on Zulip):

Quite possible, yeah

Jonas Schievink (Jul 15 2020 at 18:51, on Zulip):

Maybe that is a good next thing to look into - how much memory is actually occupied by names?

matklad (Jul 15 2020 at 18:59, on Zulip):

Just did a quick check, making Name twice as bit leads to +120mb of memory

matklad (Jul 15 2020 at 19:00, on Zulip):

https://github.com/matklad/rust-analyzer/commit/4df3407d253e606e580ac8fbf1d748a6f44f9cba

Jonas Schievink (Jul 15 2020 at 19:02, on Zulip):

Okay, that seems promising!

Last update: Sep 27 2020 at 13:15UTC