From 44f22c2128bdb4de55cbd3bee9a6e9a8a4f35d5c Mon Sep 17 00:00:00 2001 From: skull132 Date: Mon, 2 Jan 2017 00:45:36 +0200 Subject: [PATCH] Improve Object Initialization (#1368) Okay. You can read the entire theory over in this issue: #1365 But basically. All objects to be initialized are now added to a list in their New() proc on world/New(). This is substantially smaller than the entire world. This allows game_controller/setup_objects() to crawl a much larger list, and speeds it up by 6 seconds (from 9.5 to 3.5 seconds). The impact on world/New() is, on average, under 1 second. Also fixes #1365 and #1363 as a result of reworking the initialization call logic in atoms_movable.dm. initialize() is now more secure, and can handle recursive calls at any point in time. --- code/controllers/master_controller.dm | 9 +++++++-- code/game/atoms_movable.dm | 8 +++++--- code/global.dm | 7 ++++++- code/world.dm | 3 +++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/code/controllers/master_controller.dm b/code/controllers/master_controller.dm index ae1203df9ae..cca7c5e9b36 100644 --- a/code/controllers/master_controller.dm +++ b/code/controllers/master_controller.dm @@ -46,13 +46,18 @@ datum/controller/game_controller/proc/setup() datum/controller/game_controller/proc/setup_objects() admin_notice("Initializing objects", R_DEBUG) sleep(-1) - for(var/atom/movable/object in world) + objects_initialized = 1 + for(var/A in objects_init_list) + var/atom/movable/object = A if(isnull(object.gcDestroyed)) object.initialize() + objects_init_list.Cut() + admin_notice("Initializing areas", R_DEBUG) sleep(-1) - for(var/area/area in all_areas) + for(var/A in all_areas) + var/area/area = A area.initialize() admin_notice("Initializing pipe networks", R_DEBUG) diff --git a/code/game/atoms_movable.dm b/code/game/atoms_movable.dm index 6c918a78788..78e928f0f8b 100644 --- a/code/game/atoms_movable.dm +++ b/code/game/atoms_movable.dm @@ -20,8 +20,11 @@ /atom/movable/New() ..() - if(auto_init && ticker && ticker.current_state == GAME_STATE_PLAYING) - initialize() + if (objects_initialized) + if (auto_init) + initialize() + else + objects_init_list += src /atom/movable/Del() if(isnull(gcDestroyed) && loc) @@ -271,4 +274,3 @@ var/list/accessible_z_levels = list("1" = 5, "3" = 10, "4" = 15, "6" = 60) if(!candidates.len) return null return text2num(pickweight(candidates)) - diff --git a/code/global.dm b/code/global.dm index eeec488d973..93da2ce5bd7 100644 --- a/code/global.dm +++ b/code/global.dm @@ -167,4 +167,9 @@ var/global/const/TICKS_IN_SECOND = 10 //List of exosuit tracking beacons, to save performance -var/global/list/exo_beacons = list() \ No newline at end of file +var/global/list/exo_beacons = list() + +// Global variables to speed up object initialization. +// Boolean to indicate whether objects should initialize themselves in their New() or wait for the game ticker to do it. +// Check world.dm for the object list. +var/global/objects_initialized = 0 diff --git a/code/world.dm b/code/world.dm index f01e5f4a64f..9f73fcc7876 100644 --- a/code/world.dm +++ b/code/world.dm @@ -11,6 +11,9 @@ */ var/global/datum/global_init/init = new () +// The list of objects that will need to be initialized in ticker creation. +var/global/list/objects_init_list = list() + /* Pre-map initialization stuff should go here. */