Files
Bubberstation/code/__HELPERS/string_assoc_nested_lists.dm
Ghom 6f862d107f Adds a unit test to stop elements from using identical lists for their arguments. (#76322)
## About The Pull Request
Ok, so a few days ago I made an issue report about multiple instances of
identical elements being generated because of uncached lists.
ninjanomnom (the mind being the element datums) cleared it up and said
an implementation of GetIdFromArguments() that also checks the list
contents wouldn't be worth the performance cost, while adding that a
unit test should be written to check that it doesn't happen at least
during init, which should catch a good chunk of cases.

Also, i'm stopping RemoveElement() from initializing new elements
whenever a cached element is not found. Ideally, there should be a focus
only unit test for that too, but that's something we should tackle on a
different PR.

Some of the code comments may be a tad inaccurate, as much as I'd like
to blame drowsiness for it. Regardless, the unit test takes less than
0.2 seconds to complete on my potato so it's fairly lite.

## Why It's Good For The Game
This will close #76279.

## Changelog
No player-facing change to be logged.
2023-07-27 16:04:16 -07:00

19 lines
556 B
Plaintext

GLOBAL_LIST_EMPTY(string_assoc_nested_lists)
/**
* Caches associative nested lists with non-numeric stringify-able index keys and stringify-able values (text/typepath -> text/path/number).
*/
/datum/proc/string_assoc_nested_list(list/list)
var/list/string_id = list()
for(var/key in list)
var/assoc = list[key]
string_id += "[key]_[islist(assoc) ? "ASSLIST([string_assoc_nested_list(assoc)])" : assoc]"
string_id = string_id.Join("-")
. = GLOB.string_assoc_lists[string_id]
if(.)
return .
return GLOB.string_assoc_lists[string_id] = list