Skip to content

Commit db535ba

Browse files
committed
Generate original vtable and clone's vtable in the same CGU
1 parent 793b45f commit db535ba

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

library/alloc/src/task.rs

+13
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker {
136136
#[inline(always)]
137137
fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker {
138138
// Increment the reference count of the arc to clone it.
139+
//
140+
// The #[inline(always)] is to ensure that raw_waker and clone_waker are
141+
// always generated in the same code generation unit as one another, and
142+
// therefore that the structurally identical const-promoted RawWakerVTable
143+
// within both functions is deduplicated at LLVM IR code generation time.
144+
// This allows optimizing Waker::will_wake to a single pointer comparison of
145+
// the vtable pointers, rather than comparing all four function pointers
146+
// within the vtables.
147+
#[inline(always)]
139148
unsafe fn clone_waker<W: Wake + Send + Sync + 'static>(waker: *const ()) -> RawWaker {
140149
unsafe { Arc::increment_strong_count(waker as *const W) };
141150
RawWaker::new(
@@ -304,6 +313,10 @@ impl<W: LocalWake + 'static> From<Rc<W>> for RawWaker {
304313
#[inline(always)]
305314
fn local_raw_waker<W: LocalWake + 'static>(waker: Rc<W>) -> RawWaker {
306315
// Increment the reference count of the Rc to clone it.
316+
//
317+
// Refer to the comment on raw_waker's clone_waker regarding why this is
318+
// always inline.
319+
#[inline(always)]
307320
unsafe fn clone_waker<W: LocalWake + 'static>(waker: *const ()) -> RawWaker {
308321
unsafe { Rc::increment_strong_count(waker as *const W) };
309322
RawWaker::new(

0 commit comments

Comments
 (0)