5

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.

ljedrz
  • 20,316
  • 4
  • 69
  • 97
  • 3
    Do you want to increase the stack size of the stack the compiler operates on **or** the [stack of *your* program](http://stackoverflow.com/questions/29937697/how-to-set-the-thread-stack-size-during-compile-time)? – Lukas Kalbertodt May 16 '17 at 14:16
  • @LukasKalbertodt the issue occurred during cargo tests and the overflowed thread was `libname-hash.exe`, so I guess it's the program after all. – ljedrz May 16 '17 at 16:40

2 Answers2

9

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();
}
ljedrz
  • 20,316
  • 4
  • 69
  • 97
4

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:

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 7
    @ljedrz: I'll note that [`std::thread::Builder`](https://doc.rust-lang.org/1.8.0/std/thread/struct.Builder.html) let you specify the stack size of the created thread from within the program. Only the stack size of the *main* thread is set by the OS. – Matthieu M. May 16 '17 at 15:21
  • @MatthieuM. can you really set a stack size of like 256MB that way? Also, linking to the Rust 1.8 documentation? It's much better to link to [the current docs](https://doc.rust-lang.org/std/thread/struct.Builder.html), which clarifies what *unit* `stack_size` takes. – Shepmaster May 16 '17 at 15:31
  • @Shepmaster: I blame Google for the link ;) As for the maximum stack size, it may indeed be limited by the OS, or by the underlying thread implementation, but you *can* tune it from within Rust. – Matthieu M. May 16 '17 at 15:35
  • @MatthieuM. I was under the impression that `ulimit` (and other OS equivalents) was what controlled the maximum; do you know if that's true? – Shepmaster May 16 '17 at 15:37
  • @Shepmaster: No idea, the only times I've hit a stackoverflow was because of an unbounded recursion, so the fix was in the logic, not in the OS. I don't use FreeBSD though, which is notorious for its small stacks (1MB or 2MB by default I think). – Matthieu M. May 16 '17 at 15:43