From cf7cb8751e2afa3c993b4033eccf03be97a1ffee Mon Sep 17 00:00:00 2001 From: kevinz000 <2003111+kevinz000@users.noreply.github.com> Date: Sat, 1 Feb 2020 00:43:59 -0700 Subject: [PATCH] speeds up get_hearers_in_view/getallcontents just a little bit (#48975) * Update game.dm * Update unsorted.dm * good point * one indexed lists and ++i is faster * ditto --- code/__HELPERS/game.dm | 15 ++++++--------- code/__HELPERS/unsorted.dm | 30 ++++++++++++++---------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/code/__HELPERS/game.dm b/code/__HELPERS/game.dm index e6424592ff6..f3ff9dcf1ae 100644 --- a/code/__HELPERS/game.dm +++ b/code/__HELPERS/game.dm @@ -152,11 +152,11 @@ /proc/recursive_hear_check(O) var/list/processing_list = list(O) . = list() - while(processing_list.len) - var/atom/A = processing_list[1] + var/i = 0 + while(i < length(processing_list)) + var/atom/A = processing_list[++i] if(A.flags_1 & HEAR_1) . += A - processing_list.Cut(1, 2) processing_list += A.contents /** recursive_organ_check @@ -200,10 +200,8 @@ // Returns a list of hearers in view(R) from source (ignoring luminosity). Used in saycode. var/turf/T = get_turf(source) . = list() - if(!T) return - var/list/processing_list = list() if (R == 0) // if the range is zero, we know exactly where to look for, we can skip view processing_list += T.contents // We can shave off one iteration by assuming turfs cannot hear @@ -216,12 +214,12 @@ processing_list += O T.luminosity = lum - while(processing_list.len) // recursive_hear_check inlined here - var/atom/A = processing_list[1] + var/i = 0 + while(i < length(processing_list)) // recursive_hear_check inlined here + var/atom/A = processing_list[++i] if(A.flags_1 & HEAR_1) . += A SEND_SIGNAL(A, COMSIG_ATOM_HEARER_IN_VIEW, processing_list, .) - processing_list.Cut(1, 2) processing_list += A.contents /proc/get_mobs_in_radio_ranges(list/obj/item/radio/radios) @@ -231,7 +229,6 @@ if(R) . |= get_hearers_in_view(R.canhear_range, R) - #define SIGNV(X) ((X<0)?-1:1) /proc/inLineOfSight(X1,Y1,X2,Y2,Z=1,PX1=16.5,PY1=16.5,PX2=16.5,PY2=16.5) diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index 4001a960449..bb3a7136815 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -460,36 +460,34 @@ Turf and target are separate in case you want to teleport some distance from a t /atom/proc/GetAllContents(var/T) var/list/processing_list = list(src) - var/list/assembled = list() if(T) - while(processing_list.len) - var/atom/A = processing_list[1] - processing_list.Cut(1, 2) + . = list() + var/i = 0 + while(i < length(processing_list)) + var/atom/A = processing_list[++i] //Byond does not allow things to be in multiple contents, or double parent-child hierarchies, so only += is needed //This is also why we don't need to check against assembled as we go along processing_list += A.contents if(istype(A,T)) - assembled += A + . += A else - while(processing_list.len) - var/atom/A = processing_list[1] - processing_list.Cut(1, 2) + var/i = 0 + while(i < length(processing_list)) + var/atom/A = processing_list[++i] processing_list += A.contents - assembled += A - return assembled + return processing_list /atom/proc/GetAllContentsIgnoring(list/ignore_typecache) if(!length(ignore_typecache)) return GetAllContents() var/list/processing = list(src) - var/list/assembled = list() - while(processing.len) - var/atom/A = processing[1] - processing.Cut(1,2) + . = list() + var/i = 0 + while(i < length(processing)) + var/atom/A = processing[++i] if(!ignore_typecache[A.type]) processing += A.contents - assembled += A - return assembled + . += A //Step-towards method of determining whether one atom can see another. Similar to viewers() /proc/can_see(atom/source, atom/target, length=5) // I couldnt be arsed to do actual raycasting :I This is horribly inaccurate.