2

General performance advice for Rust is to try to avoid placing things on the heap if possible.

An issue I am having is that I do not know where/when the size limit of a function stack will be reached, until my program panics unpredictably at runtime.

Two examples are:

  • Parsing deeply nested structs from JSON using Serde.
  • Creating many futures inside a function.

Questions:

  • Can I avoid this by detecting it at compile time?

  • How can I know what the limit of the stack is whilst I am writing code? Do others just know the exact size of their variables?

  • Why do people advise to try to avoid the heap?

zino
  • 1,222
  • 2
  • 17
  • 47
  • 4
    You can't detect this. You can avoid it in some languages by using tail calls, but Rust does not guarantee these will be optimised. Instead try to avoid unbounded recursion altogether. – Peter Hall Nov 01 '21 at 14:03
  • 2
    There are lots of existing answers about heap vs stack. The short answer is "performance", but you should easily be able to find complete discussions on that topic on SO. – Peter Hall Nov 01 '21 at 14:04
  • 1
    Avoid the recursion by placing it into a Box instead of the stack? I cannot avoid the recursion if it is part of the essential complexity (e.g recursive JSON structures). – zino Nov 01 '21 at 14:08
  • 2
    You are asking a very general question, which will have different answers depending on the specifics. – Peter Hall Nov 01 '21 at 14:10
  • 4
    @zino you can implement any recursive algorithm iteratively by tracking state in a heap allocated stack data structure instead of local variables in call stack frames. – yuri kilochek Nov 01 '21 at 14:13
  • 2
    @zino Have you actually encountered the stack-limit panics, or are you asking this based on advice you've seen elsewhere? If it's the former, do you run in release or in debug mode? – user4815162342 Nov 01 '21 at 14:20
  • 1
    "Can I avoid this by detecting it at compile time?" it's a non trivial problem to "prove" the maximun stack space a program will use, This first require to prove your program work for every case. – Stargateur Nov 01 '21 at 14:27
  • 1
    @user4815162342 Yes I have hit this issue a few times, each time I Box something. Release mode does fix it sometimes, but is not a solution as I need debug mode to develop my program. – zino Nov 01 '21 at 15:58
  • @yurikilochek Thanks, any links for "heap allocated stack data structure" examples? – zino Nov 01 '21 at 15:58
  • 3
    @zino Maybe [this crate](https://github.com/dtolnay/serde-stacker) would help with serde? – user4815162342 Nov 01 '21 at 16:31
  • 1
    I wish I had a dime for every developer who invoked the old adage, "you can implement any recursive algorithm iteratively", and didn't provide an example. It is not that easy to convert most recursive algorithms using a stack especially if there are multiple recursive calls in the same function. Next developer who says this has to try it. I did, and [I posted a generalized method](https://stackoverflow.com/a/68872963/7915759). – Todd Nov 01 '21 at 17:37
  • On Unix-like operating systems, you can also use `ulimit` to increase the stack size for you program, which might help with debugging. – Sven Marnach Nov 02 '21 at 13:12

0 Answers0