Faster alloc/drop without lifetime?

Hi. I'm the author of the SWC project. https://swc.rs/

It's an ECMAScript compiler, and I have performance problems with it. It holds AST nodes, and AST nodes are inherently recursive. And it holds many Box<T>s and Vec<T>s.

Currently, memory allocation and deallocation take a noticeable amount of time. So I'm considering using a crate like bumpalo, but it requires enormous amount of work because I have to add lifetimes to all AST nodes.
So, I want to know if there's an alternative way to improve performance in this case.

Are these traits exploitable in another way?

  • Drop implementations of AST nodes have no side effects other than deallocating memory.
  • Single Program (top-level AST node) is allocated almost linearly in the memory, while parsing.

What global allocator are you using?

I'm using mimalloc

To have a struct without a lifetime bound, it must be in charge of how long its data lives, independently of anything else. For this it needs to either use the global allocator or some form of reference counting.

If a struct borrows data from a pool, then it will have to have a lifetime that ensures it cannot be used outside of the scope in which the pool exists. Lifetimes will necessarily "infect" every use, because Rust can't make any reference live longer, and must always ensure that any use anywhere is limited to that scope.

If your program is short lived, and will always be short lived, and if there is a lot of data that lives until the end or near the end of the program runtime, you could choose to simply leak memory. The OS will be able to free up the entire memory much faster than your program freeing the allocations one by one. It also has the nice side effect that you can just work with 'static references everywhere and not worry about lifetimes because the allocations will last as long as your program runs. It might also help avoid copying (due to moves or clones) because you'll be working with references. It might also enable you to use your own version of a bump allocator: at the beginning, allocate and leak an appropriately large arena, then put your stuff in there.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.