Skip to content

Commit

Permalink
to_bits again, modernizes code, more caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed Dec 1, 2023
1 parent f0b2c94 commit d4dae40
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 148 deletions.
44 changes: 32 additions & 12 deletions src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,11 @@ impl GasArena {
let next_idx = gas_mixtures.len();
gas_mixtures.push(RwLock::new(Mixture::from_vol(init_volume)));

mix.write_var("_extools_pointer_gasmixture", &(next_idx as f32).into())
.unwrap();
mix.write_var_id(
byond_string!("_extools_pointer_gasmixture"),
&f32::from_bits(next_idx as u32).into(),
)
.unwrap();

let mut ids_lock = NEXT_GAS_IDS.write();
let cur_last = gas_mixtures.len();
Expand Down Expand Up @@ -222,8 +225,11 @@ impl GasArena {
.unwrap()
.write()
.clear_with_vol(init_volume);
mix.write_var("_extools_pointer_gasmixture", &(idx as f32).into())
.unwrap();
mix.write_var_id(
byond_string!("_extools_pointer_gasmixture"),
&f32::from_bits(idx as u32).into(),
)
.unwrap();
}
Ok(ByondValue::null())
}
Expand All @@ -249,7 +255,8 @@ where
F: FnOnce(&Mixture) -> Result<T>,
{
GasArena::with_gas_mixture(
mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize,
mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize,
f,
)
}
Expand All @@ -262,7 +269,8 @@ where
F: FnOnce(&mut Mixture) -> Result<T>,
{
GasArena::with_gas_mixture_mut(
mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize,
mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize,
f,
)
}
Expand All @@ -275,8 +283,12 @@ where
F: FnOnce(&Mixture, &Mixture) -> Result<T>,
{
GasArena::with_gas_mixtures(
src_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize,
arg_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize,
src_mix
.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize,
arg_mix
.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize,
f,
)
}
Expand All @@ -289,8 +301,12 @@ where
F: FnOnce(&mut Mixture, &mut Mixture) -> Result<T>,
{
GasArena::with_gas_mixtures_mut(
src_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize,
arg_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize,
src_mix
.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize,
arg_mix
.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize,
f,
)
}
Expand All @@ -303,8 +319,12 @@ where
F: FnMut(&RwLock<Mixture>, &RwLock<Mixture>) -> Result<T>,
{
GasArena::with_gas_mixtures_custom(
src_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize,
arg_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize,
src_mix
.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize,
arg_mix
.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize,
f,
)
}
Expand Down
25 changes: 9 additions & 16 deletions src/gas/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ pub fn destroy_gas_info_structs() {
GAS_INFO_BY_IDX.write().as_mut().unwrap().clear();
GAS_SPECIFIC_HEATS.write().as_mut().unwrap().clear();
TOTAL_NUM_GASES.store(0, Ordering::Release);
CACHED_GAS_IDS.with(|gas_ids| {
gas_ids.borrow_mut().clear();
CACHED_GAS_IDS.with_borrow_mut(|gas_ids| {
gas_ids.clear();
});
CACHED_IDX_TO_STRINGS.with(|gas_ids| {
gas_ids.borrow_mut().clear();
CACHED_IDX_TO_STRINGS.with_borrow_mut(|gas_ids| {
gas_ids.clear();
});
}

Expand Down Expand Up @@ -274,10 +274,8 @@ fn hook_register_gas(gas: ByondValue) {
.unwrap()
.push(gas_cache.specific_heat);
GAS_INFO_BY_IDX.write().as_mut().unwrap().push(gas_cache);
CACHED_IDX_TO_STRINGS.with(|gas_ids| {
let mut map = gas_ids.borrow_mut();
map.insert(cached_idx, cached_id.into_boxed_str())
});
CACHED_IDX_TO_STRINGS
.with_borrow_mut(|map| map.insert(cached_idx, cached_id.into_boxed_str()));
TOTAL_NUM_GASES.fetch_add(1, Ordering::Release); // this is the only thing that stores it other than shutdown
}
}
Expand Down Expand Up @@ -456,8 +454,7 @@ pub fn gas_idx_from_string(id: &str) -> Result<GasIDX> {
/// # Errors
/// If the given string is not a string or is not a valid gas ID.
pub fn gas_idx_from_value(string_val: &ByondValue) -> Result<GasIDX> {
CACHED_GAS_IDS.with(|c| {
let mut cache = c.borrow_mut();
CACHED_GAS_IDS.with_borrow_mut(|cache| {
if let Some(idx) = cache.get(&string_val.get_strid().unwrap()) {
Ok(*idx)
} else {
Expand All @@ -473,8 +470,7 @@ pub fn gas_idx_from_value(string_val: &ByondValue) -> Result<GasIDX> {
/// # Panics
/// If an invalid gas index is given to this. This should never happen, so we panic instead of runtiming.
pub fn gas_idx_to_id(idx: GasIDX) -> ByondValue {
CACHED_IDX_TO_STRINGS.with(|thin| {
let stuff = thin.borrow();
CACHED_IDX_TO_STRINGS.with_borrow(|stuff| {
ByondValue::new_str(
stuff
.get(&idx)
Expand Down Expand Up @@ -513,10 +509,7 @@ pub fn register_gas_manually(gas_id: &'static str, specific_heat: f32) {
.unwrap()
.push(gas_cache.specific_heat);
GAS_INFO_BY_IDX.write().as_mut().unwrap().push(gas_cache);
CACHED_IDX_TO_STRINGS.with(|gas_ids| {
let mut map = gas_ids.borrow_mut();
map.insert(cached_idx, gas_id.into())
});
CACHED_IDX_TO_STRINGS.with_borrow_mut(|map| map.insert(cached_idx, gas_id.into()));
TOTAL_NUM_GASES.fetch_add(1, Ordering::Release); // this is the only thing that stores it other than shutdown
}

Expand Down
22 changes: 12 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ fn react_hook(src: ByondValue, holder: ByondValue) {
let reactions = with_mix(&src, |mix| Ok(mix.all_reactable()))?;
for reaction in reactions {
ret |= ReactionReturn::from_bits_truncate(
react_by_id(reaction, src.clone(), holder.clone())?
react_by_id(reaction, src, holder)?
.get_number()
.unwrap_or_default() as u32,
);
Expand Down Expand Up @@ -570,28 +570,30 @@ fn equalize_all_hook(gas_list: ByondValue) {
value
.read_number_id(byond_string!("_extools_pointer_gasmixture"))
.ok()
.map(|f| f as usize)
.map(|f| f.to_bits() as usize)
})
.collect::<BTreeSet<_>>();
GasArena::with_all_mixtures(move |all_mixtures| {
let mut tot = gas::Mixture::new();
let mut tot_vol: f64 = 0.0;
for &id in &gas_list {
if let Some(src_gas_lock) = all_mixtures.get(id) {
gas_list
.iter()
.filter_map(|&id| all_mixtures.get(id))
.for_each(|src_gas_lock| {
let src_gas = src_gas_lock.read();
tot.merge(&src_gas);
tot_vol += f64::from(src_gas.volume);
}
}
});
if tot_vol > 0.0 {
for &id in &gas_list {
if let Some(dest_gas_lock) = all_mixtures.get(id) {
gas_list
.iter()
.filter_map(|&id| all_mixtures.get(id))
.for_each(|dest_gas_lock| {
let dest_gas = &mut dest_gas_lock.write();
let vol = dest_gas.volume; // don't wanna borrow it in the below
dest_gas.copy_from_mutable(&tot);
dest_gas.multiply((f64::from(vol) / tot_vol) as f32);
}
}
});
}
});
Ok(ByondValue::null())
Expand Down
15 changes: 6 additions & 9 deletions src/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ thread_local! {

pub fn clean_up_reaction_values() {
crate::turfs::wait_for_tasks();
REACTION_VALUES.with(|reaction_values| {
reaction_values.borrow_mut().clear();
REACTION_VALUES.with_borrow_mut(|reaction_values| {
reaction_values.clear();
});
}

Expand All @@ -53,8 +53,8 @@ pub fn react_by_id(
src: ByondValue,
holder: ByondValue,
) -> Result<ByondValue> {
REACTION_VALUES.with(|r| {
r.borrow().get(&id).map_or_else(
REACTION_VALUES.with_borrow(|r| {
r.get(&id).map_or_else(
|| Err(eyre::eyre!("Reaction with invalid id")),
|reaction| match reaction {
ReactionSide::ByondSide(val) => val
Expand Down Expand Up @@ -134,15 +134,12 @@ impl Reaction {
}
}?;

REACTION_VALUES.with(|r| -> Result<()> {
let mut reaction_map = r.borrow_mut();
REACTION_VALUES.with_borrow_mut(|reaction_map| -> Result<()> {
match func {
Some(function) => {
reaction_map.insert(our_reaction.id, ReactionSide::RustSide(function))
}
None => {
reaction_map.insert(our_reaction.id, ReactionSide::ByondSide(reaction.clone()))
}
None => reaction_map.insert(our_reaction.id, ReactionSide::ByondSide(reaction)),
};
Ok(())
})?;
Expand Down
53 changes: 23 additions & 30 deletions src/reaction/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ fn plasma_fire(byond_air: ByondValue, holder: ByondValue) -> Result<ByondValue>
let mut cached_results = byond_air.read_var_id(byond_string!("reaction_results"))?;
cached_results.write_list_index("fire", fire_amount)?;
if temperature > FIRE_MINIMUM_TEMPERATURE_TO_EXIST {
byondapi::global_call::call_global(
"fire_expose",
byondapi::global_call::call_global_id(
byond_string!("fire_expose"),
&[holder, byond_air, temperature.into()],
)?;
}
Ok(1.0.into())
Ok(true.into())
} else {
Ok(0.0.into())
Ok(false.into())
}
}

Expand Down Expand Up @@ -141,18 +141,18 @@ fn tritium_fire(byond_air: ByondValue, holder: ByondValue) -> Result<ByondValue>
Ok((burned_fuel, energy_released, new_temp))
})?;
if burned_fuel > TRITIUM_MINIMUM_RADIATION_FACTOR {
byondapi::global_call::call_global(
"radiation_burn",
&[holder.clone(), energy_released.into()],
byondapi::global_call::call_global_id(
byond_string!("radiation_burn"),
&[holder, energy_released.into()],
)?;
}
if temperature > FIRE_MINIMUM_TEMPERATURE_TO_EXIST {
byondapi::global_call::call_global(
"fire_expose",
byondapi::global_call::call_global_id(
byond_string!("fire_expose"),
&[holder, byond_air, temperature.into()],
)?;
}
Ok(1.0.into())
Ok(true.into())
}

#[cfg(feature = "fusion_hook")]
Expand Down Expand Up @@ -288,15 +288,15 @@ fn fusion(byond_air: ByondValue, holder: ByondValue) -> Result<ByondValue> {
Ok(standard_energy)
})?;
if reaction_energy != 0.0 {
byondapi::global_call::call_global(
"fusion_ball",
byondapi::global_call::call_global_id(
byond_string!("fusion_ball"),
&[holder, reaction_energy.into(), standard_energy.into()],
)?;
Ok(1.0.into())
Ok(true.into())
} else if reaction_energy == 0.0 && instability <= FUSION_INSTABILITY_ENDOTHERMALITY {
Ok(1.0.into())
Ok(true.into())
} else {
Ok(0.0.into())
Ok(false.into())
}
}

Expand Down Expand Up @@ -396,27 +396,20 @@ fn generic_fire(byond_air: ByondValue, holder: ByondValue) -> Result<ByondValue>
let mut cached_results = byond_air.read_var_id(byond_string!("reaction_results"))?;
cached_results.write_list_index("fire", fire_amount)?;
if temperature > FIRE_MINIMUM_TEMPERATURE_TO_EXIST {
byondapi::global_call::call_global(
"fire_expose",
&[holder.clone(), byond_air, temperature.into()],
byondapi::global_call::call_global_id(
byond_string!("fire_expose"),
&[holder, byond_air, temperature.into()],
)?;
}
if radiation_released > 0.0 {
byondapi::global_call::call_global(
"radiation_burn",
&[holder.clone(), radiation_released.into()],
byondapi::global_call::call_global_id(
byond_string!("radiation_burn"),
&[holder, radiation_released.into()],
)?;
}
Ok({
if fire_amount > 0.0 {
1.0
} else {
0.0
}
}
.into())
Ok((fire_amount > 0.0).into())
} else {
Ok(0.0.into())
Ok(false.into())
}
})
}
10 changes: 6 additions & 4 deletions src/turfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ impl TurfGases {
Ok(())
}

//This isn't a useless collect(), we can't hold a mutable ref and an immutable ref at once on the graph
#[allow(clippy::needless_collect)]
pub fn remove_adjacencies(&mut self, index: NodeIndex) {
let edges = self
.graph
Expand Down Expand Up @@ -443,7 +441,9 @@ fn hook_register_turf(src: ByondValue, flag: ByondValue) {
if flag >= 0 {
let mut to_insert: TurfMixture = TurfMixture::default();
let air = src.read_var_id(byond_string!("air"))?;
to_insert.mix = air.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize;
to_insert.mix = air
.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize;
to_insert.flags = SimulationFlags::from_bits_truncate(flag as u8);
to_insert.id = id;

Expand Down Expand Up @@ -542,7 +542,9 @@ fn update_visuals(src: ByondValue) -> Result<ByondValue> {
let mut overlay_types = Vec::new();
let gas_overlays =
byondapi::global_call::call_global_id(byond_string!("get_overlays"), &[])?;
let ptr = air.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize;
let ptr = air
.read_number_id(byond_string!("_extools_pointer_gasmixture"))?
.to_bits() as usize;
GasArena::with_gas_mixture(ptr, |mix| {
mix.for_each_gas(|idx, moles| {
if let Some(amt) = gas::types::gas_visibility(idx) {
Expand Down
9 changes: 6 additions & 3 deletions src/turfs/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ fn groups_hook(mut src: ByondValue, remaining: ByondValue) {
std::column!()
)
})?;
src.write_var(
"cost_groups",
src.write_var_id(
byond_string!("cost_groups"),
&(0.8 * prev_cost + 0.2 * (bench as f32)).into(),
)?;
src.write_var("num_group_turfs_processed", &(num_eq as f32).into())?;
src.write_var_id(
byond_string!("num_group_turfs_processed"),
&(num_eq as f32).into(),
)?;
Ok(is_cancelled.into())
}

Expand Down
Loading

0 comments on commit d4dae40

Please sign in to comment.