Skip to content

Commit

Permalink
fix stats, convert some things to iter
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed Dec 1, 2023
1 parent a9cbc71 commit 4ecac45
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
9 changes: 5 additions & 4 deletions crates/auxcallback/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ pub fn byond_callback_sender() -> flume::Sender<DeferredFunc> {
fn process_callbacks() {
//let stack_trace = Proc::find("/proc/auxtools_stack_trace").unwrap();
with_callback_receiver(|receiver| {
for callback in receiver.try_iter() {
if let Err(e) = callback() {
receiver
.try_iter()
.filter_map(|cb| cb().err())
.for_each(|e| {
let error_string = format!("{e:?}").try_into().unwrap();
byondapi::global_call::call_global_id(
byond_string!("stack_trace"),
&[error_string],
)
.unwrap();
}
}
})
})
}

Expand Down
33 changes: 15 additions & 18 deletions src/gas/mixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ impl Mixture {
/// # Errors
/// If the closure errors.
pub fn for_each_gas(&self, mut f: impl FnMut(GasIDX, f32) -> Result<()>) -> Result<()> {
for (i, g) in self.enumerate() {
f(i, g)?;
}
self.enumerate().try_for_each(|(i, g)| f(i, g))?;
Ok(())
}
/// As `for_each_gas`, but with mut refs to the mole counts instead of copies.
Expand All @@ -152,9 +150,10 @@ impl Mixture {
&mut self,
mut f: impl FnMut(GasIDX, &mut f32) -> Result<()>,
) -> Result<()> {
for (i, g) in self.moles.iter_mut().enumerate() {
f(i, g)?;
}
self.moles
.iter_mut()
.enumerate()
.try_for_each(|(i, g)| f(i, g))?;
Ok(())
}
/// Returns (by value) the amount of moles of a given index the mix has. M
Expand Down Expand Up @@ -271,9 +270,10 @@ impl Mixture {
let our_heat_capacity = self.heat_capacity();
let other_heat_capacity = giver.heat_capacity();
self.maybe_expand(giver.moles.len());
for (a, b) in self.moles.iter_mut().zip(giver.moles.iter()) {
*a += b;
}
self.moles
.iter_mut()
.zip(giver.moles.iter())
.for_each(|(a, b)| *a += b);
let combined_heat_capacity = our_heat_capacity + other_heat_capacity;
if combined_heat_capacity > MINIMUM_HEAT_CAPACITY {
self.set_temperature(
Expand All @@ -293,9 +293,10 @@ impl Mixture {
let our_heat_capacity = self.heat_capacity();
let other_heat_capacity = giver.heat_capacity() * ratio;
self.maybe_expand(giver.moles.len());
for (a, b) in self.moles.iter_mut().zip(giver.moles.iter()) {
*a += b * ratio;
}
self.moles
.iter_mut()
.zip(giver.moles.iter())
.for_each(|(a, b)| *a += b * ratio);
let combined_heat_capacity = our_heat_capacity + other_heat_capacity;
if combined_heat_capacity > MINIMUM_HEAT_CAPACITY {
self.set_temperature(
Expand Down Expand Up @@ -465,18 +466,14 @@ impl Mixture {
/// Multiplies every gas molage with this value.
pub fn multiply(&mut self, multiplier: f32) {
if !self.immutable {
for amt in self.moles.iter_mut() {
*amt *= multiplier;
}
self.moles.iter_mut().for_each(|amt| *amt *= multiplier);
self.cached_heat_capacity.invalidate();
self.garbage_collect();
}
}
pub fn add(&mut self, num: f32) {
if !self.immutable {
for amt in self.moles.iter_mut() {
*amt += num;
}
self.moles.iter_mut().for_each(|amt| *amt += num);
self.cached_heat_capacity.invalidate();
self.garbage_collect();
}
Expand Down
24 changes: 11 additions & 13 deletions src/turfs/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn process_turf(
remaining: Duration,
fdm_max_steps: i32,
equalize_enabled: bool,
ssair: ByondValue,
mut ssair: ByondValue,
) -> Result<()> {
//this will block until process_turfs is called
let (low_pressure_turfs, _high_pressure_turfs) = {
Expand All @@ -51,25 +51,23 @@ fn process_turf(
let bench = start_time.elapsed().as_millis();
let (lpt, hpt) = (low_pressure_turfs.len(), high_pressure_turfs.len());
let prev_cost = ssair.read_number_id(byond_string!("cost_turfs"))?;
ssair
.read_var_id(byond_string!("cost_turfs"))?
.set_number(0.8 * prev_cost + 0.2 * (bench as f32));
ssair
.read_var_id(byond_string!("low_pressure_turfs"))?
.set_number(lpt as f32);
ssair
.read_var_id(byond_string!("high_pressure_turfs"))?
.set_number(hpt as f32);
ssair.write_var_id(
byond_string!("cost_turfs"),
&(0.8 * prev_cost + 0.2 * (bench as f32)).into(),
)?;
ssair.write_var_id(byond_string!("low_pressure_turfs"), &(lpt as f32).into())?;
ssair.write_var_id(byond_string!("high_pressure_turfs"), &(hpt as f32).into())?;
(low_pressure_turfs, high_pressure_turfs)
};
{
let start_time = Instant::now();
post_process();
let bench = start_time.elapsed().as_millis();
let prev_cost = ssair.read_number_id(byond_string!("cost_post_process"))?;
ssair
.read_var_id(byond_string!("cost_post_process"))?
.set_number(0.8 * prev_cost + 0.2 * (bench as f32));
ssair.write_var_id(
byond_string!("cost_post_process"),
&(0.8 * prev_cost + 0.2 * (bench as f32)).into(),
)?;
}
{
planet_process();
Expand Down

0 comments on commit 4ecac45

Please sign in to comment.