News for hackers. Stuff that matters.
Re:How much memory do you need in 2024 to run 1M concurrent ... by davidatbu on Thursday November 28, 2024 @11:23PM (#42270771)
I write (async) Rust regularly, and I don't understand how the version in the appendix doesn't take 10x1,000,000 seconds to complete. In other words, I'd have expected no concurrency to take place.

Am I wrong?

UPDATE: From the replies below, it looks like I was right about "no concurrency takes place", but I was wrong about how long it takes, because `tokio::time::sleep()` keeps track of when the future was created, (ie when `sleep()` was called) instead of when the future is first `.await`ed (which was my unsaid assumption).

Re:How much memory do you need in 2024 to run 1M concurrent ... by claytonwramsey on Thursday November 28, 2024 @11:29PM (#42270807)
The implementation of `sleep` [1] decides the wake up time by when `sleep` is called, rather than when its future is polled. So the first task waits one second, then the remaining tasks see that they have already passed the wake-up time and so return instantly.

[1]: https://docs.rs/tokio/latest/tokio/time/fn.sleep.html

Re:How much memory do you need in 2024 to run 1M concurrent ... by davidatbu on Thursday November 28, 2024 @11:41PM (#42270863)
This makes total sense!
Re:How much memory do you need in 2024 to run 1M concurrent ... by ch33zer on Thursday November 28, 2024 @11:26PM (#42270786)
Tokyo::sleep is async
Re:How much memory do you need in 2024 to run 1M concurrent ... by davidatbu on Thursday November 28, 2024 @11:43PM (#42270876)
I think the points people made in other replies make sense, but "Tokio::sleep is async" by itself is not enough of an explanation. If it were the case that `Tokio::sleep()` tracked the moment `.await` was called as it's start time, I believe it would indeed take 10x1,000,000 seconds, _even if it's async_.
Re:How much memory do you need in 2024 to run 1M concurrent ... by fuzzybear3965 on Thursday November 28, 2024 @11:34PM (#42270831)
Yeah, I think you're wrong. It should only take ~10s. tokio::time::sleep records the time it was called before returning the future [1]. So, all 1 million tasks should be stamped with +/- the same time (within a few milliseconds).

[1]: https://docs.rs/tokio/1.41.1/src/tokio/time/sleep.rs.html#12...

Re:How much memory do you need in 2024 to run 1M concurrent ... by davidatbu on Thursday November 28, 2024 @11:41PM (#42270866)
This makes total sense!
Re:How much memory do you need in 2024 to run 1M concurrent ... by vbsd on Friday November 29, 2024 @01:41AM (#42271479)
> because `tokio::time::sleep()` keeps track of when the future was created, (ie when `sleep()` was called) instead of when the future is first `.await`ed

I’m not a Rust programmer but I strongly suspect this updated explanation is erroneous. It’s probably more like this: start time is recorded when the task execution is started. However, the task immediately yields control back to the async loop. Then the async loop starts another task, and so on. It’s just that the async loop only returns the control to sleeping task no earlier than the moment 1s passes after the task execution was initialy started. I’d be surprised if it had anything to do with when sleep() was called.

Re:How much memory do you need in 2024 to run 1M concurrent ... by davidatbu on Friday November 29, 2024 @03:11AM (#42271970)
Someone linked the code in another comment, and the start time is most definitely recorded when the future is created: https://docs.rs/tokio/1.41.1/src/tokio/time/sleep.rs.html#12...
Re:How much memory do you need in 2024 to run 1M concurrent ... by vbsd on Friday November 29, 2024 @09:39AM (#42274172)
Huh, you're right about this, thanks.

On the other hand, I maintain that this is an incidental rather than essential reason for the program finishing quickly. In that benchmark code, we can replace "sleep" with our custom sleep function which does not record start time before execution:

  async fn wrapped_sleep(d: Duration) {
      sleep(d).await
  }

The following program will still finish in ~10 seconds.

  #[tokio::main]
  async fn main() {
      let num_tasks = 100;
      let mut tasks = Vec::new();
      for _ in 0..num_tasks {
          tasks.push(wrapped_sleep(Duration::from_secs(10)));
      }
      futures::future::join_all(tasks).await;
  }
Re:How much memory do you need in 2024 to run 1M concurrent ... by davidatbu on Saturday November 30, 2024 @10:17AM (#42282047)
In `wrapped_sleep` function body, where does the `sleep()` come from? It's still tokio::time::sleep, right? If so, the start time is recorded before the first `.await`.

Regardless, the program you provided _does_ actually run the futures concurrently, because of the `join_all()`. My point above was that in the original blog post, the appendix has a version without `join_all()`, which has no concurrency.

Re:How much memory do you need in 2024 to run 1M concurrent ... by vbsd on Saturday November 30, 2024 @01:15PM (#42283074)
Ah, I missed that you were talking about the code in the appendix, not the top examples. Yeah, you’re right on every count then, aplologies.