Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spawn_pinned is substantially more expensive than other spawn mechanisms #4819

Open
futursolo opened this issue Jul 10, 2022 · 1 comment
Open
Labels
A-tokio-util Area: The tokio-util crate C-bug Category: This is a bug. M-task Module: tokio/task

Comments

@futursolo
Copy link

Version

  • tokio v1.19.2
  • tokio-macros v1.8.0 (proc-macro)
  • tokio-util v0.7.3
  • tokio v1.19.2 (*)

Platform

I have tested the following targets and seems like this behaviour is prevalent on most UNIX platforms.

  • aarch64-apple-darwin
  • x86_64-apple-darwin
  • aarch64-unknown-linux-gnu
  • x86_64-unknown-linux-gnu

Description

If LocalPoolHandle::spawn_pinned is used to spawn a future that will be ready immediately (e.g.: async {}), it will take a significantly longer time to complete the task comparing to tokio::task::spawn. However, if a different future that will become temporarily pending is used (e.g.: tokio::time::sleep(Duration::ZERO)), it will be completed with a similar time to tokio::task::spawn.

I wrote a simple test to measure the time used to spawn 1 million tasks and joining them and got the following result:

https://github.com/futursolo/tokio-task-spawn-test/blob/master/src/main.rs

Testing Ready Future...
  Round: 1
    futures ordered: 107ms
        spawn local: 133ms
       spawn pinned: 1221ms
              spawn: 332ms

  Round: 2
    futures ordered: 64ms
        spawn local: 136ms
       spawn pinned: 1402ms
              spawn: 320ms

  Round: 3
    futures ordered: 75ms
        spawn local: 127ms
       spawn pinned: 1127ms
              spawn: 321ms

  Round: 4
    futures ordered: 73ms
        spawn local: 127ms
       spawn pinned: 1455ms
              spawn: 330ms

  Round: 5
    futures ordered: 69ms
        spawn local: 139ms
       spawn pinned: 1447ms
              spawn: 320ms

Testing sleep(Duration::ZERO) Future...
  Round: 1
    futures ordered: 449ms
        spawn local: 474ms
       spawn pinned: 747ms
              spawn: 926ms

  Round: 2
    futures ordered: 467ms
        spawn local: 505ms
       spawn pinned: 715ms
              spawn: 813ms

  Round: 3
    futures ordered: 410ms
        spawn local: 470ms
       spawn pinned: 725ms
              spawn: 833ms

  Round: 4
    futures ordered: 409ms
        spawn local: 478ms
       spawn pinned: 731ms
              spawn: 838ms

  Round: 5
    futures ordered: 399ms
        spawn local: 478ms
       spawn pinned: 727ms
              spawn: 826ms
@futursolo futursolo added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Jul 10, 2022
@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-task Module: tokio/task and removed A-tokio Area: The main tokio crate labels Jul 10, 2022
@Darksonn
Copy link
Contributor

Well, I am not surprised that spawn_pinned is the slowest. I would be happy to accept alternate implementations if they are faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate C-bug Category: This is a bug. M-task Module: tokio/task
Projects
None yet
Development

No branches or pull requests

2 participants