From fe25dce0355d76e4e9aff4399b411a53ed73cf17 Mon Sep 17 00:00:00 2001 From: SyncIt21 <110812394+SyncIt21@users.noreply.github.com> Date: Mon, 25 Dec 2023 04:28:01 +0530 Subject: [PATCH] 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 :cl: 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. /:cl: --- code/modules/plumbing/plumbers/plumbing_buffer.dm | 4 ++-- code/modules/plumbing/plumbers/reaction_chamber.dm | 2 +- code/modules/reagents/chemistry/holder/holder.dm | 3 +-- code/modules/reagents/chemistry/holder/reactions.dm | 1 - code/modules/reagents/chemistry/recipes.dm | 3 ++- code/modules/reagents/chemistry/recipes/pyrotechnics.dm | 6 +++--- code/modules/surgery/organs/internal/liver/_liver.dm | 2 +- 7 files changed, 10 insertions(+), 11 deletions(-) diff --git a/code/modules/plumbing/plumbers/plumbing_buffer.dm b/code/modules/plumbing/plumbers/plumbing_buffer.dm index b2bb21bc24e..7b3ef306d04 100644 --- a/code/modules/plumbing/plumbers/plumbing_buffer.dm +++ b/code/modules/plumbing/plumbers/plumbing_buffer.dm @@ -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() diff --git a/code/modules/plumbing/plumbers/reaction_chamber.dm b/code/modules/plumbing/plumbers/reaction_chamber.dm index 965b428acbd..2b56bfb4ae6 100644 --- a/code/modules/plumbing/plumbers/reaction_chamber.dm +++ b/code/modules/plumbing/plumbers/reaction_chamber.dm @@ -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 diff --git a/code/modules/reagents/chemistry/holder/holder.dm b/code/modules/reagents/chemistry/holder/holder.dm index 112c9fca8a1..867245e9c25 100644 --- a/code/modules/reagents/chemistry/holder/holder.dm +++ b/code/modules/reagents/chemistry/holder/holder.dm @@ -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 diff --git a/code/modules/reagents/chemistry/holder/reactions.dm b/code/modules/reagents/chemistry/holder/reactions.dm index 30148802e08..d7f959abbb1 100644 --- a/code/modules/reagents/chemistry/holder/reactions.dm +++ b/code/modules/reagents/chemistry/holder/reactions.dm @@ -190,7 +190,6 @@ if(!LAZYLEN(reaction_list)) finish_reacting() else - update_total() handle_reactions() /* diff --git a/code/modules/reagents/chemistry/recipes.dm b/code/modules/reagents/chemistry/recipes.dm index 579dcb52370..26fb0f472dd 100644 --- a/code/modules/reagents/chemistry/recipes.dm +++ b/code/modules/reagents/chemistry/recipes.dm @@ -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) diff --git a/code/modules/reagents/chemistry/recipes/pyrotechnics.dm b/code/modules/reagents/chemistry/recipes/pyrotechnics.dm index f1fb70faf38..78942431bb8 100644 --- a/code/modules/reagents/chemistry/recipes/pyrotechnics.dm +++ b/code/modules/reagents/chemistry/recipes/pyrotechnics.dm @@ -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) ..() diff --git a/code/modules/surgery/organs/internal/liver/_liver.dm b/code/modules/surgery/organs/internal/liver/_liver.dm index 92e2cb0039f..33dcaa83fd1 100755 --- a/code/modules/surgery/organs/internal/liver/_liver.dm +++ b/code/modules/surgery/organs/internal/liver/_liver.dm @@ -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)