From 41d93353efd777ebe66b3f7feb6958462f7e77c4 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 13 Apr 2021 19:50:08 +0200 Subject: [PATCH] Outline drop glue for App to ensure that a lot of drop glue is codegened inside bevy_app --- crates/bevy_app/src/app.rs | 27 ++++++++++++++++++++++----- crates/bevy_app/src/app_builder.rs | 4 ++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index c7b850cff795d7..e57bcfad364b39 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -1,3 +1,5 @@ +use std::mem::ManuallyDrop; + use crate::app_builder::AppBuilder; use bevy_ecs::{ schedule::{Schedule, Stage}, @@ -30,9 +32,9 @@ use bevy_utils::tracing::info_span; /// } /// ``` pub struct App { - pub world: World, - pub runner: Box, - pub schedule: Schedule, + pub world: ManuallyDrop, + pub runner: ManuallyDrop>, + pub schedule: ManuallyDrop, } impl Default for App { @@ -40,11 +42,26 @@ impl Default for App { Self { world: Default::default(), schedule: Default::default(), - runner: Box::new(run_once), + runner: ManuallyDrop::new(Box::new(run_once)), } } } +impl Drop for App { + fn drop(&mut self) { + unsafe { + drop_app(self); + } + } +} + +// Outlined to ensure that a lot of drop glue is codegened inside bevy_app instead of the game. +unsafe fn drop_app(app: &mut App) { + ManuallyDrop::drop(&mut app.world); + ManuallyDrop::drop(&mut app.schedule); + ManuallyDrop::drop(&mut app.runner); +} + fn run_once(mut app: App) { app.update(); } @@ -64,7 +81,7 @@ impl App { #[cfg(feature = "trace")] let _bevy_app_run_guard = bevy_app_run_span.enter(); - let runner = std::mem::replace(&mut self.runner, Box::new(run_once)); + let runner = std::mem::replace(&mut *self.runner, Box::new(run_once)); (runner)(self); } } diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index 36bb74953fe827..27dec20542214a 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -58,7 +58,7 @@ impl AppBuilder { } pub fn set_world(&mut self, world: World) -> &mut Self { - self.app.world = world; + *self.app.world = world; self } @@ -276,7 +276,7 @@ impl AppBuilder { } pub fn set_runner(&mut self, run_fn: impl Fn(App) + 'static) -> &mut Self { - self.app.runner = Box::new(run_fn); + *self.app.runner = Box::new(run_fn); self }