Skip to content

Commit

Permalink
Outline drop glue for App to ensure that a lot of drop glue is codege…
Browse files Browse the repository at this point in the history
…ned inside bevy_app
  • Loading branch information
bjorn3 committed Apr 13, 2021
1 parent e5ba919 commit 41d9335
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
27 changes: 22 additions & 5 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem::ManuallyDrop;

use crate::app_builder::AppBuilder;
use bevy_ecs::{
schedule::{Schedule, Stage},
Expand Down Expand Up @@ -30,21 +32,36 @@ use bevy_utils::tracing::info_span;
/// }
/// ```
pub struct App {
pub world: World,
pub runner: Box<dyn Fn(App)>,
pub schedule: Schedule,
pub world: ManuallyDrop<World>,
pub runner: ManuallyDrop<Box<dyn Fn(App)>>,
pub schedule: ManuallyDrop<Schedule>,
}

impl Default for App {
fn default() -> Self {
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();
}
Expand All @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 41d9335

Please sign in to comment.