The size of Rust Futures

submited by
Style Pass
2023-01-27 08:00:09

I have recently discovered that Rust Futures, or rather, async fn calls can lead to surprising performance problems if they are nested too deeply.

I learned that the hard way by triggering a stack overflow in a PR to symbolicator. I then tracked that down to unreasonably huge (as measured by mem::size_of_val) futures. The problem was also being exacerbated by the deeply nested async fn calls in the moka crate that I have reported here.

It is not my intention to bash that specific crate here, as I absolutely love its intuitive APIs, and would love to use it even more in the future. However the crate does make some wrong assumptions about how async Rust code works that I am sure not a lot of people are aware of, and which can cause problems.

Apart from highlighting the source of the problem in great depth, I also want to propose some workarounds for this specific issue.

The problem I will highlight is also only present in todays Rust (nightly 1.68). It is perfectly possible that the compiler will optimize these things in the future.

Leave a Comment