Skip to content

Commit

Permalink
[MIRROR] Refactors fancy type generation (#844) (#1863)
Browse files Browse the repository at this point in the history
* Refactors fancy type generation (#81259)

## About The Pull Request

[Refactors fancy type
generation](tgstation/tgstation@3f218ac)

Ok so we have this proc that generates concatenated names for types so
admins have a nice list to sort through.

The trouble is this is done by, for each type, iterating all possible
replacements, and seeing which ones apply (with expensive string
operations)

A clean run of this applied to all datums takes about 3.5 seconds on my
pc.
This sucks.

Ok so can we do better. Well, yes, pretty easily.

Rather then, for each potential type, iterating all the options, let's
build a zebra typecache (a lookup list of type -> string to use), and
use that.
Then we can use a list of replacement -> the bit to tear out to figure
out what to remove.

This works quite well. It does mean that we're doing it based off the
type tree and not type paths, so if we didn't have a replacement for
like, mob, it'd look weird, but we don't have cases like that so it's
fine.

Or well we sorta did, didn't have anything for atom movables or areas,
but I fixed that so sall good.

Anyway, we only need to do this work once. It takes about 0.3 seconds on
my machine, so we can cache it.

Just this on its own would technically slow init, since we have a some
code that's running this proc off static, but we can just not, that's
fine (technically saves init time too since we don't have to burn 0.1
seconds on it anymore).

This brings the cost of generating this list for all datums from 3
seconds to 0.16, assuming we have the static pre generated.

We could in theory pre-generate just like, all the strings? 
But I don't think the cached cost is high enough for that to be a real
problem. IDK open to other thoughts

Oh also I had to reorder the strings in that list, cause
zebra_typecacheof has reverse priority. s life

[Updates stat tracking macro to work at world
start](tgstation/tgstation@1fbfb70)

It for some reason doesn't actually get anything this early, but now at
least the logging would in theory function

## Why It's Good For The Game

Better response times for admins, faster code, more better

* Refactors fancy type generation

---------

Co-authored-by: NovaBot <[email protected]>
Co-authored-by: LemonInTheDark <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2024
1 parent 345ddca commit 4bba66e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 66 deletions.
3 changes: 2 additions & 1 deletion code/__DEFINES/stat_tracking.dm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
#define EXPORT_STATS_TO_FILE_LATER(filename, costs, counts, proc) \
do { \
var/static/last_export = 0; \
if (world.time - last_export > 1.1 SECONDS) { \
/* Need to always run if we haven't yet, since this code can be placed ANYWHERE */ \
if (world.time - last_export > 1.1 SECONDS || (last_export == 0)) { \
last_export = world.time; \
/* spawn() is used here because this is often used to track init times, where timers act oddly. */ \
/* I was making timers and even after init times were complete, the timers didn't run :shrug: */ \
Expand Down
142 changes: 79 additions & 63 deletions code/__HELPERS/type_processing.dm
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
/proc/make_types_fancy(list/types)
if (ispath(types))
types = list(types)
. = list()
for(var/type in types)
var/typename = "[type]"
// Longest paths comes first
var/static/list/TYPES_SHORTCUTS = list(
/obj/effect/decal/cleanable = "CLEANABLE",
var/static/list/types_to_replacement
var/static/list/replacement_to_text
if(!types_to_replacement)
// Longer paths come after shorter ones, try and keep the structure
var/list/work_from = list(
/datum = "DATUM",
/area = "AREA",
/atom/movable = "MOVABLE",
/obj = "OBJ",
/turf = "TURF",
/turf/closed = "CLOSED",
/turf/open = "OPEN",

/mob = "MOB",
/mob/living = "LIVING",
/mob/living/carbon = "CARBON",
/mob/living/carbon/human = "HUMANOID",
/mob/living/simple_animal = "SIMPLE",
/mob/living/basic = "BASIC",
/mob/living/silicon = "SILICON",
/mob/living/silicon/robot = "CYBORG",

/obj/item = "ITEM",
/obj/item/mecha_parts/mecha_equipment = "MECHA_EQUIP",
/obj/item/mecha_parts/mecha_equipment/weapon = "MECHA_WEAPON",
/obj/item/organ = "ORGAN",
/obj/item/mod/control = "MODSUIT",
/obj/item/mod/module = "MODSUIT_MOD",
/obj/item/gun = "GUN",
/obj/item/gun/magic = "GUN_MAGIC",
/obj/item/gun/energy = "GUN_ENERGY",
/obj/item/gun/energy/laser = "GUN_LASER",
/obj/item/gun/ballistic = "GUN_BALLISTIC",
/obj/item/gun/ballistic/automatic = "GUN_AUTOMATIC",
/obj/item/gun/ballistic/revolver = "GUN_REVOLVER",
/obj/item/gun/ballistic/rifle = "GUN_RIFLE",
/obj/item/gun/ballistic/shotgun = "GUN_SHOTGUN",
/obj/item/stack/sheet = "SHEET",
/obj/item/stack/sheet/mineral = "MINERAL_SHEET",
/obj/item/stack/ore = "ORE",
/obj/item/ai_module = "AI_LAW_MODULE",
/obj/item/circuitboard = "CIRCUITBOARD",
/obj/item/circuitboard/machine = "MACHINE_BOARD",
/obj/item/circuitboard/computer = "COMPUTER_BOARD",
/obj/item/reagent_containers = "REAGENT_CONTAINERS",
/obj/item/reagent_containers/pill = "PILL",
/obj/item/reagent_containers/pill/patch = "MEDPATCH",
/obj/item/reagent_containers/hypospray/medipen = "MEDIPEN",
/obj/item/reagent_containers/cup/glass = "DRINK",
/obj/item/food = "FOOD",
/obj/item/bodypart = "BODYPART",
/obj/effect/decal/cleanable = "CLEANABLE",
/obj/item/radio/headset = "HEADSET",
/obj/item/clothing = "CLOTHING",
/obj/item/clothing/accessory = "ACCESSORY",
/obj/item/clothing/mask/gas = "GASMASK",
/obj/item/clothing/mask = "MASK",
Expand All @@ -21,75 +67,45 @@
/obj/item/clothing/head/helmet = "HELMET",
/obj/item/clothing/head = "HEAD",
/obj/item/clothing/neck = "NECK",
/obj/item/clothing = "CLOTHING",
/obj/item/storage/backpack = "BACKPACK",
/obj/item/storage/belt = "BELT",
/obj/item/book/manual = "MANUAL",
/obj/item/storage/pill_bottle = "PILL_BOTTLE",
/obj/item/reagent_containers/pill/patch = "MEDPATCH",
/obj/item/reagent_containers/pill = "PILL",
/obj/item/reagent_containers/hypospray/medipen = "MEDIPEN",
/obj/item/reagent_containers/cup/glass = "DRINK",
/obj/item/food = "FOOD",
/obj/item/reagent_containers = "REAGENT_CONTAINERS",
/obj/machinery/atmospherics = "ATMOS_MECH",
/obj/machinery/portable_atmospherics = "PORT_ATMOS",
/obj/item/mecha_parts/mecha_equipment/weapon = "MECHA_WEAPON",
/obj/item/mecha_parts/mecha_equipment = "MECHA_EQUIP",
/obj/item/organ = "ORGAN",
/obj/item/mod/control = "MODSUIT",
/obj/item/mod/module = "MODSUIT_MOD",
/obj/item/gun/ballistic/automatic = "GUN_AUTOMATIC",
/obj/item/gun/ballistic/revolver = "GUN_REVOLVER",
/obj/item/gun/ballistic/rifle = "GUN_RIFLE",
/obj/item/gun/ballistic/shotgun = "GUN_SHOTGUN",
/obj/item/gun/ballistic = "GUN_BALLISTIC",
/obj/item/gun/energy/laser = "GUN_LASER",
/obj/item/gun/energy = "GUN_ENERGY",
/obj/item/gun/magic = "GUN_MAGIC",
/obj/item/gun = "GUN",
/obj/item/stack/sheet/mineral = "MINERAL_SHEET",
/obj/item/stack/sheet = "SHEET",
/obj/item/stack/ore = "ORE",
/obj/item/ai_module = "AI_LAW_MODULE",
/obj/item/circuitboard/machine = "MACHINE_BOARD",
/obj/item/circuitboard/computer = "COMPUTER_BOARD",
/obj/item/circuitboard = "CIRCUITBOARD",
/obj/item = "ITEM",
/obj/structure/closet/crate/secure = "LOCKED_CRATE",
/obj/item/book/manual = "MANUAL",

/obj/structure = "STRUCTURE",
/obj/structure/closet = "CLOSET",
/obj/structure/closet/crate = "CRATE",
/obj/structure/closet/crate/secure = "LOCKED_CRATE",
/obj/structure/closet/secure_closet = "LOCKED_CLOSET",
/obj/structure/closet = "CLOSET",
/obj/structure = "STRUCTURE",
/obj/machinery/door/airlock = "AIRLOCK",

/obj/machinery = "MACHINERY",
/obj/machinery/atmospherics = "ATMOS_MECH",
/obj/machinery/portable_atmospherics = "PORT_ATMOS",
/obj/machinery/door = "DOOR",
/obj/machinery/door/airlock = "AIRLOCK",
/obj/machinery/rnd/production = "RND_FABRICATOR",
/obj/machinery/computer/camera_advanced/shuttle_docker = "DOCKING_COMPUTER",
/obj/machinery/computer = "COMPUTER",
/obj/machinery/vending/wardrobe = "JOBDROBE",
/obj/machinery/computer/camera_advanced/shuttle_docker = "DOCKING_COMPUTER",
/obj/machinery/vending = "VENDING",
/obj/machinery = "MACHINERY",
/obj/machinery/vending/wardrobe = "JOBDROBE",
/obj/effect = "EFFECT",
/obj/projectile = "PROJECTILE",
/obj = "O",
/datum = "D",
/turf/open = "OPEN",
/turf/closed = "CLOSED",
/turf = "T",
/mob/living/carbon/human = "HUMANOID",
/mob/living/carbon = "CARBON",
/mob/living/simple_animal = "SIMPLE",
/mob/living/basic = "BASIC",
/mob/living/silicon/robot = "CYBORG",
/mob/living/silicon = "SILICON",
/mob/living = "LIVING",
/mob = "M",
)
for (var/tn in TYPES_SHORTCUTS)
if(copytext(typename, 1, length("[tn]/") + 1) == "[tn]/" /*findtextEx(typename,"[tn]/",1,2)*/ )
typename = TYPES_SHORTCUTS[tn] + copytext(typename, length("[tn]/"))
break
.[typename] = type
// ignore_root_path so we can draw the root normally
types_to_replacement = zebra_typecacheof(work_from, ignore_root_path = TRUE)
replacement_to_text = list()
for(var/key in work_from)
replacement_to_text[work_from[key]] = "[key]"

. = list()
for(var/type in types)
var/replace_with = types_to_replacement[type]
if(!replace_with)
.["[type]"] = type
continue
var/cut_out = replacement_to_text[replace_with]
// + 1 to account for /
.[replace_with + copytext("[type]", length(cut_out) + 1)] = type

/proc/get_fancy_list_of_atom_types()
var/static/list/pre_generated_list
Expand Down
3 changes: 1 addition & 2 deletions code/modules/admin/admin_verbs.dm
Original file line number Diff line number Diff line change
Expand Up @@ -1211,8 +1211,7 @@ GLOBAL_PROTECT(admin_verbs_poll)

var/desired_mob = text2path(attempted_target_path)
if(!ispath(desired_mob))
var/static/list/mob_paths = make_types_fancy(subtypesof(/mob/living))
desired_mob = pick_closest_path(attempted_target_path, mob_paths)
desired_mob = pick_closest_path(attempted_target_path, make_types_fancy(subtypesof(/mob/living)))
if(isnull(desired_mob) || !ispath(desired_mob) || QDELETED(head))
return //The user pressed "Cancel"

Expand Down

0 comments on commit 4bba66e

Please sign in to comment.