Files
Bubberstation/code/controllers/subsystem/dcs.dm
SkyratBot 1bf20d4622 [MIRROR] Micro-optimize GetIdFromArguments to be 48% faster, gaining 0.48s of init time on local (likely more in prod) [MDB IGNORE] (#16039)
* Micro-optimize GetIdFromArguments to be 48% faster, gaining 0.48s of init time on local (likely more in prod) (#69659)

About The Pull Request

    Avoids stringifying key unless its necessary. This was done redundantly twice, but I locked it to just the isnum path, as REF will always return a string, and the other path passes istext.
    Use sortTim directly instead of sort_list. sort_list is just sortTim but it copies the list, so it's just wasted cost.

I still would like the bespoke element key option, as that's the only way to drastically cut down costs on things like item descriptions and decals, but this is good for the general use case, and makes it marginally less pressing.

I also want to test if we'd be better off inserting into the list in sorted order rather than sorting it all in the end, but I suspect not.

* Micro-optimize GetIdFromArguments to be 48% faster, gaining 0.48s of init time on local (likely more in prod)

Co-authored-by: Mothblocks <35135081+Mothblocks@users.noreply.github.com>
2022-09-04 03:34:06 +01:00

61 lines
1.7 KiB
Plaintext

PROCESSING_SUBSYSTEM_DEF(dcs)
name = "Datum Component System"
flags = SS_NO_INIT
wait = 1 SECONDS
var/list/elements_by_type = list()
/datum/controller/subsystem/processing/dcs/Recover()
comp_lookup = SSdcs.comp_lookup
/datum/controller/subsystem/processing/dcs/proc/GetElement(list/arguments)
var/datum/element/eletype = arguments[1]
var/element_id = eletype
if(!ispath(eletype, /datum/element))
CRASH("Attempted to instantiate [eletype] as a /datum/element")
if(initial(eletype.element_flags) & ELEMENT_BESPOKE)
element_id = GetIdFromArguments(arguments)
. = elements_by_type[element_id]
if(.)
return
. = elements_by_type[element_id] = new eletype
/****
* Generates an id for bespoke elements when given the argument list
* Generating the id here is a bit complex because we need to support named arguments
* Named arguments can appear in any order and we need them to appear after ordered arguments
* We assume that no one will pass in a named argument with a value of null
**/
/datum/controller/subsystem/processing/dcs/proc/GetIdFromArguments(list/arguments)
var/datum/element/eletype = arguments[1]
var/list/fullid = list("[eletype]")
var/list/named_arguments = list()
for(var/i in initial(eletype.id_arg_index) to length(arguments))
var/key = arguments[i]
if(istext(key))
var/value = arguments[key]
if (isnull(value))
fullid += key
else
if (!istext(value) && !isnum(value))
value = REF(value)
named_arguments[key] = value
continue
if (isnum(key))
fullid += "[key]"
else
fullid += REF(key)
if(length(named_arguments))
named_arguments = sortTim(named_arguments, /proc/cmp_text_asc)
fullid += named_arguments
return list2params(fullid)