threadlocal count for join/heatbeat

This commit is contained in:
Janis 2025-06-20 12:16:51 +02:00
parent 9b0cc41834
commit 940c681222

View file

@ -1013,7 +1013,7 @@ mod job {
use std::{ use std::{
any::Any, any::Any,
cell::UnsafeCell, cell::{Cell, UnsafeCell},
collections::BTreeMap, collections::BTreeMap,
future::Future, future::Future,
hint::cold_path, hint::cold_path,
@ -1084,8 +1084,6 @@ struct WorkerThread {
} }
pub struct Scope<'scope> { pub struct Scope<'scope> {
// counter for join_every_* function
join_count: AtomicUsize,
// latch to wait on before the scope finishes // latch to wait on before the scope finishes
job_counter: JobCounter, job_counter: JobCounter,
// local threadpool // local threadpool
@ -1572,9 +1570,20 @@ impl<'scope> Scope<'scope> {
A: FnOnce(&Self) -> RA + Send, A: FnOnce(&Self) -> RA + Send,
B: FnOnce(&Self) -> RB + Send, B: FnOnce(&Self) -> RB + Send,
{ {
let count = self.join_count.load(Ordering::Relaxed); thread_local! {
self.join_count static JOIN_COUNT: Cell<usize> = Cell::new(0);
.store(count.wrapping_add(1) % TIMES, Ordering::Relaxed); }
// a threadlocal counter is much faster than a sync atomic counter
let count = JOIN_COUNT.with(|count| {
count.set(count.get().wrapping_add(1) % TIMES);
count.get()
});
// let count = self.join_count.load(Ordering::Relaxed);
// self.join_count
// .store(count.wrapping_add(1) % TIMES, Ordering::Relaxed);
// let count = self // let count = self
// .join_count // .join_count
// .update(Ordering::Relaxed, Ordering::Relaxed, |n| { // .update(Ordering::Relaxed, Ordering::Relaxed, |n| {
@ -1645,7 +1654,6 @@ impl<'scope> Scope<'scope> {
fn from_context(ctx: Arc<Context>) -> Self { fn from_context(ctx: Arc<Context>) -> Self {
Self { Self {
context: ctx, context: ctx,
join_count: AtomicUsize::new(0),
job_counter: JobCounter::default(), job_counter: JobCounter::default(),
panic: AtomicPtr::new(ptr::null_mut()), panic: AtomicPtr::new(ptr::null_mut()),
_pd: PhantomData, _pd: PhantomData,