Async large stack allocation

I got this:

 1  warning: this function may allocate 517668 bytes on the stack

This function does something very similar to:

async fn proc_foo(args: Args) {
  let res = match args.cmd {
    Cmd::Foo1 => crate::foo1::run(args).await,
    Cmd::Foo2 => crate::foo2::run(args).await,
    // ...
    Cmd::Foo72 => crate::foo72::run(args).await
  };
  if res.is_err() {
    log::error!("booo");
  }
}

Each future is relatively small, with small return values, but there are many of these calls, so I assume it is making the assumption based on summing up the sizes of the futures?

How optimistically should I read the word "may" in the warning? Should I read it as "This definitely allocates 517668", or should I read it as "in a worst case scenario, this function would have allocated 517668 bytes"?

The lint docs does warn of false positives -- but can I actually verify its size some way?

Found How to compute the size of a returned Future at compile time, without constructing it? - #2 by SkiFire13 answer by @SkiFire13 on how to get the size of a future.

If it turns out to be that big, you should box the future.

1 Like

But this isn't about the size of the future is it? The message said stack specifically. So it would be some temporary thing that doesn't cross await points that could grow that big, or at least that is how I read it. Async is probably a red herring here.

You can definitely get that warning by awaiting a large future.

That seems like a misleading message if it is the future that can grow that big. The compiler doesn't know at this point where the future will ultimately be allocated.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.