Skip to main content

Crate thin_cancellation_token

Crate thin_cancellation_token 

Source
Expand description

§thin-cancellation-token

A minimal async cancellation token.

Benchmarked on Linux x86_64 with an i9-13900K, Rust 1.94.0, and Tokio 1.53.1, using a release build.

Ranges show the point estimates from two runs, with the implementations tested in opposite orders. The ratio is tokio-util time / thin time; values above 1 mean thin is faster.

Operation / scenariothintokio-util 0.7.18Time ratio (tokio-util / thin)
Status check (not cancelled)1.07–1.08 ns7.72–7.84 ns7.19–7.28×
Status check (cancelled)1.08–1.10 ns7.79–7.81 ns7.13–7.20×
Create and drop8.09–8.10 ns34.61–35.29 ns4.27–4.36×
Clone and drop9.96–10.03 ns23.86–24.27 ns2.40–2.42×
First cancellation (no waiters)15.38–15.79 ns21.43–21.74 ns1.38–1.39×
Repeated cancellation4.27–4.36 ns9.54–9.70 ns2.22–2.24×
Create and drop a cancellation future6.26–6.38 ns6.53–6.55 ns1.02–1.05×
Wait on a cancelled token3.39–3.45 ns11.80–11.94 ns3.46–3.48×
Register and remove a waiter31.46–32.03 ns40.51–41.20 ns1.29×
Poll again while pending9.96–10.28 ns18.28–18.78 ns1.83–1.84×
Poll with a new waker17.36–17.74 ns25.34–26.34 ns1.46–1.49×
Notify 1,024 waiters10.03–10.20 µs9.92–10.05 µs0.97–1.00×
Shared status checks: 8 threads, 800,000 per batch0.22–0.31 ms47.20–54.66 ms176.38–218.96×
Tokio, single thread: 1,024 tasks completed122.39–122.89 µs139.41–141.45 µs1.14–1.15×
Tokio, 4 workers: 1 task completed13.17–14.14 µs13.11–13.60 µs0.96–1.00×
Tokio, 4 workers: 1,024 tasks completed149.58–215.31 µs189.76–283.64 µs0.88–1.90×
Token handle8 B8 B—
Heap allocation request (new token)48 B112 B—
Borrowed cancellation future72 B72 B—
async move { token.cancelled().await }88 B88 B—
DropGuard / DropGuardRef (each)8 B8 B—

Structs§

CancellationToken
A one-shot cancellation signal shared by all clones.
DropGuard
An owned guard that cancels its token when dropped, unless disarmed.
DropGuardRef
A borrowed guard that cancels its token when dropped, unless disarmed.
WaitForCancellationFuture
A future that completes when its borrowed token is cancelled.