Skip to content

Commit 1ad1e54

Browse files
committed
Regain some lost performance due to de-bloating.
#[inline] are added to common and trivial functions, and slow paths are separated out from inlined hotpath. Signed-off-by: Gary Guo <gary@garyguo.net>
1 parent 0f7756e commit 1ad1e54

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/sync/mutex.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ unsafe impl Sync for RawMutex {}
2525

2626
impl RawMutex {
2727
/// Creates a new raw mutex.
28+
#[inline]
2829
pub fn new() -> RawMutex {
2930
RawMutex {
3031
state: AtomicUsize::new(0),
@@ -35,6 +36,7 @@ impl RawMutex {
3536
/// Acquires the lock.
3637
///
3738
/// We don't use `async` signature here for performance concern.
39+
#[inline]
3840
pub fn lock(&self) -> RawLockFuture<'_> {
3941
RawLockFuture {
4042
mutex: self,
@@ -44,24 +46,31 @@ impl RawMutex {
4446
}
4547

4648
/// Attempts to acquire the lock.
49+
#[inline]
4750
pub fn try_lock(&self) -> bool {
4851
self.state.fetch_or(LOCK, Ordering::Acquire) & LOCK == 0
4952
}
5053

54+
#[cold]
55+
fn unlock_slow(&self) {
56+
let mut blocked = self.blocked.lock().unwrap();
57+
58+
if let Some((_, opt_waker)) = blocked.iter_mut().next() {
59+
// If there is no waker in this entry, that means it was already woken.
60+
if let Some(w) = opt_waker.take() {
61+
w.wake();
62+
}
63+
}
64+
}
65+
5166
/// Unlock this mutex.
67+
#[inline]
5268
pub fn unlock(&self) {
5369
let state = self.state.fetch_and(!LOCK, Ordering::AcqRel);
5470

5571
// If there are any blocked tasks, wake one of them up.
5672
if state & BLOCKED != 0 {
57-
let mut blocked = self.blocked.lock().unwrap();
58-
59-
if let Some((_, opt_waker)) = blocked.iter_mut().next() {
60-
// If there is no waker in this entry, that means it was already woken.
61-
if let Some(w) = opt_waker.take() {
62-
w.wake();
63-
}
64-
}
73+
self.unlock_slow();
6574
}
6675
}
6776
}
@@ -190,6 +199,7 @@ impl<T> Mutex<T> {
190199
///
191200
/// let mutex = Mutex::new(0);
192201
/// ```
202+
#[inline]
193203
pub fn new(t: T) -> Mutex<T> {
194204
Mutex {
195205
mutex: RawMutex::new(),
@@ -261,6 +271,7 @@ impl<T> Mutex<T> {
261271
/// #
262272
/// # })
263273
/// ```
274+
#[inline]
264275
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
265276
if self.mutex.try_lock() {
266277
Some(MutexGuard(self))
@@ -279,6 +290,7 @@ impl<T> Mutex<T> {
279290
/// let mutex = Mutex::new(10);
280291
/// assert_eq!(mutex.into_inner(), 10);
281292
/// ```
293+
#[inline]
282294
pub fn into_inner(self) -> T {
283295
self.value.into_inner()
284296
}
@@ -301,6 +313,7 @@ impl<T> Mutex<T> {
301313
/// #
302314
/// # })
303315
/// ```
316+
#[inline]
304317
pub fn get_mut(&mut self) -> &mut T {
305318
unsafe { &mut *self.value.get() }
306319
}
@@ -326,12 +339,14 @@ impl<T: fmt::Debug> fmt::Debug for Mutex<T> {
326339
}
327340

328341
impl<T> From<T> for Mutex<T> {
342+
#[inline]
329343
fn from(val: T) -> Mutex<T> {
330344
Mutex::new(val)
331345
}
332346
}
333347

334348
impl<T: Default> Default for Mutex<T> {
349+
#[inline]
335350
fn default() -> Mutex<T> {
336351
Mutex::new(Default::default())
337352
}
@@ -344,6 +359,7 @@ unsafe impl<T: Send> Send for MutexGuard<'_, T> {}
344359
unsafe impl<T: Sync> Sync for MutexGuard<'_, T> {}
345360

346361
impl<T> Drop for MutexGuard<'_, T> {
362+
#[inline]
347363
fn drop(&mut self) {
348364
self.0.mutex.unlock();
349365
}
@@ -364,12 +380,14 @@ impl<T: fmt::Display> fmt::Display for MutexGuard<'_, T> {
364380
impl<T> Deref for MutexGuard<'_, T> {
365381
type Target = T;
366382

383+
#[inline]
367384
fn deref(&self) -> &T {
368385
unsafe { &*self.0.value.get() }
369386
}
370387
}
371388

372389
impl<T> DerefMut for MutexGuard<'_, T> {
390+
#[inline]
373391
fn deref_mut(&mut self) -> &mut T {
374392
unsafe { &mut *self.0.value.get() }
375393
}

0 commit comments

Comments
 (0)