From 0e037767cda41dafb02b4c347f698dc2f4004d8b Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Tue, 31 Oct 2017 16:10:52 -0400 Subject: [PATCH 1/2] Minor optimization for _GetInverseTypeList (#32155) * Minor optimization for _GetInverseTypeList * We also need to make sure the root component is never instantiated to avoid stack overflows --- code/datums/components/_component.dm | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/code/datums/components/_component.dm b/code/datums/components/_component.dm index 4e187a6a08..91a6436d04 100644 --- a/code/datums/components/_component.dm +++ b/code/datums/components/_component.dm @@ -6,6 +6,10 @@ var/datum/parent /datum/component/New(datum/P, ...) + if(type == /datum/component) + qdel(src) + CRASH("[type] instantiated!") + parent = P var/list/arguments = args.Copy() arguments.Cut(1, 2) @@ -91,8 +95,7 @@ /datum/component/proc/_RemoveFromParent() var/datum/P = parent var/list/dc = P.datum_components - var/our_type = type - for(var/I in _GetInverseTypeList(our_type)) + for(var/I in _GetInverseTypeList()) var/list/components_of_type = dc[I] if(islist(components_of_type)) // var/list/subtracted = components_of_type - src @@ -133,8 +136,11 @@ set waitfor = FALSE return -/datum/component/proc/_GetInverseTypeList(current_type) - . = list(current_type) +/datum/component/proc/_GetInverseTypeList(our_type = type) + //we can do this one simple trick + var/current_type = parent_type + . = list(our_type, current_type) + //and since most components are root level + 1, this won't even have to run while (current_type != /datum/component) current_type = type2parent(current_type) . += current_type