-
Notifications
You must be signed in to change notification settings - Fork 69
Description
I noticed a comment in the InflateState::new() function:
Note that this struct is quite large due to internal buffers, and as such storing it on the stack is not recommended.
The size of the struct is about 43KB. Probably not a huge issue for Windows or Linux desktops where you have 1MB+ of stack space but maybe an issue for embedded platforms.
The solution seems to be to use the new_boxed() function. However, this results in a temporary stack allocation of the entire size of the InflateState struct regardless. The function calls Box::default() to do the heap allocation which has a definition of
fn default() -> Self {
Box::new(T::default())
}This first constructs InflateState on the stack and then moves it to the heap. I was able to confirm this by using rust-gdb to examine the assembly code. I will note that a release build on x86_64 Linux appears to optimize this to first allocate the memory and then initialize directly on the heap but a debug build does not. Other platforms also may not have this optimization.
The only solutions I can think of off the top of my head is unsafe code doing manual allocation or moving some of the larger fixed size arrays to Vec. Neither is really ideal since it seems to be a goal of this project to not use unsafe and Vec would result in some additional overhead. Still figured I would raise this issue in case someone has a better idea.