Skip to content

Commit f98eb57

Browse files
committed
Improve inlining of scope latch counters
The `increment` and `set` methods now have `#[inline]` hints, and the `counter` fields in `CountLatch` and `CountLockLatch` are now listed first to increase the chance that layout puts them at the same offset. (That layout is not critical to ensure, but works out nicely.)
1 parent 85dd75d commit f98eb57

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

rayon-core/src/latch.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ impl Latch for LockLatch {
286286
/// contexts).
287287
#[derive(Debug)]
288288
pub(super) struct CountLatch {
289-
core_latch: CoreLatch,
289+
// counter is first to nudge layout like CountLockLatch
290290
counter: AtomicUsize,
291+
core_latch: CoreLatch,
291292
}
292293

293294
impl CountLatch {
@@ -347,8 +348,9 @@ impl AsCoreLatch for CountLatch {
347348

348349
#[derive(Debug)]
349350
pub(super) struct CountLockLatch {
350-
lock_latch: LockLatch,
351+
// counter is first to nudge layout like CountLatch
351352
counter: AtomicUsize,
353+
lock_latch: LockLatch,
352354
}
353355

354356
impl CountLockLatch {

rayon-core/src/scope/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ impl<'scope> ScopeBase<'scope> {
655655
}
656656
}
657657

658+
#[inline]
658659
fn increment(&self) {
659660
self.job_completed_latch.increment();
660661
}
@@ -710,17 +711,15 @@ impl<'scope> ScopeBase<'scope> {
710711
where
711712
FUNC: FnOnce() -> R,
712713
{
713-
match unwind::halt_unwinding(func) {
714-
Ok(r) => {
715-
Latch::set(&(*this).job_completed_latch);
716-
Some(r)
717-
}
714+
let result = match unwind::halt_unwinding(func) {
715+
Ok(r) => Some(r),
718716
Err(err) => {
719717
(*this).job_panicked(err);
720-
Latch::set(&(*this).job_completed_latch);
721718
None
722719
}
723-
}
720+
};
721+
Latch::set(&(*this).job_completed_latch);
722+
result
724723
}
725724

726725
fn job_panicked(&self, err: Box<dyn Any + Send + 'static>) {
@@ -772,6 +771,7 @@ impl ScopeLatch {
772771
}
773772
}
774773

774+
#[inline]
775775
fn increment(&self) {
776776
match self {
777777
ScopeLatch::Stealing { latch, .. } => latch.increment(),
@@ -797,6 +797,7 @@ impl ScopeLatch {
797797
}
798798

799799
impl Latch for ScopeLatch {
800+
#[inline]
800801
unsafe fn set(this: *const Self) {
801802
match &*this {
802803
ScopeLatch::Stealing {

0 commit comments

Comments
 (0)