Skip to content

Commit 65574c3

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.) (cherry picked from commit f98eb57)
1 parent b1174a4 commit 65574c3

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
@@ -660,6 +660,7 @@ impl<'scope> ScopeBase<'scope> {
660660
}
661661
}
662662

663+
#[inline]
663664
fn increment(&self) {
664665
self.job_completed_latch.increment();
665666
}
@@ -719,17 +720,15 @@ impl<'scope> ScopeBase<'scope> {
719720
where
720721
FUNC: FnOnce() -> R,
721722
{
722-
match unwind::halt_unwinding(func) {
723-
Ok(r) => {
724-
Latch::set(&(*this).job_completed_latch);
725-
Some(r)
726-
}
723+
let result = match unwind::halt_unwinding(func) {
724+
Ok(r) => Some(r),
727725
Err(err) => {
728726
(*this).job_panicked(err);
729-
Latch::set(&(*this).job_completed_latch);
730727
None
731728
}
732-
}
729+
};
730+
Latch::set(&(*this).job_completed_latch);
731+
result
733732
}
734733

735734
fn job_panicked(&self, err: Box<dyn Any + Send + 'static>) {
@@ -785,6 +784,7 @@ impl ScopeLatch {
785784
}
786785
}
787786

787+
#[inline]
788788
fn increment(&self) {
789789
match self {
790790
ScopeLatch::Stealing { latch, .. } => latch.increment(),
@@ -810,6 +810,7 @@ impl ScopeLatch {
810810
}
811811

812812
impl Latch for ScopeLatch {
813+
#[inline]
813814
unsafe fn set(this: *const Self) {
814815
match &*this {
815816
ScopeLatch::Stealing {

0 commit comments

Comments
 (0)