Optimizes /proc/icon_exists() (#89357)

## About The Pull Request
This PR reimplements https://github.com/tgstation/tgstation/pull/71538
atop `master`. Quoting the original PR:

> Every `icon_exists()` call will cache the entire file. Past me didn't
realise _why_ file opts were so expensive, but I do now. This is
immeasurably slower on a single call, and _significantly_ faster on
subsequent calls to the same file.

I attempted to handle some of the review comments that were posted
there, by splitting screaming functionality into its own proc.

* `if(icon_state in icon_states(file))` and `if(!(icon_state in
icon_states(file)))` were refactored to use `icon_exists(file,
icon_state)`.
* Where screaming was seemingly wanted (and where there wasn't a more
descriptive error inside the `if` block), I refactored them to use
`icon_exists_or_scream(file, icon_state)`
* The exception to the above was under
`/datum/unit_test/turf_icons/Run()` and
`/datum/unit_test/worn_icons/Run()`, where `icon_states()` was being
passed a mode flag. Given that this is only used in unit tests (where
performance isn't a priority), I opted to leave these be.

Additionally, I revised the documentation comment for
`/proc/icon_exists()`, as I felt it was a bit vague currently.

## Why It's Good For The Game
https://youtu.be/Z9G1Mf6TZRs

## Changelog
No player-facing changes (hopefully).

---------

Co-authored-by: SyncIt21 <110812394+SyncIt21@users.noreply.github.com>
This commit is contained in:
MichiRecRoom
2025-02-05 20:00:27 +01:00
committed by GitHub
co-authored by SyncIt21
parent 465f654548
commit 344d3b6266
14 changed files with 56 additions and 51 deletions
+1 -1
View File
@@ -392,7 +392,7 @@ GLOBAL_LIST_EMPTY(asset_datums)
/datum/asset/spritesheet/proc/queuedInsert(sprite_name, icon/I, icon_state="", dir=SOUTH, frame=1, moving=FALSE)
#ifdef UNIT_TESTS
if (I && icon_state && !(icon_state in icon_states(I))) // check the base icon prior to extracting the state we want
if (I && icon_state && !icon_exists(I, icon_state)) // check the base icon prior to extracting the state we want
stack_trace("Tried to insert nonexistent icon_state '[icon_state]' from [I] into spritesheet [name] ([type])")
return
#endif
+1 -1
View File
@@ -40,7 +40,7 @@
icon_state ||= initial(preview_item.icon_state_preview) || initial(preview_item.icon_state)
if(PERFORM_ALL_TESTS(focus_only/bad_cooking_crafting_icons))
if(!icon_exists(icon_file, icon_state, scream = TRUE))
if(!icon_exists_or_scream(icon_file, icon_state))
return
Insert("a[id]", icon(icon_file, icon_state, SOUTH))
@@ -15,7 +15,7 @@
icon_file = initial(path.research_icon)
icon_state = initial(path.research_icon_state)
if (PERFORM_ALL_TESTS(focus_only/invalid_research_designs))
if(!(icon_state in icon_states(icon_file)))
if(!icon_exists(icon_file, icon_state))
stack_trace("design [path] with icon '[icon_file]' missing state '[icon_state]'")
continue
I = icon(icon_file, icon_state, SOUTH)
@@ -48,7 +48,7 @@
icon_state = initial(item.icon_state)
if (PERFORM_ALL_TESTS(focus_only/invalid_research_designs))
if(!(icon_state in icon_states(icon_file)))
if(!icon_exists(icon_file, icon_state))
stack_trace("design [path] with icon '[icon_file]' missing state '[icon_state]'")
continue
I = icon(icon_file, icon_state, SOUTH)