Skip to content

Commit

Permalink
automata: add some #[inline] annotations
Browse files Browse the repository at this point in the history
This hopefully ensures these functions can be inlined across crate
boundaries. (Although I think they likely already can be due to
generics?)
  • Loading branch information
BurntSushi committed Sep 28, 2023
1 parent 061ee81 commit 27a2538
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions regex-automata/src/util/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl<T: Send, F: Fn() -> T> Pool<T, F> {
/// the value to go back into the pool) and then calling get again is
/// *not* guaranteed to return the same value received in the first `get`
/// call.
#[inline]
pub fn get(&self) -> PoolGuard<'_, T, F> {
PoolGuard(self.0.get())
}
Expand All @@ -200,6 +201,7 @@ impl<'a, T: Send, F: Fn() -> T> PoolGuard<'a, T, F> {
/// This circumvents the guard's `Drop` implementation. This can be useful
/// in circumstances where the automatic `Drop` results in poorer codegen,
/// such as calling non-inlined functions.
#[inline]
pub fn put(this: PoolGuard<'_, T, F>) {
inner::PoolGuard::put(this.0);
}
Expand All @@ -208,12 +210,14 @@ impl<'a, T: Send, F: Fn() -> T> PoolGuard<'a, T, F> {
impl<'a, T: Send, F: Fn() -> T> core::ops::Deref for PoolGuard<'a, T, F> {
type Target = T;

#[inline]
fn deref(&self) -> &T {
self.0.value()
}
}

impl<'a, T: Send, F: Fn() -> T> core::ops::DerefMut for PoolGuard<'a, T, F> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.0.value_mut()
}
Expand Down Expand Up @@ -469,6 +473,7 @@ mod inner {
impl<T: Send, F: Fn() -> T> Pool<T, F> {
/// Get a value from the pool. This may block if another thread is also
/// attempting to retrieve a value from the pool.
#[inline]
pub(super) fn get(&self) -> PoolGuard<'_, T, F> {
// Our fast path checks if the caller is the thread that "owns"
// this pool. Or stated differently, whether it is the first thread
Expand Down Expand Up @@ -562,6 +567,7 @@ mod inner {
/// Puts a value back into the pool. Callers don't need to call this.
/// Once the guard that's returned by 'get' is dropped, it is put back
/// into the pool automatically.
#[inline]
fn put_value(&self, value: Box<T>) {
let caller = THREAD_ID.with(|id| *id);
let stack_id = caller % self.stacks.len();
Expand All @@ -587,18 +593,21 @@ mod inner {
}

/// Create a guard that represents the special owned T.
#[inline]
fn guard_owned(&self, caller: usize) -> PoolGuard<'_, T, F> {
PoolGuard { pool: self, value: Err(caller), discard: false }
}

/// Create a guard that contains a value from the pool's stack.
#[inline]
fn guard_stack(&self, value: Box<T>) -> PoolGuard<'_, T, F> {
PoolGuard { pool: self, value: Ok(value), discard: false }
}

/// Create a guard that contains a value from the pool's stack with an
/// instruction to throw away the value instead of putting it back
/// into the pool.
#[inline]
fn guard_stack_transient(&self, value: Box<T>) -> PoolGuard<'_, T, F> {
PoolGuard { pool: self, value: Ok(value), discard: true }
}
Expand Down Expand Up @@ -633,6 +642,7 @@ mod inner {

impl<'a, T: Send, F: Fn() -> T> PoolGuard<'a, T, F> {
/// Return the underlying value.
#[inline]
pub(super) fn value(&self) -> &T {
match self.value {
Ok(ref v) => &**v,
Expand All @@ -657,6 +667,7 @@ mod inner {
}

/// Return the underlying value as a mutable borrow.
#[inline]
pub(super) fn value_mut(&mut self) -> &mut T {
match self.value {
Ok(ref mut v) => &mut **v,
Expand All @@ -681,6 +692,7 @@ mod inner {
}

/// Consumes this guard and puts it back into the pool.
#[inline]
pub(super) fn put(this: PoolGuard<'_, T, F>) {
// Since this is effectively consuming the guard and putting the
// value back into the pool, there's no reason to run its Drop
Expand Down Expand Up @@ -729,6 +741,7 @@ mod inner {
}

impl<'a, T: Send, F: Fn() -> T> Drop for PoolGuard<'a, T, F> {
#[inline]
fn drop(&mut self) {
self.put_imp();
}
Expand Down Expand Up @@ -806,6 +819,7 @@ mod inner {
impl<T: Send, F: Fn() -> T> Pool<T, F> {
/// Get a value from the pool. This may block if another thread is also
/// attempting to retrieve a value from the pool.
#[inline]
pub(super) fn get(&self) -> PoolGuard<'_, T, F> {
let mut stack = self.stack.lock();
let value = match stack.pop() {
Expand All @@ -815,6 +829,7 @@ mod inner {
PoolGuard { pool: self, value: Some(value) }
}

#[inline]
fn put(&self, guard: PoolGuard<'_, T, F>) {
let mut guard = core::mem::ManuallyDrop::new(guard);
if let Some(value) = guard.value.take() {
Expand All @@ -825,6 +840,7 @@ mod inner {
/// Puts a value back into the pool. Callers don't need to call this.
/// Once the guard that's returned by 'get' is dropped, it is put back
/// into the pool automatically.
#[inline]
fn put_value(&self, value: Box<T>) {
let mut stack = self.stack.lock();
stack.push(value);
Expand All @@ -847,16 +863,19 @@ mod inner {

impl<'a, T: Send, F: Fn() -> T> PoolGuard<'a, T, F> {
/// Return the underlying value.
#[inline]
pub(super) fn value(&self) -> &T {
self.value.as_deref().unwrap()
}

/// Return the underlying value as a mutable borrow.
#[inline]
pub(super) fn value_mut(&mut self) -> &mut T {
self.value.as_deref_mut().unwrap()
}

/// Consumes this guard and puts it back into the pool.
#[inline]
pub(super) fn put(this: PoolGuard<'_, T, F>) {
// Since this is effectively consuming the guard and putting the
// value back into the pool, there's no reason to run its Drop
Expand All @@ -878,6 +897,7 @@ mod inner {
}

impl<'a, T: Send, F: Fn() -> T> Drop for PoolGuard<'a, T, F> {
#[inline]
fn drop(&mut self) {
self.put_imp();
}
Expand Down Expand Up @@ -931,6 +951,7 @@ mod inner {
/// Lock this mutex and return a guard providing exclusive access to
/// `T`. This blocks if some other thread has already locked this
/// mutex.
#[inline]
fn lock(&self) -> MutexGuard<'_, T> {
while self
.locked
Expand Down Expand Up @@ -963,18 +984,21 @@ mod inner {
impl<'a, T> core::ops::Deref for MutexGuard<'a, T> {
type Target = T;

#[inline]
fn deref(&self) -> &T {
self.data
}
}

impl<'a, T> core::ops::DerefMut for MutexGuard<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.data
}
}

impl<'a, T> Drop for MutexGuard<'a, T> {
#[inline]
fn drop(&mut self) {
// Drop means 'data' is no longer accessible, so we can unlock
// the mutex.
Expand Down

0 comments on commit 27a2538

Please sign in to comment.