[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
+20 -15
View File
@@ -32,6 +32,8 @@
/// The APCs power channel is automatically on.
#define APC_CHANNEL_AUTO_ON 3
#define APC_CHANNEL_IS_ON(channel) (channel >= APC_CHANNEL_ON)
// APC autoset enums:
/// The APC turns automated and manual power channels off.
#define AUTOSET_FORCE_OFF 0
@@ -138,7 +140,7 @@
var/environ = APC_CHANNEL_AUTO_ON
var/operating = TRUE
var/charging = APC_NOT_CHARGING
var/chargemode = 1
var/chargemode = TRUE
var/chargecount = 0
var/locked = TRUE
var/coverlocked = TRUE
@@ -158,6 +160,7 @@
var/beenhit = 0 // used for counting how many times it has been hit, used for Aliens at the moment
var/mob/living/silicon/ai/occupier = null
var/transfer_in_progress = FALSE //Is there an AI being transferred out of us?
///buffer state that makes apcs not shut off channels immediately as long as theres some power left, effect visible in apcs only slowly losing power
var/longtermpower = 10
var/auto_name = FALSE
var/failure_timer = 0
@@ -1281,9 +1284,10 @@
force_update = TRUE
return
lastused_light = area.power_usage[AREA_USAGE_LIGHT] + area.power_usage[AREA_USAGE_STATIC_LIGHT]
lastused_equip = area.power_usage[AREA_USAGE_EQUIP] + area.power_usage[AREA_USAGE_STATIC_EQUIP]
lastused_environ = area.power_usage[AREA_USAGE_ENVIRON] + area.power_usage[AREA_USAGE_STATIC_ENVIRON]
//dont use any power from that channel if we shut that power channel off
lastused_light = APC_CHANNEL_IS_ON(lighting) ? area.power_usage[AREA_USAGE_LIGHT] + area.power_usage[AREA_USAGE_STATIC_LIGHT] : 0
lastused_equip = APC_CHANNEL_IS_ON(equipment) ? area.power_usage[AREA_USAGE_EQUIP] + area.power_usage[AREA_USAGE_STATIC_EQUIP] : 0
lastused_environ = APC_CHANNEL_IS_ON(environ) ? area.power_usage[AREA_USAGE_ENVIRON] + area.power_usage[AREA_USAGE_STATIC_ENVIRON] : 0
area.clear_usage()
lastused_total = lastused_light + lastused_equip + lastused_environ
@@ -1447,17 +1451,18 @@
* - [AUTOSET_OFF]: The APC turns automatic channels off.
*/
/obj/machinery/power/apc/proc/autoset(val, on)
if(on == AUTOSET_FORCE_OFF)
if(val == APC_CHANNEL_ON) // if on, return off
return APC_CHANNEL_OFF
else if(val == APC_CHANNEL_AUTO_ON) // if auto-on, return auto-off
return APC_CHANNEL_AUTO_OFF
else if(on == AUTOSET_ON)
if(val == APC_CHANNEL_AUTO_OFF) // if auto-off, return auto-on
return APC_CHANNEL_AUTO_ON
else if(on == AUTOSET_OFF)
if(val == APC_CHANNEL_AUTO_ON) // if auto-on, return auto-off
return APC_CHANNEL_AUTO_OFF
switch(on)
if(AUTOSET_FORCE_OFF)
if(val == APC_CHANNEL_ON) // if on, return off
return APC_CHANNEL_OFF
else if(val == APC_CHANNEL_AUTO_ON) // if auto-on, return auto-off
return APC_CHANNEL_AUTO_OFF
if(AUTOSET_ON)
if(val == APC_CHANNEL_AUTO_OFF) // if auto-off, return auto-on
return APC_CHANNEL_AUTO_ON
if(AUTOSET_OFF)
if(val == APC_CHANNEL_AUTO_ON) // if auto-on, return auto-off
return APC_CHANNEL_AUTO_OFF
return val
/**
+1 -1
View File
@@ -294,7 +294,7 @@ GLOBAL_LIST_EMPTY(gravity_generators) // We will keep track of this by adding ne
/obj/machinery/gravity_generator/main/proc/set_state(new_state)
charging_state = POWER_IDLE
on = new_state
use_power = on ? ACTIVE_POWER_USE : IDLE_POWER_USE
update_use_power(on ? ACTIVE_POWER_USE : IDLE_POWER_USE)
// Sound the alert if gravity was just enabled or disabled.
var/alert = FALSE
if(SSticker.IsRoundInProgress())
+2 -2
View File
@@ -39,10 +39,10 @@
/obj/machinery/computer/monitor/process()
if(!get_powernet())
use_power = IDLE_POWER_USE
update_use_power(IDLE_POWER_USE)
search()
else
use_power = ACTIVE_POWER_USE
update_use_power(ACTIVE_POWER_USE)
record()
/obj/machinery/computer/monitor/proc/search() //keep in sync with /datum/computer_file/program/power_monitor's version
+5 -12
View File
@@ -79,7 +79,7 @@
// returns true if the area has power on given channel (or doesn't require power).
// defaults to power_channel
/obj/machinery/proc/powered(chan = -1) // defaults to power_channel
/obj/machinery/proc/powered(chan = power_channel)
if(!loc)
return FALSE
if(!use_power)
@@ -88,18 +88,13 @@
var/area/A = get_area(src) // make sure it's in an area
if(!A)
return FALSE // if not, then not powered
if(chan == -1)
chan = power_channel
return A.powered(chan) // return power status of the area
// increment the power usage stats for an area
/obj/machinery/proc/use_power(amount, chan = -1) // defaults to power_channel
/obj/machinery/proc/use_power(amount, chan = power_channel)
var/area/A = get_area(src) // make sure it's in an area
if(!A)
return
if(chan == -1)
chan = power_channel
A.use_power(amount, chan)
A?.use_power(amount, chan)
/**
* An alternative to 'use_power', this proc directly costs the APC in direct charge, as opposed to being calculated periodically.
@@ -154,9 +149,7 @@
/obj/machinery/proc/addStaticPower(value, powerchannel)
var/area/A = get_area(src)
if(!A)
return
A.addStaticPower(value, powerchannel)
A?.addStaticPower(value, powerchannel)
/obj/machinery/proc/removeStaticPower(value, powerchannel)
addStaticPower(-value, powerchannel)
+1 -1
View File
@@ -80,7 +80,7 @@
//see if there's a surplus of power remaining in the powernet and stores unused power in the SMES
netexcess = avail - load
if(netexcess > 100 && nodes?.len) // if there was excess power last cycle
if(netexcess > 100 && length(nodes)) // if there was excess power last cycle
for(var/obj/machinery/power/smes/S in nodes) // find the SMESes in the network
S.restore() // and restore some of the power that was used
+1 -1
View File
@@ -69,7 +69,7 @@
toggle_power()
user.visible_message(span_notice("[user.name] turns the [src.name] [active? "on":"off"]."), \
span_notice("You turn the [src.name] [active? "on":"off"]."))
var/datum/gas_mixture/tank_mix = loaded_tank.return_air()
var/datum/gas_mixture/tank_mix = loaded_tank?.return_air()
var/fuel
if(loaded_tank)
fuel = tank_mix.gases[/datum/gas/plasma]
+4 -4
View File
@@ -67,12 +67,12 @@
/obj/machinery/power/emitter/ctf
name = "Energy Cannon"
active = TRUE
active_power_usage = FALSE
idle_power_usage = FALSE
active_power_usage = 0
idle_power_usage = 0
locked = TRUE
req_access_txt = "100"
welded = TRUE
use_power = FALSE
use_power = NO_POWER_USE
/obj/machinery/power/emitter/Initialize()
. = ..()
@@ -110,7 +110,7 @@
fire_delay = fire_shoot_delay
for(var/obj/item/stock_parts/manipulator/manipulator in component_parts)
power_usage -= 50 * manipulator.rating
active_power_usage = power_usage
update_mode_power_usage(ACTIVE_POWER_USE, power_usage)
/obj/machinery/power/emitter/examine(mob/user)
. = ..()