Skip to content

Commit 82a20e7

Browse files
committed
On nightly, use std::thread::current_id()
This avoids the performance problems with std::thread::current(), meaning StdThreadId can just wrap the stdlib without needing a thread_local.
1 parent 24930d4 commit 82a20e7

5 files changed

Lines changed: 54 additions & 10 deletions

File tree

benchmarks/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ description = "Benchmarks for the threadid crate"
44
version = "0.0.0"
55
license = "Apache-2.0 OR MIT"
66
repository = "https://github.com/Techcable/threadid.rs"
7-
publish = false # internal use only
7+
publish = false # internal use only
88
edition = "2021"
99

1010
[dev-dependencies]
1111
threadid.path = ".."
1212
criterion = "0.8"
1313

14+
[build-dependencies]
15+
rustversion = "1"
16+
1417
[[bench]]
1518
name = "access"
1619
required-features = []

benchmarks/benches/access.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![allow(clippy::redundant_closure)] // slightly cleaner
2+
#![cfg_attr(nightly, feature(current_thread_id))]
23

4+
use cfg_if::cfg_if;
35
use criterion::{Criterion, criterion_group, criterion_main};
46
use threadid::{LiveThreadId, StdThreadId, UniqueThreadId};
57

@@ -9,7 +11,19 @@ fn std_current(c: &mut Criterion) {
911
});
1012
}
1113

12-
fn std_id_current(c: &mut Criterion) {
14+
fn std_current_id(c: &mut Criterion) {
15+
cfg_if! {
16+
if #[cfg(has_thread_current_id)] {
17+
c.bench_function("std::thread::current_id()", |x| {
18+
x.iter(|| std::thread::current_id())
19+
});
20+
} else {
21+
let _ = c;
22+
}
23+
}
24+
}
25+
26+
fn threadid_std_current(c: &mut Criterion) {
1327
c.bench_function("threadid::StdThreadId::current()", |x| {
1428
x.iter(|| StdThreadId::current())
1529
});
@@ -27,5 +41,12 @@ fn live_id_current(c: &mut Criterion) {
2741
});
2842
}
2943

30-
criterion_group!(access, std_current, std_id_current, unique_id_current, live_id_current);
44+
criterion_group!(
45+
access,
46+
std_current,
47+
std_current_id,
48+
threadid_std_current,
49+
unique_id_current,
50+
live_id_current
51+
);
3152
criterion_main!(access);

benchmarks/build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub fn main() {
2+
println!("cargo:rustc-check-cfg=cfg(has_thread_current_id)");
3+
println!("cargo:rustc-check-cfg=cfg(nightly)");
4+
if rustversion::cfg!(nightly) {
5+
println!("cargo:rustc-cfg=has_thread_current_id");
6+
println!("cargo:rustc-cfg=nightly");
7+
}
8+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
feature(
3232
// used to make UniqueThreadId match std::thread::ThreadId
3333
thread_id_value,
34+
// used to quickly access the std thread id
35+
current_thread_id,
3436
)
3537
)]
3638
#![cfg_attr(feature = "nightly-docs", feature(doc_cfg))]

src/std.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! Wraps [`std::thread::ThreadId`], providng faster lookup.
22
use core::borrow::Borrow;
3+
#[cfg(not(feature = "nightly"))]
34
use core::cell::Cell;
45
use core::ops::Deref;
56
use std::thread::ThreadId;
67

8+
use cfg_if::cfg_if;
79
use equivalent::Equivalent;
810

11+
#[cfg(not(feature = "nightly"))]
912
fast_thread_local! {
1013
static STD_TID: Cell<Option<StdThreadId>> = Cell::new(None);
1114
}
@@ -24,14 +27,20 @@ impl StdThreadId {
2427
/// Lookup the [`std::thread::ThreadId`] of the current thread.
2528
#[inline]
2629
pub fn current() -> Self {
27-
STD_TID.with(|cell| match cell.get() {
28-
None => {
29-
let new_id = Self::acquire();
30-
cell.set(Some(new_id));
31-
new_id
30+
cfg_if! {
31+
if #[cfg(feature = "nightly")] {
32+
StdThreadId(std::thread::current_id())
33+
} else {
34+
STD_TID.with(|cell| match cell.get() {
35+
None => {
36+
let new_id = Self::acquire();
37+
cell.set(Some(new_id));
38+
new_id
39+
}
40+
Some(existing) => existing,
41+
})
3242
}
33-
Some(existing) => existing,
34-
})
43+
}
3544
}
3645
}
3746
#[cfg_attr(feature = "nightly-docs", doc(cfg(feature = "std")))]
@@ -55,6 +64,7 @@ unsafe impl crate::IThreadId for ThreadId {
5564
}
5665
impl StdThreadId {
5766
#[cold]
67+
#[cfg(not(feature = "nightly"))]
5868
fn acquire() -> StdThreadId {
5969
StdThreadId(std::thread::current().id())
6070
}

0 commit comments

Comments
 (0)