Merge pull request #1896 from haileys/inline-never-for-rvo

Write to TaskStorage::future via never-inlined fn to encourage RVO
This commit is contained in:
Dario Nieuwenhuis 2023-09-15 18:45:21 +00:00 committed by GitHub
commit 5a158b94bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 5 deletions

View File

@ -73,9 +73,10 @@ mod thread {
pub fn start(&'static mut self, init: impl FnOnce(Spawner)) { pub fn start(&'static mut self, init: impl FnOnce(Spawner)) {
unsafe { unsafe {
let executor = &self.inner; let executor = &self.inner;
self.ctx.closure.write(Closure::new(move |_| { let future = Closure::new(move |_| {
executor.poll(); executor.poll();
})); });
self.ctx.closure.write_in_place(|| future);
init(self.inner.spawner()); init(self.inner.spawner());
} }
} }

View File

@ -203,7 +203,7 @@ impl<F: Future + 'static> AvailableTask<F> {
fn initialize_impl<S>(self, future: impl FnOnce() -> F) -> SpawnToken<S> { fn initialize_impl<S>(self, future: impl FnOnce() -> F) -> SpawnToken<S> {
unsafe { unsafe {
self.task.raw.poll_fn.set(Some(TaskStorage::<F>::poll)); self.task.raw.poll_fn.set(Some(TaskStorage::<F>::poll));
self.task.future.write(future()); self.task.future.write_in_place(future);
let task = TaskRef::new(self.task); let task = TaskRef::new(self.task);

View File

@ -17,8 +17,9 @@ impl<T> UninitCell<T> {
&mut *self.as_mut_ptr() &mut *self.as_mut_ptr()
} }
pub unsafe fn write(&self, val: T) { #[inline(never)]
ptr::write(self.as_mut_ptr(), val) pub unsafe fn write_in_place(&self, func: impl FnOnce() -> T) {
ptr::write(self.as_mut_ptr(), func())
} }
pub unsafe fn drop_in_place(&self) { pub unsafe fn drop_in_place(&self) {