Rust stack usage is bad
For a little bit of context, I'm developing a kernel entirely written in rust. Everything was going to plan, I got the memory map, GDT, IDT, TSS setup, serial drivers, NVME drivers until I went into paging. See usually, most of my kernels at max were using around 2 pages worth of stack (or 4096\*2 bytes in total) which seems like a fairly reasonable size given that the kernel is really only meant to just initialize some stuff before starting the processes and drivers that really take up more memory themselves or allocate any big buffers in the memory map.
(The following is just information I've quickly found online about the compiler + some of my limited testing, so please correct me if I get anything wrong with how the compiler works):
That assumption was thrown out the window, because as it turns out, the rust compiler uses A LOT of stack allocations. The rust compiler relies on the idea that LLVM will optimize out any alloca's that aren't needed.
I disassembled just my entry function alone and I got the following information out of it:
In debug mode, the \_start function used 3 pages worth of stack memory
In release mode, the \_start function only used 1 page worth of stack memory
This significant difference in stack memory usage is due to LLVM optimizing out a lot of the unnecessary allocas that happen throughout the code. That however means that running my kernel in debug mode makes it almost impossible to work with, unless I preemptively increase the stack size by a lot of pages, and even then I can't be sure that it would be enough, given I don't know the exact maximum stack size the kernel will need.
Some things that has helped me reduce the stack memory usage is not using a lot of let wrappers and overwriting variables that you end up only using once. Another thing is using less #\[inline\] like suggested in one of the comments on this this post
A lot of these get in the way of a lot of the pre-existing code and/or make it really hard to refactor it to use less stack allocations. I feel like the Rust compiler needs to optimize out more stack allocations before passing things to LLVM and is still in a pretty immature state to be used in this scenario. I haven't given up on the idea of a fully rustified kernel, but it just seems like rust might not be the right tool for the job.
I'm open to any and all suggestions on how to fix some of these problems, your personal experience with it or just general information on how to optimize this kind of stuff and any errors I made in this rant about how the compiler works.
Thank you for reading my post