From d17a4dbccd599a54d1ecfbcd71e8c71798228f09 Mon Sep 17 00:00:00 2001 From: Mothblocks <35135081+Mothblocks@users.noreply.github.com> Date: Mon, 15 May 2023 15:22:11 -0700 Subject: [PATCH] Fix very common smoothing groups runtime by deferring icon smoothing until all atom initializations are complete (#75444) ## About The Pull Request Fixes the "bad index" runtime that shows up on 80% of rounds. ![image](https://github.com/tgstation/tgstation/assets/35135081/8b8a7b6a-90b6-4c07-8e3b-47f31e4864c5) The issue is that during dynamic maploading, the initialization of objects is split over several ticks. However, before the initialization is complete, an object's smoothing groups is still in its string form (as per [the smoothing group optimizations](https://github.com/tgstation/tgstation/pull/71989)). Thus, code later down the line attempts to read from this string, and errors. It would be expensive to check all nearby neighbors every time in any place in this code, so instead we defer icon smoothing if there are any initializations currently in progress. Because this only happens for dynamic loading, the stuff being loaded dynamically is usually small, and icon smoothing already has a somewhat visible delay for shuttles, this delay doesn't matter. ## Changelog :cl: fix: Fixed an issue where objects on something that loaded dynamically, like shuttles, would not be smoothed. /:cl: --- code/controllers/subsystem/atoms.dm | 4 ++++ code/controllers/subsystem/icon_smooth.dm | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/code/controllers/subsystem/atoms.dm b/code/controllers/subsystem/atoms.dm index ff4641a66cf..13a6befbd04 100644 --- a/code/controllers/subsystem/atoms.dm +++ b/code/controllers/subsystem/atoms.dm @@ -178,6 +178,10 @@ SUBSYSTEM_DEF(atoms) if(!initialized_changed) initialized = old_initialized +/// Returns TRUE if anything is currently being initialized +/datum/controller/subsystem/atoms/proc/initializing_something() + return initialized_changed > 0 + /datum/controller/subsystem/atoms/Recover() initialized = SSatoms.initialized if(initialized == INITIALIZATION_INNEW_MAPLOAD) diff --git a/code/controllers/subsystem/icon_smooth.dm b/code/controllers/subsystem/icon_smooth.dm index d3de5a1b4ac..4d5f3069eca 100644 --- a/code/controllers/subsystem/icon_smooth.dm +++ b/code/controllers/subsystem/icon_smooth.dm @@ -11,6 +11,13 @@ SUBSYSTEM_DEF(icon_smooth) var/list/deferred = list() /datum/controller/subsystem/icon_smooth/fire() + // We do not want to smooth icons of atoms whose neighbors are not initialized yet, + // this causes runtimes. + // Icon smoothing SS runs after atoms, so this only happens for something like shuttles. + // This kind of map loading shouldn't take too long, so the delay is not a problem. + if (SSatoms.initializing_something()) + return + var/list/smooth_queue_cache = smooth_queue while(length(smooth_queue_cache)) var/atom/smoothing_atom = smooth_queue_cache[length(smooth_queue_cache)]