perf: Eliminates allocations in Container searching. (#2409)

## Summary

Removes per-call heap allocations from `Container`'s consume / find / group hot paths and from `BaseCreature.OnDeath`'s fame/karma tracking. The headline wins: kill the `List<List<Item>>` + `Item[][]` + `int[]` grouping bridges in `ConsumeTotal*` / `ConsumeTotalGrouped*` / `GetBestGroupAmount*`, and kill the per-call `Predicate<Item>` allocations in `FindItemsByType(Type)` / `FindItemsByType(Type[])`.

### `Container.cs`

- `ConsumeTotal`, `ConsumeTotalGrouped`, `GetBestGroupAmount` now share four streaming helpers (`HasAmount`, `TryFindGroupMeetingAmount`, `BestGroupTotal`, `ConsumeSlice`) backed by `PooledRefList` instead of allocating per-group lists and jagged arrays. Two-phase validate-then-consume pattern preserved — all-or-nothing semantics for spell reagents, vendor pay, and crafting still hold.
- `(Type)` / `(Type[])` / `(Type[][])` overload trios collapsed to single `ReadOnlySpan<Type>` + `ReadOnlySpan<int>` implementations. Implicit `T[] → ReadOnlySpan<T>` conversion means UOContent callers compile unchanged.
- Unused overloads deleted: `ConsumeTotalGrouped(Type)`, `ConsumeTotalGrouped(Type[][])`, `GetBestGroupAmount(Type)`, `GetBestGroupAmount(Type[][])`, plus the never-called `TryDropItems` hook and its private `ItemStackEntry` struct.
- Fixes a `PooledRefList` leak in `GetBestGroupAmount(Type[], …)` (missing `using`).
- `m_ContainerData` / `m_Items` / `m_TotalGold` / `m_TotalItems` / `m_TotalWeight` / `ContainerData.m_Table` / `ContainerData.logger` renamed to the underscored convention. `m_Items` cross-file rename for the Container-side references in `Item.cs`; `Item.CompactInfo.m_Items` deliberately left alone (separate effort).
- `CheckHold` parent walk simplified; trivial dispatch methods (`CheckHold` overloads, `OnItemAdded`, `OnItemRemoved`, `OnStackAttempt`) get `[MethodImpl(AggressiveInlining)]`; `Destroy` and `DisplayTo` cache `Items` outside the loop; dead comments removed.

### `Item.Enumerable.cs`

- `FindItemsByType(Type)` previously allocated a `Predicate<Item>` per call (method-group conversion). `FindItemsByType(Type[])` allocated a closure capturing `types`. Both now construct the enumerator with a `Type` / `ReadOnlySpan<Type>` field directly, no delegate.
- `FindItemsByTypeEnumerator<T>` gains two constructors plus a `Matches(T)` helper that picks the right filter inline. Constructor chaining via a private 2-arg seed constructor incidentally fixes a pre-existing bug where `PooledRefQueue` was always rented at capacity 0 because `_recurse` hadn't been assigned yet.
- `(Type[])` overload of `FindItemsByType` becomes `(ReadOnlySpan<Type>)`.
- `EnumerateItemsByType(Type)` / `EnumerateItemsByType(ReadOnlySpan<Type>)` / `ListItemsByType(Type)` / `ListItemsByType(ReadOnlySpan<Type>)` simplified to delegate to the new alloc-free overloads instead of filtering manually.

### `Utility.cs`

- `InTypeList<T>(this T, Type[])` and `InTypeList(this Type, Type[])` switched to `ReadOnlySpan<Type>`.

### `BaseCreature.cs`

- `OnDeath` per-death `List<Mobile>` / `List<int>` / `List<int>` for fame/karma tracking switched to `PooledRefList`.
This commit is contained in:
Kamron Batman 2026-04-25 13:40:21 -07:00 committed by GitHub
parent 597c81345e
commit c552f65673
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 771 additions and 792 deletions

File diff suppressed because it is too large Load diff

View file

@ -54,16 +54,16 @@ public partial class Item
/// <paramref name="predicate" />.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public FindItemsByTypeEnumerator<T> FindItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) where T : Item =>
new(this, recurse, predicate);
public FindItemsByTypeEnumerator<T> FindItemsByType<T>(bool recurse = true, Predicate<T> predicate = null)
where T : Item => new(this, recurse, predicate);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public FindItemsByTypeEnumerator<Item> FindItemsByType(Type type, bool recurse = true) =>
new(this, recurse, type.IsInstanceOfType);
new(this, recurse, type);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public FindItemsByTypeEnumerator<Item> FindItemsByType(Type[] types, bool recurse = true) =>
new(this, recurse, item => item.InTypeList(types));
public FindItemsByTypeEnumerator<Item> FindItemsByType(ReadOnlySpan<Type> types, bool recurse = true) =>
new(this, recurse, types);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public FindItemsByTypeEnumerator<Item> FindItems(bool recurse = true, Predicate<Item> predicate = null) =>
@ -122,28 +122,22 @@ public partial class Item
{
var queue = PooledRefQueue<Item>.Create(128);
foreach (var item in FindItemsByType<Item>(recurse))
foreach (var item in FindItemsByType(type, recurse))
{
if (type.IsInstanceOfType(item))
{
queue.Enqueue(item);
}
queue.Enqueue(item);
}
return queue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public PooledRefQueue<Item> EnumerateItemsByType(Type[] types, bool recurse = true)
public PooledRefQueue<Item> EnumerateItemsByType(ReadOnlySpan<Type> types, bool recurse = true)
{
var queue = PooledRefQueue<Item>.Create(128);
foreach (var item in FindItemsByType<Item>(recurse))
foreach (var item in FindItemsByType(types, recurse))
{
if (item.InTypeList(types))
{
queue.Enqueue(item);
}
queue.Enqueue(item);
}
return queue;
@ -170,28 +164,22 @@ public partial class Item
{
var list = PooledRefList<Item>.Create(128);
foreach (var item in FindItemsByType<Item>(recurse))
foreach (var item in FindItemsByType(type, recurse))
{
if (type.IsInstanceOfType(item))
{
list.Add(item);
}
list.Add(item);
}
return list;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public PooledRefList<Item> ListItemsByType(Type[] types, bool recurse = true)
public PooledRefList<Item> ListItemsByType(ReadOnlySpan<Type> types, bool recurse = true)
{
var list = PooledRefList<Item>.Create(128);
foreach (var item in FindItemsByType<Item>(recurse))
foreach (var item in FindItemsByType(types, recurse))
{
if (item.InTypeList(types))
{
list.Add(item);
}
list.Add(item);
}
return list;
@ -211,13 +199,30 @@ public partial class Item
private int _index;
private T _current;
private readonly bool _recurse;
private readonly Predicate<T> _predicate;
private Item _currentContainer;
private int _version;
// Exactly one filter source is used per enumerator instance, depending on
// which constructor was called. The unused fields stay at default and the
// branches below pick the right path. This avoids the per-call delegate
// allocation that the (Type)/(Type[]) factory methods used to incur.
private readonly Predicate<T> _predicate;
private readonly Type _runtimeType;
private readonly ReadOnlySpan<Type> _runtimeTypes;
public FindItemsByTypeEnumerator(Item container, bool recurse, Predicate<T> predicate)
: this(container, recurse) => _predicate = predicate;
public FindItemsByTypeEnumerator(Item container, bool recurse, Type runtimeType)
: this(container, recurse) => _runtimeType = runtimeType;
public FindItemsByTypeEnumerator(Item container, bool recurse, ReadOnlySpan<Type> runtimeTypes)
: this(container, recurse) => _runtimeTypes = runtimeTypes;
private FindItemsByTypeEnumerator(Item container, bool recurse)
{
_containers = PooledRefQueue<Item>.Create(_recurse ? 64 : 0);
_recurse = recurse;
_containers = PooledRefQueue<Item>.Create(recurse ? 64 : 0);
if (container != null)
{
@ -230,11 +235,6 @@ public partial class Item
_currentContainer = container;
_version = container.LookupContainerVersion();
}
_current = default;
_index = 0;
_recurse = recurse;
_predicate = predicate;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -259,6 +259,22 @@ public partial class Item
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool Matches(T t)
{
if (_runtimeType is not null)
{
return _runtimeType.IsInstanceOfType(t);
}
if (_runtimeTypes.Length > 0)
{
return t.GetType().InTypeList(_runtimeTypes);
}
return _predicate?.Invoke(t) != false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool SetNextItem()
{
@ -270,12 +286,12 @@ public partial class Item
while (_index < _items.Length)
{
var item = _items[_index++];
if (_recurse && item.LookupItems() is { Count: > 0 } items)
if (_recurse && item.LookupItems() is { Count: > 0 })
{
_containers.Enqueue(item);
}
if (item is T t && _predicate?.Invoke(t) != false)
if (item is T t && Matches(t))
{
if (_version != _currentContainer.LookupContainerVersion())
{

View file

@ -1705,13 +1705,13 @@ public partial class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropert
}
}
public List<Item> LookupItems() => (this is Container container ? container.m_Items : LookupCompactInfo()?.m_Items) ?? EmptyItems;
public List<Item> LookupItems() => (this is Container container ? container._items : LookupCompactInfo()?.m_Items) ?? EmptyItems;
public List<Item> AcquireItems()
{
if (this is Container cont)
{
return cont.m_Items ??= new List<Item>();
return cont._items ??= new List<Item>();
}
var info = AcquireCompactInfo();
@ -2714,7 +2714,7 @@ public partial class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropert
if (this is Container)
{
(this as Container).m_Items = items;
(this as Container)._items = items;
}
else
{
@ -2875,7 +2875,7 @@ public partial class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropert
if (this is Container cont)
{
cont.m_Items = items;
cont._items = items;
}
else
{
@ -3004,7 +3004,7 @@ public partial class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropert
if (this is Container cont)
{
cont.m_Items = items;
cont._items = items;
}
else
{

View file

@ -1422,9 +1422,9 @@ public static partial class Utility
public static bool IsNullOrWhiteSpace(this ReadOnlySpan<char> span) => span.IsEmpty || span.IsWhiteSpace();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool InTypeList<T>(this T obj, Type[] types) => obj.GetType().InTypeList(types);
public static bool InTypeList<T>(this T obj, ReadOnlySpan<Type> types) => obj.GetType().InTypeList(types);
public static bool InTypeList(this Type t, Type[] types)
public static bool InTypeList(this Type t, ReadOnlySpan<Type> types)
{
for (var i = 0; i < types.Length; ++i)
{