Skip to content

Reduce contention in timer driver #6504

@Darksonn

Description

@Darksonn

I have often heard reports about performance issues due to large amounts of timers. Specifically, this is due to contention on the mutex in the timer driver.

Some ideas:

  • If a Sleep instance is dropped before it is polled, its destructor will still lock the mutex to check whether it needs to be removed from the driver. This seems unnecessary, and will be a common case for timeouts.
  • We could shard the timer into multiple instances with one per runtime thread.
  • It's worth thinking about whether our timers are optimized for the case where most timers complete normally (often the case of tokio::time::sleep), or the case where most timers are cancelled before they complete (often the case for timeouts). Can we do anything to differentiate this in the driver?
  • Perhaps a completely different implementation is appropriate?

Before spending a lot of time on a large change, please check with us beforehand by posting here or in Discord so that we can discuss whether your implementation idea is what we want.

PRs related to this issue:

  • Lazily init timers on first poll (#6512)
  • Using sharded locks instead of a global lock for Timers (#6534)
  • Avoid traversing entries in the time wheel twice (#6584)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateE-hardCall for participation: Experience needed to fix: Hard / a lotE-help-wantedCall for participation: Help is requested to fix this issue.M-runtimeModule: tokio/runtimeM-timeModule: tokio/timeT-performanceTopic: performance and benchmarks

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions