Micro-optimisations

Moved `GetInventoryItemHash` to extensions and rewrote to make better use of linq
This commit is contained in:
Chomp
2025-07-19 11:11:59 +01:00
parent 5f5bf3c39f
commit d7f83e1b7d
3 changed files with 28 additions and 39 deletions
@@ -668,7 +668,7 @@ public class InventoryHelper(
public (int, int) GetItemSize(MongoId itemTpl, MongoId itemId, List<Item> inventoryItems)
{
// -> Prepares item Width and height returns [sizeX, sizeY]
return GetSizeByInventoryItemHash(itemTpl, itemId, GetInventoryItemHash(inventoryItems));
return GetSizeByInventoryItemHash(itemTpl, itemId, inventoryItems.GetInventoryItemHash());
}
/// <summary>
@@ -755,7 +755,7 @@ public class InventoryHelper(
{
// Storage for root item and its children, store root item id for now
// Will store child items that may have sub-children to process
var toDo = new Queue<string>([itemId]);
var toDo = new Queue<MongoId>([itemId]);
while (toDo.Count > 0)
{
// Lookup parent in `to do queue`, get all of its children, then loop over them
@@ -870,7 +870,7 @@ public class InventoryHelper(
);
// Get all items in players inventory keyed by their parentId and by ItemId
var inventoryItemHash = GetInventoryItemHash(itemList);
var inventoryItemHash = itemList.GetInventoryItemHash();
// Get subset of items that belong to the desired container
if (!inventoryItemHash.ByParentId.TryGetValue(containerId, out var rootItemsInContainer))
@@ -948,38 +948,6 @@ public class InventoryHelper(
return container;
}
protected InventoryItemHash GetInventoryItemHash(List<Item> inventoryItems)
{
var inventoryItemHash = new InventoryItemHash
{
ByItemId = new Dictionary<MongoId, Item>(),
ByParentId = new Dictionary<MongoId, HashSet<Item>>(),
};
foreach (var item in inventoryItems)
{
inventoryItemHash.ByItemId.TryAdd(item.Id, item);
if (item.ParentId is null)
{
continue;
}
if (item.ParentId == "hideout")
{
continue;
}
if (!inventoryItemHash.ByParentId.ContainsKey(item.ParentId))
{
inventoryItemHash.ByParentId[item.ParentId] = [];
}
inventoryItemHash.ByParentId[item.ParentId].Add(item);
}
return inventoryItemHash;
}
/// <summary>
/// Return the inventory that needs to be modified (scav/pmc etc)
/// Changes made to result apply to character inventory