What the program does: I have made a program that simulates a ball in an area made of triangles and many optimizations because it's for high-performance ball path simulation (technically ball path prediction).
Vec3 and Ball definitions:
#[derive(Clone, Copy, Debug)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Clone, Copy, Debug)]
pub struct Ball {
pub time: f32,
pub location: Vec3,
pub velocity: Vec3,
pub angular_velocity: Vec3,
pub radius: f32,
pub collision_radius: f32,
pub moi: f32,
}
How it works right now: I have a function that takes all of the ball info/game info as well as the desired time to simulate the ball for. It converts the time into the number of game ticks that need to be run to actually simulate the ball for that time. It runs a loop for that number of ticks. For each tick, it calls another function, step(game: &mut Game, dt: f32)
. This runs all of the physics that would advance the game that one tick (CPU only, no graphical output whatsoever or GPU involvement.) I then throw the ball data into a Box
and throw that into a Vec
. This is so, when the Vec
is returned, I basically have the predicted path of the ball, from game tick to game tick.
The function:
pub fn get_ball_prediction_struct_for_time(game: &mut Game, time: &f32) -> BallPrediction {
let num_slices = (time / Ball::SIMULATION_DT).round() as usize;
let mut slices = Vec::with_capacity(num_slices);
for _ in 0..num_slices {
Ball::step(game, Ball::SIMULATION_DT);
slices.push(Box::new(game.ball));
}
BallPrediction {
slices,
num_slices,
}
}
My question: Is putting the ball into a box and putting that into a Vec
the best, most efficient way to do this? Is it possible for me to rather clone the balls into the stack (I believe Box
stores it on the heap?) but then Vec
stores stuff on the heap anyway, so does that even matter? How can I minimize moving all the data around?
I'm also new-er to Rust, so insight into Rust and its conventions is appreciated! I can share as might insight to the program as needed, this is an open-source hobby project of mine.
Ball
at least; preferably a complete runnable program. It's very likely that you should not be trying to “minimize moving”, and theBox
is doing you no good, but more details of your data will help determine that. (And in the end, the only way to be sure about performance is to benchmark, not to read the code.) \$\endgroup\$fast_predict_soccar
, where you can really just swap out the functions (line 362) and put in a value fortime
. \$\endgroup\$fast_predict_custom_soccar
, it's in the repo for anyone to run. \$\endgroup\$