From a774329534e52ca9a003f75ce083b8871947d590 Mon Sep 17 00:00:00 2001 From: Alex Saveau Date: Tue, 25 Jan 2022 13:04:34 -0800 Subject: [PATCH 01/14] Slight perf improvements and tidy for bevymark Signed-off-by: Alex Saveau --- examples/tools/bevymark.rs | 123 +++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 54 deletions(-) diff --git a/examples/tools/bevymark.rs b/examples/tools/bevymark.rs index 290fdf42594aa..60985b0f5d5eb 100644 --- a/examples/tools/bevymark.rs +++ b/examples/tools/bevymark.rs @@ -1,9 +1,12 @@ +use std::fmt::Write; + +use rand::{thread_rng, Rng}; + use bevy::{ core::FixedTimestep, diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, }; -use rand::random; const BIRDS_PER_SECOND: u32 = 10000; const _BASE_COLOR: Color = Color::rgb(5.0, 5.0, 5.0); @@ -72,67 +75,74 @@ fn scheduled_spawner( scheduled.per_wave, bird_texture.0.clone_weak(), ); - counter.color = Color::rgb_linear(random(), random(), random()); + + let mut random = thread_rng(); + counter.color = Color::rgb_linear(random.gen(), random.gen(), random.gen()); scheduled.wave -= 1; } } struct BirdTexture(Handle); +#[derive(Component)] +struct StatsText; + fn setup(mut commands: Commands, asset_server: Res) { let texture = asset_server.load("branding/icon.png"); commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(UiCameraBundle::default()); - commands.spawn_bundle(TextBundle { - text: Text { - sections: vec![ - TextSection { - value: "Bird Count: ".to_string(), - style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 40.0, - color: Color::rgb(0.0, 1.0, 0.0), + commands + .spawn_bundle(TextBundle { + text: Text { + sections: vec![ + TextSection { + value: "Bird Count: ".to_string(), + style: TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 40.0, + color: Color::rgb(0.0, 1.0, 0.0), + }, }, - }, - TextSection { - value: "".to_string(), - style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 40.0, - color: Color::rgb(0.0, 1.0, 1.0), + TextSection { + value: "".to_string(), + style: TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 40.0, + color: Color::rgb(0.0, 1.0, 1.0), + }, }, - }, - TextSection { - value: "\nAverage FPS: ".to_string(), - style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 40.0, - color: Color::rgb(0.0, 1.0, 0.0), + TextSection { + value: "\nAverage FPS: ".to_string(), + style: TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 40.0, + color: Color::rgb(0.0, 1.0, 0.0), + }, }, - }, - TextSection { - value: "".to_string(), - style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 40.0, - color: Color::rgb(0.0, 1.0, 1.0), + TextSection { + value: "".to_string(), + style: TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 40.0, + color: Color::rgb(0.0, 1.0, 1.0), + }, }, + ], + ..Default::default() + }, + style: Style { + position_type: PositionType::Absolute, + position: Rect { + top: Val::Px(5.0), + left: Val::Px(5.0), + ..Default::default() }, - ], - ..Default::default() - }, - style: Style { - position_type: PositionType::Absolute, - position: Rect { - top: Val::Px(5.0), - left: Val::Px(5.0), ..Default::default() }, ..Default::default() - }, - ..Default::default() - }); + }) + .insert(StatsText); commands.insert_resource(BirdTexture(texture)); commands.insert_resource(BirdScheduled { @@ -156,7 +166,8 @@ fn mouse_handler( mut counter: ResMut, ) { if mouse_button_input.just_released(MouseButton::Left) { - counter.color = Color::rgb_linear(random(), random(), random()); + let mut random = thread_rng(); + counter.color = Color::rgb_linear(random.gen(), random.gen(), random.gen()); } if mouse_button_input.pressed(MouseButton::Left) { @@ -181,6 +192,8 @@ fn spawn_birds( let window = windows.get_primary().unwrap(); let bird_x = (window.width() as f32 / -2.) + HALF_BIRD_SIZE; let bird_y = (window.height() as f32 / 2.) - HALF_BIRD_SIZE; + let mut random = thread_rng(); + for count in 0..spawn_count { let bird_z = (counter.count + count) as f32 * 0.00001; commands @@ -199,7 +212,7 @@ fn spawn_birds( }) .insert(Bird { velocity: Vec3::new( - rand::random::() * MAX_VELOCITY - (MAX_VELOCITY * 0.5), + random.gen::() * MAX_VELOCITY - (MAX_VELOCITY * 0.5), 0., 0., ), @@ -209,11 +222,11 @@ fn spawn_birds( } fn movement_system(time: Res