[MIRROR] large refactor of machine/power code to cut down on processing time and wasted lists (#7920)

* large refactor of machine/power code to cut down on processing time and wasted lists (#60317)

original pr here: #59789 (Closed because he didn't think it was good enough)
came back to this because i realized that

    all machines were area sensitive, meaning they had a list with at least a reference to themselves (assuming they arent in the contents of another movable which most arent) for the purposes of handling power differences when their area changes
    pipes are machines
    there are ~14k machines and ~6k pipes
    i made this problem worse with a recent pr by making it a nested list

so i needed to track what machines needed power, and this pr had work already done that could be used for that purpose. now machines that have use_power == NO_POWER_USE do not have this extra memory overhead for no reason

currently every machine that uses power draws that amount from its area from a dynamic channel via auto_use_power() which is called every SSmachines fire(), then in apc/process() the area's dynamic power draw is reset and the power is used. with static power its not calculated then reset every loop, its just taken from the grid. so now machines handle updating their static power usage from their current area (this doesnt touch power machines that require a wire connection). in order to allow this, use_power, idle_power_usage, and active_power_usage have setters to track state correctly and update the static power usage on the machines current area and handle area sensitivity.

also goes through a lot of heavy abusers of SSmachine processing time and tries to make it faster. makes airalarm/process() into a signal handler for COMSIG_TURF_EXPOSE since air alarms only need to process for changes.
Why It's Good For The Game

SSmachines isnt the heaviest hitter in terms of total cpu and certainly not in terms of overtime, but its not a lightweight. it frequently takes > 50ms to complete a run and seems to be in the top 5 or so of subsystem costs looking at some round profilers

also gets rid of a few thousand lists since every pipe no longer has two useless lists each (and any other machines that dont use power)

Love ya kyler

Co-authored-by: Rohesie <rohesie@ gmail.com>

* large refactor of machine/power code to cut down on processing time and wasted lists

Co-authored-by: Kylerace <kylerlumpkin1@gmail.com>
Co-authored-by: Rohesie <rohesie@ gmail.com>
This commit is contained in:
SkyratBot
2021-09-02 03:11:59 +01:00
committed by GitHub
co-authored by Rohesie Kylerace
parent d8e8493e85
commit a2aaacdead
37 changed files with 547 additions and 357 deletions
+13 -5
View File
@@ -7,7 +7,8 @@
pixel_z = 8
obj_flags = CAN_BE_HIT | UNIQUE_RENAME
circuit = /obj/item/circuitboard/machine/hydroponics
idle_power_usage = 0
idle_power_usage = 5000
use_power = NO_POWER_USE
///The amount of water in the tray (max 100)
var/waterlevel = 100
///The maximum amount of water in the tray
@@ -125,6 +126,11 @@
else
return ..()
/obj/machinery/hydroponics/power_change()
. = ..()
if(machine_stat & NOPOWER && self_sustaining)
self_sustaining = FALSE
/obj/machinery/hydroponics/process(delta_time)
var/needs_update = 0 // Checks if the icon needs updating so we don't redraw empty trays every time
@@ -132,8 +138,8 @@
myseed.forceMove(src)
if(!powered() && self_sustaining)
visible_message(span_warning("[name]'s auto-grow functionality shuts off!"))
idle_power_usage = 0
visible_message("<span class='warning'>[name]'s auto-grow functionality shuts off!</span>")
update_use_power(NO_POWER_USE)
self_sustaining = FALSE
update_appearance()
@@ -767,12 +773,14 @@
return
if(!powered())
to_chat(user, span_warning("[name] has no power."))
update_use_power(NO_POWER_USE)
return
if(!anchored)
return
self_sustaining = !self_sustaining
idle_power_usage = self_sustaining ? 5000 : 0
update_use_power(self_sustaining ? IDLE_POWER_USE : NO_POWER_USE)
to_chat(user, "<span class='notice'>You [self_sustaining ? "activate" : "deactivated"] [src]'s autogrow function[self_sustaining ? ", maintaining the tray's health while using high amounts of power" : ""].")
update_appearance()
/obj/machinery/hydroponics/AltClick(mob/user)
@@ -809,7 +817,7 @@
desc = initial(desc)
TRAY_NAME_UPDATE
if(self_sustaining) //No reason to pay for an empty tray.
idle_power_usage = 0
update_use_power(NO_POWER_USE)
self_sustaining = FALSE
update_appearance()