From 8d911ccc19554def6aed9fdc48f53fba3183d584 Mon Sep 17 00:00:00 2001 From: gasmo Date: Fri, 18 Aug 2023 14:17:54 +0000 Subject: [PATCH] Fix ballooning recursive function (!120) The `BotEquipmentModPoolService.generatePool()` method is recursive, but it does not check if the pool it creates already contains data for sub items when it finds them and calls itself on them. This creates substantial bloat in the function that is undetectable to end users, but causes some additional delay when it is called. I noticed this when using a server mod that allows for less restrictions on mod combinations (as it would hit the system recursion limit and error out) but the bloat still affects vanilla - it takes ~1 minute for all the recursive calls to end, whereas this fix reduces that to <10 seconds. To fix this, I've moved a block of code into the conditional block that checks if the item we're adding (and its' sub items) are already in the pool before it calls itself. This is my first pull request, so let me know if I've fudged up somewhere and I'll do my best to fix it. I submitted a previous request about this, but I removed my name from the commits so I had to delete the branch. Co-authored-by: gasmo Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/120 Co-authored-by: gasmo Co-committed-by: gasmo --- .../services/BotEquipmentModPoolService.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/project/src/services/BotEquipmentModPoolService.ts b/project/src/services/BotEquipmentModPoolService.ts index fbcc424e..a10cda24 100644 --- a/project/src/services/BotEquipmentModPoolService.ts +++ b/project/src/services/BotEquipmentModPoolService.ts @@ -83,16 +83,16 @@ export class BotEquipmentModPoolService if (!pool[item._id][slot._name].some(x => x === itemToAdd)) { pool[item._id][slot._name].push(itemToAdd); - } - - // Check item added into array for slots, need to iterate over those - const subItemDetails = this.databaseServer.getTables().templates.items[itemToAdd]; - const hasSubItemsToAdd = subItemDetails?._props?.Slots?.length > 0; - if (hasSubItemsToAdd) - { - // Recursive call - this.generatePool([subItemDetails], poolType); - } + + // Check item added into array for slots, need to iterate over those + const subItemDetails = this.databaseServer.getTables().templates.items[itemToAdd]; + const hasSubItemsToAdd = subItemDetails?._props?.Slots?.length > 0; + if (hasSubItemsToAdd && !pool[subItemDetails._id]) + { + // Recursive call + this.generatePool([subItemDetails], poolType); + } + } } } }