#Checking if the user provided future returns immediately before spawning it

6 messages · Page 1 of 1 (latest)

modest kettle
#

Hey, is it worth it/doable to poll a given future one time to check if it returns immediately before spawning it?

Or Tokio is already doing this kind of thing? I'm in a scenario where 50% of the user provided futures might not have any wait point.

solemn prawn
cosmic jungle
#

it sounds like what you want is to poll it once and then spawn it if not immediately ready

so you can pin it and poll it once, and then spawn it if it returns pending

you probably want futures::poll! rather than now or never for that case
https://docs.rs/futures/latest/futures/macro.poll.html

#

this is indeed a pattern i've used, for handling futures that call a backend service except may be cached on host and not need a sleep

#

it also is nice to make sure the remote i/o kicks off immediately (and then sleeps) instead of adding scheduling delay before the task is first polled

#

if your futures aren't already Unpin, like alphakeks said, you'll probably need to Box::pin them instead of just naked pinning - which does involve an allocation, so it's not zero cost (but also spawning has its own overhead)