fix: Eliminates List allocations in various places. (#2158)

This commit is contained in:
Kamron Batman 2025-11-16 18:33:53 -08:00 committed by GitHub
parent 6f64cddd0b
commit 4836bff5eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 263 additions and 200 deletions

View file

@ -430,8 +430,8 @@ public partial class Container : Item
public virtual bool TryDropItems(Mobile from, bool sendFullMessage, params ReadOnlySpan<Item> droppedItems)
{
var dropItems = new List<Item>();
var stackItems = new List<ItemStackEntry>();
using var dropItems = PooledRefQueue<Item>.Create();
using var stackItems = PooledRefQueue<ItemStackEntry>.Create();
var extraItems = 0;
var extraWeight = 0;
@ -451,7 +451,7 @@ public partial class Container : Item
if (item is not Container && CheckHold(from, dropped, false, false, 0, extraWeight) &&
item.CanStackWith(dropped))
{
stackItems.Add(new ItemStackEntry(item, dropped));
stackItems.Enqueue(new ItemStackEntry(item, dropped));
extraWeight += (int)Math.Ceiling(item.Weight * (item.Amount + dropped.Amount)) -
item.PileWeight; // extra weight delta, do not need TotalWeight as we do not have hybrid stackable container types
stacked = true;
@ -461,7 +461,7 @@ public partial class Container : Item
if (!stacked && CheckHold(from, dropped, false, true, extraItems, extraWeight))
{
dropItems.Add(dropped);
dropItems.Enqueue(dropped);
extraItems++;
extraWeight += dropped.TotalWeight + dropped.PileWeight;
}
@ -469,14 +469,15 @@ public partial class Container : Item
if (dropItems.Count + stackItems.Count == droppedItems.Length) // All good
{
for (var i = 0; i < dropItems.Count; i++)
while (dropItems.Count > 0)
{
DropItem(dropItems[i]);
DropItem(dropItems.Dequeue());
}
for (var i = 0; i < stackItems.Count; i++)
while (stackItems.Count > 0)
{
stackItems[i].m_StackItem.StackWith(from, stackItems[i].m_DropItem, false);
var stackItem = stackItems.Dequeue();
stackItem.m_StackItem.StackWith(from, stackItem.m_DropItem, false);
}
return true;