Skip to content

Commit 27a2538

Browse files
committed
automata: add some #[inline] annotations
This hopefully ensures these functions can be inlined across crate boundaries. (Although I think they likely already can be due to generics?)
1 parent 061ee81 commit 27a2538

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

regex-automata/src/util/pool.rs

+24
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ impl<T: Send, F: Fn() -> T> Pool<T, F> {
177177
/// the value to go back into the pool) and then calling get again is
178178
/// *not* guaranteed to return the same value received in the first `get`
179179
/// call.
180+
#[inline]
180181
pub fn get(&self) -> PoolGuard<'_, T, F> {
181182
PoolGuard(self.0.get())
182183
}
@@ -200,6 +201,7 @@ impl<'a, T: Send, F: Fn() -> T> PoolGuard<'a, T, F> {
200201
/// This circumvents the guard's `Drop` implementation. This can be useful
201202
/// in circumstances where the automatic `Drop` results in poorer codegen,
202203
/// such as calling non-inlined functions.
204+
#[inline]
203205
pub fn put(this: PoolGuard<'_, T, F>) {
204206
inner::PoolGuard::put(this.0);
205207
}
@@ -208,12 +210,14 @@ impl<'a, T: Send, F: Fn() -> T> PoolGuard<'a, T, F> {
208210
impl<'a, T: Send, F: Fn() -> T> core::ops::Deref for PoolGuard<'a, T, F> {
209211
type Target = T;
210212

213+
#[inline]
211214
fn deref(&self) -> &T {
212215
self.0.value()
213216
}
214217
}
215218

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

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

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

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

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

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

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

731743
impl<'a, T: Send, F: Fn() -> T> Drop for PoolGuard<'a, T, F> {
744+
#[inline]
732745
fn drop(&mut self) {
733746
self.put_imp();
734747
}
@@ -806,6 +819,7 @@ mod inner {
806819
impl<T: Send, F: Fn() -> T> Pool<T, F> {
807820
/// Get a value from the pool. This may block if another thread is also
808821
/// attempting to retrieve a value from the pool.
822+
#[inline]
809823
pub(super) fn get(&self) -> PoolGuard<'_, T, F> {
810824
let mut stack = self.stack.lock();
811825
let value = match stack.pop() {
@@ -815,6 +829,7 @@ mod inner {
815829
PoolGuard { pool: self, value: Some(value) }
816830
}
817831

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

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

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

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

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

987+
#[inline]
966988
fn deref(&self) -> &T {
967989
self.data
968990
}
969991
}
970992

971993
impl<'a, T> core::ops::DerefMut for MutexGuard<'a, T> {
994+
#[inline]
972995
fn deref_mut(&mut self) -> &mut T {
973996
self.data
974997
}
975998
}
976999

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

0 commit comments

Comments
 (0)