Files
Bubberstation/code/modules/client/preferences/assets.dm
Mothblocks af8902331d Amortize the cost of creating preference assets by caching them per git revision on production, reducing best case init times by ~20 seconds (#63503)
Preference asset creation, which while consistently created in early assets, can be requested at any time before then and often is, currently takes about 15 to 25 seconds to produce. Because of extremely hard to reproduce BYOND icon bugs, most of this is done on the same tick.

Lowering the cost of initialization itself is very tricky. Some of it we can theoretically optimize, such as creating humans for antagonists, others we can't, such as the raw cost of icon blending.

Furthermore, adding new icons later down the line would just increase this initialization time even more.

Instead of optimizing the asset creation, which is an uphill battle, this instead chooses to amortize the cost by caching preference assets created per git revision. This means that preference assets will be created, with their long delay, only once whenever the code changes.

This is done on a config, defaulting to on so that production needs no changes, as the whole point of these being made at runtime at all is that it keeps assets/art styles consistent, and PRs making subtle bugs that break preference generation in some way is not uncommon. On development, your git revision will stay the same until you commit, no matter what code changes you make.
2022-01-01 04:36:30 +00:00

66 lines
2.2 KiB
Plaintext

/// Assets generated from `/datum/preference` icons
/datum/asset/spritesheet/preferences
name = "preferences"
early = TRUE
cross_round_cachable = TRUE
/datum/asset/spritesheet/preferences/create_spritesheets()
var/list/to_insert = list()
for (var/preference_key in GLOB.preference_entries_by_key)
var/datum/preference/choiced/preference = GLOB.preference_entries_by_key[preference_key]
if (!istype(preference))
continue
if (!preference.should_generate_icons)
continue
var/list/choices = preference.get_choices_serialized()
for (var/preference_value in choices)
var/create_icon_of = choices[preference_value]
var/icon/icon
var/icon_state
if (ispath(create_icon_of, /atom))
var/atom/atom_icon_source = create_icon_of
icon = initial(atom_icon_source.icon)
icon_state = initial(atom_icon_source.icon_state)
else if (isicon(create_icon_of))
icon = create_icon_of
else
CRASH("[create_icon_of] is an invalid preference value (from [preference_key]:[preference_value]).")
to_insert[preference.get_spritesheet_key(preference_value)] = list(icon, icon_state)
for (var/spritesheet_key in to_insert)
var/list/inserting = to_insert[spritesheet_key]
Insert(spritesheet_key, inserting[1], inserting[2])
/// Returns the key that will be used in the spritesheet for a given value.
/datum/preference/proc/get_spritesheet_key(value)
return "[savefile_key]___[sanitize_css_class_name(value)]"
/// Sends information needed for shared details on individual preferences
/datum/asset/json/preferences
name = "preferences"
/datum/asset/json/preferences/generate()
var/list/preference_data = list()
for (var/middleware_type in subtypesof(/datum/preference_middleware))
var/datum/preference_middleware/middleware = new middleware_type
var/data = middleware.get_constant_data()
if (!isnull(data))
preference_data[middleware.key] = data
qdel(middleware)
for (var/preference_type in GLOB.preference_entries)
var/datum/preference/preference_entry = GLOB.preference_entries[preference_type]
var/data = preference_entry.compile_constant_data()
if (!isnull(data))
preference_data[preference_entry.savefile_key] = data
return preference_data