How to increase the stack size available to a Rust library?

6.5k views Asked by At

I am playing with lambda calculus and would like to have a bit more stack space to be able to build and compute (very) long function chains. Is there a way to increase it for the crate, similar to increasing the recursion limit (#![recursion_limit = "100"])?

The crate is a library and I would like it to be able to perform stack-intensive operations regardless of the target operating system.

2

There are 2 answers

0
ljedrz On BEST ANSWER

After some research I concluded that there isn't a universal way to achieve what I am after, but using std::thread::Builder I was able to create an extra thread with a specified stack size and perform stack-heavy operations inside it:

fn huge_reduction() {
    let builder = thread::Builder::new()
                  .name("reductor".into())
                  .stack_size(32 * 1024 * 1024); // 32MB of stack space

    let handler = builder.spawn(|| {
        // stack-intensive operations
    }).unwrap();

    handler.join().unwrap();
}
5
Shepmaster On

This is not a language feature, it's an operating system feature. On *nix systems, you will use a tool like ulimit. Other systems likely use other tools: