windows-gnu: every access to a thread_local with Drop contends on a process-wide atomic since 1.98 (~100x slower across threads)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 43/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- rust
- Lĩnh vực
- operating-systems, performance
Hướng nghiên cứu
Start with library/std/src/sys/thread_local/key/windows.rs and guard/windows.rs, especially LazyKey::force, guard::enable, and EnableGuard. Check how the Windows GNU and MSVC configurations track whether a thread's guard is armed, then reproduce the reported contention with the supplied benchmark on x86_64-pc-windows-gnu. Done means accesses to destructible thread-locals no longer contend on the process-wide atomic while thread destructor behavior remains correct.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Since 1.98.0, on x86_64-pc-windows-gnu, every access to a thread_local! whose type needs Drop costs about 10x more on one thread, and when many threads touch such thread-locals at once it becomes ~100x slower: all of them contend on one process-wide atomic. 1.97.0 is not affected.
Code
use std::cell::RefCell;
use std::time::Instant;
thread_local! {
// Any thread-local whose type needs Drop.
static LOCAL: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
}
fn touch(n: usize) {
for _ in 0..n {
LOCAL.with(|v| std::hint::black_box(v.borrow().len()));
}
}
fn main() {
const N: usize = 2_000_000;
let t = Instant::now();
touch(N);
println!("1 thread: {:7.1} ns per access", t.elapsed().as_nanos() as f64 / N as f64);
let threads = std::thread::available_parallelism().map_or(8, |n| n.get());
let t = Instant::now();
std::thread::scope(|s| {
for _ in 0..threads {
s.spawn(|| touch(N));
}
});
println!("{threads} threads: {:7.1} ns per access (wall time / accesses per thread)", t.elapsed().as_nanos() as f64 / N as f64);
}
rustc -C opt-level=3 repro.rs, target x86_64-pc-windows-gnu, i7-12700H (20 logical CPUs), Windows 11. Two runs each:
| rustc | 1 thread, ns per access | 20 threads at once, ns per access |
|---|---|---|
| 1.95.0 | 2.4 / 2.6 | 15.6 / 18.1 |
| 1.97.0 | 2.3 / 2.3 | 25.0 / 24.0 |
| 1.98.0 | 23.1 / 22.0 | 1353.7 / 1305.1 |
| 1.100.0 | 23.9 / 23.1 | 1522.2 / 1515.6 |
Likely cause
#148799 (milestone 1.98.0) moved TLS destructors to FLS. windows-gnu has no #[thread_local] (target_thread_local is off), so thread-locals with destructors go through LazyKey, and LazyKey::force calls guard::enable() on every access (library/std/src/sys/thread_local/key/windows.rs):
pub fn force(&'static self) -> Key {
if self.dtor.is_some() {
// Needs to be called on all threads where the key might have a non-null value!
// Otherwise, `run_dtors` might not be called on this thread.
guard::enable();
}
...
In guard::enable (guard/windows.rs) the "already registered on this thread" check exists only under cfg!(target_thread_local). On windows-gnu registered is always false, so every access runs EnableGuard::new() (a fetch_add on the process-wide ACTIVE_ENABLE_CALLS and a SeqCst fence), KEY.load, IsThreadAFiber(), FlsSetValue(), and the fetch_sub in EnableGuard's Drop. The comment there expects setting the FLS value to be about as cheap as TlsGetValue, but the two RMWs on one shared counter put every thread on the same cache line.
Impact
Code that touches such thread-locals on hot paths from many threads slows down a lot. rayon is one: crossbeam-deque pins crossbeam-epoch (a thread-local handle with Drop) on every steal. With rayon 1.12 / rayon-core 1.13 on a 20-thread pool, one par_iter().sum() over 256 f32s takes 29–43 µs with 1.95.0 and 266–340 µs with 1.100.0. In a real application, per-frame CPU work made of ~20 such small parallel reductions went from ~4 ms to ~7.5 ms, while single-threaded code did not change.
x86_64-pc-windows-msvc should not be affected, since there the REGISTERED thread-local makes enable() return early (not measured).
A possible fix: on windows-gnu, find out without touching shared state that this thread's guard is already armed, e.g. check FlsGetValue(key) before EnableGuard::new(), or call guard::enable() only when a key's value is first set on a thread rather than on every force().
@rustbot label +O-windows-gnu +I-slow +T-libs +A-thread-locals +regression-from-stable-to-stable
- Ngôn ngữ chính
- Rust
- Star
- 122k
- Fork
- 16.7k
- Merge trung bình
- 2 ngày 16 giờ
- Pull request đã merge (30 ngày)
- 515
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rust-lang/rust
-
Remove unnecessary parentheses when suggesting to remove '&' from a patternCó thể đã có người làm @BartSimpson001 đã nhận 1 ngày trước. Đang mởA-diagnostics D-invalid-suggestion T-compiler
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Windows `File::set_permissions` silently does nothing when clearing `readonly` leaves `FileAttributes == 0`Có thể đã có người làm @ChrisDenton đã nhận 11 ngày trước. Đang mởA-filesystem C-bug O-windows T-libs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
A-run-make relnotes relnotes-needs-review relnotes-tracking-issue T-lang T-libs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
A-LLVM O-Arm O-UEFI O-x86_64 relnotes relnotes-needs-review relnotes-tracking-issue T-compiler
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
rust-lang/rust#165504 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
relnotes relnotes-tracking-issue T-libs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của rust-lang/rust
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
antithesishq/bombadil#473 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
bench/api.sh exec test fails with curl exit 52, and --ignore-failure still records the timingsĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
libracoder/kiri#669 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 5 ngày
-
autofixer: follow-up
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
tursodatabase/turso#12217 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Declare the CMS schema library in the optional recipients extraCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởarea:python in-progress
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
yhay81/pyropdf#1005 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày