Leafwing-Studios / Emergence

An organic factory builder about adapting to a changing world.
https://leafwing-studios.github.io/Emergence/
Apache License 2.0
265 stars 37 forks source link

Reduce allocations in `horizontal_water_movement` system #927

Open alice-i-cecile opened 1 year ago

alice-i-cecile commented 1 year ago

There are a large number of unnecessary allocations in the system, primarily in the addItion_map, removal_map and flow_map hashmaps and the hashmap returned by proposed lateral flow to neighbors.

Instead, we should add:

/// Cache of water to be added and removed from each tile during [`horizontal_water_movement`].
///
/// This is for performance reasons, as it allows us to reduce allocations by reusing the same space.
#[derive(Component, Default, Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
pub struct LateralWaterCache {
    /// The amount of water to be added to each tile.
    incoming: Volume,
    /// The amount of water to be added to each neighbor.
    outgoing: [Volume; 6],
}

to the WaterBundle.

Then, this should be mutated in place.

alice-i-cecile commented 1 year ago

This should probably wait on #905, as a very similar system is needed there for signal propagation.