More optimizations for reagent code (#80529)

## About The Pull Request

1. Removed offsetting reagent volumes by the quantization/rounding
constants before comparing values. Not required since the final total
volume is rounded inside the reagent holder anyway.
2. Removed excessive calls to the hottest proc in reagent code i.e.
`update_total()` per reaction step. Speeds up reactions even more.
3. Removed rounding of volumes (like in liver, pyrotechnics, overheated
reactions) where the volume is already rounded inside the holder.
4. Avoid calling `handle_reactions()` if the holder is already reacting
when adding new reagents.

## Changelog
🆑
code: removed excess calls to `update_total()` making reaction code
slightly faster.
code: removed excessive use of chemical constants (quantisation &
rounding constants) in places where they were not needed i.e. plumbing
buffer, reaction chamber, pyrotechnics & the liver.
/🆑
This commit is contained in:
SyncIt21
2023-12-24 23:58:01 +01:00
committed by GitHub
parent 3fc2ff7b4b
commit fe25dce035
7 changed files with 10 additions and 11 deletions
@@ -33,11 +33,11 @@
SIGNAL_HANDLER
if(!buffer_net)
return
if(reagents.total_volume + CHEMICAL_QUANTISATION_LEVEL >= activation_volume && mode == UNREADY)
if(reagents.total_volume >= activation_volume && mode == UNREADY)
mode = IDLE
buffer_net.check_active()
else if(reagents.total_volume + CHEMICAL_QUANTISATION_LEVEL < activation_volume && mode != UNREADY)
else if(reagents.total_volume < activation_volume && mode != UNREADY)
mode = UNREADY
buffer_net.check_active()
@@ -43,7 +43,7 @@
/obj/machinery/plumbing/reaction_chamber/proc/on_reagent_change(datum/reagents/holder, ...)
SIGNAL_HANDLER
if(holder.total_volume <= CHEMICAL_VOLUME_ROUNDING && emptying) //we were emptying, but now we aren't
if(!holder.total_volume && emptying) //we were emptying, but now we aren't
emptying = FALSE
holder.flags |= NO_REACT
return NONE
@@ -129,7 +129,6 @@
amount = adjusted_vol
has_split = TRUE
update_total()
var/cached_total = total_volume
if(cached_total + amount > maximum_volume)
amount = maximum_volume - cached_total //Doesnt fit in. Make it disappear. shouldn't happen. Will happen.
@@ -195,7 +194,7 @@
set_temperature(reagtemp)
SEND_SIGNAL(src, COMSIG_REAGENTS_NEW_REAGENT, new_reagent, amount, reagtemp, data, no_react)
if(!no_react)
if(!no_react && !is_reacting)
handle_reactions()
return amount
@@ -190,7 +190,6 @@
if(!LAZYLEN(reaction_list))
finish_reacting()
else
update_total()
handle_reactions()
/*
+2 -1
View File
@@ -182,7 +182,8 @@
var/datum/reagent/reagent = holder.has_reagent(id)
if(!reagent)
return
reagent.volume = round((reagent.volume * 0.98), CHEMICAL_QUANTISATION_LEVEL) //Slowly lower yield per tick
reagent.volume *= 0.98 //Slowly lower yield per tick
holder.update_total()
/**
* Occurs when a reation is too impure (i.e. it's below purity_min)
@@ -15,7 +15,7 @@
/datum/chemical_reaction/reagent_explosion/nitroglycerin/on_reaction(datum/reagents/holder, datum/equilibrium/reaction, created_volume)
if(holder.has_reagent(/datum/reagent/exotic_stabilizer,round(created_volume / 25, CHEMICAL_QUANTISATION_LEVEL)))
if(holder.has_reagent(/datum/reagent/exotic_stabilizer, created_volume / 25))
return
holder.remove_reagent(/datum/reagent/nitroglycerin, created_volume * 2)
..()
@@ -78,10 +78,10 @@
strengthdiv = 3
/datum/chemical_reaction/reagent_explosion/tatp/update_info()
required_temp = 450 + rand(-49,49) //this gets loaded only on round start
required_temp = 450 + rand(-49, 49) //this gets loaded only on round start
/datum/chemical_reaction/reagent_explosion/tatp/on_reaction(datum/reagents/holder, datum/equilibrium/reaction, created_volume)
if(holder.has_reagent(/datum/reagent/exotic_stabilizer,round(created_volume / 50, CHEMICAL_QUANTISATION_LEVEL))) // we like exotic stabilizer
if(holder.has_reagent(/datum/reagent/exotic_stabilizer, created_volume / 50)) // we like exotic stabilizer
return
holder.remove_reagent(/datum/reagent/tatp, created_volume)
..()
@@ -142,7 +142,7 @@
for(var/datum/reagent/toxin/toxin in cached_reagents)
if(toxin.affected_organ_flags && !(organ_flags & toxin.affected_organ_flags)) //this particular toxin does not affect this type of organ
continue
var/amount = round(toxin.volume, CHEMICAL_QUANTISATION_LEVEL) // this is an optimization
var/amount = toxin.volume
if(belly)
amount += belly.reagents.get_reagent_amount(toxin.type)