fix: Cleans up FindItems and removes allocations (#1516)
This commit is contained in:
parent
a4cabe2fa4
commit
d77dac9516
17 changed files with 105 additions and 127 deletions
|
|
@ -23,8 +23,8 @@ namespace Server.Items;
|
|||
public partial class Container
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public FindItemsByTypeEnumerator<Item> FindItemsByType(bool recurse = true, Predicate<Item> predicate = null)
|
||||
=> FindItemsByType<Item>(recurse, predicate);
|
||||
public FindItemsByTypeEnumerator<Item> FindItems(bool recurse = true, Predicate<Item> predicate = null)
|
||||
=> FindItemsByType(recurse, predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Performs a breadth-first search through all the <see cref="Item" />s and
|
||||
|
|
@ -61,8 +61,8 @@ public partial class Container
|
|||
where T : Item => new(this, recurse, predicate);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public QueuedItemsEnumerator<Item> EnumerateItemsByType(bool recurse = true, Predicate<Item> predicate = null)
|
||||
=> EnumerateItemsByType<Item>(recurse, predicate);
|
||||
public QueuedItemsEnumerator<Item> EnumerateItems(bool recurse = true, Predicate<Item> predicate = null)
|
||||
=> EnumerateItemsByType(recurse, predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Safely enumerates items using a breadth-first search through all the <see cref="Item" />s and
|
||||
|
|
@ -102,8 +102,8 @@ public partial class Container
|
|||
where T : Item => new(QueueItemsByType(recurse, predicate));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public PooledRefQueue<Item> QueueItemsByType(bool recurse = true, Predicate<Item> predicate = null) =>
|
||||
QueueItemsByType<Item>(recurse, predicate);
|
||||
public PooledRefQueue<Item> QueueItems(bool recurse = true, Predicate<Item> predicate = null) =>
|
||||
QueueItemsByType(recurse, predicate);
|
||||
|
||||
public PooledRefQueue<T> QueueItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) where T : Item
|
||||
{
|
||||
|
|
@ -117,8 +117,8 @@ public partial class Container
|
|||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public PooledRefList<Item> ListItemsByType(bool recurse = true, Predicate<Item> predicate = null) =>
|
||||
ListItemsByType<Item>(recurse, predicate);
|
||||
public PooledRefList<Item> ListItems(bool recurse = true, Predicate<Item> predicate = null) =>
|
||||
ListItemsByType(recurse, predicate);
|
||||
|
||||
public PooledRefList<T> ListItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) where T : Item
|
||||
{
|
||||
|
|
|
|||
|
|
@ -273,21 +273,6 @@ public partial class Container : Item
|
|||
return true;
|
||||
}
|
||||
|
||||
private static bool InTypeList(Item item, Type[] types)
|
||||
{
|
||||
var t = item.GetType();
|
||||
|
||||
for (var i = 0; i < types.Length; ++i)
|
||||
{
|
||||
if (types[i].IsAssignableFrom(t))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void SetSaveFlag(ref SaveFlag flags, SaveFlag toSet, bool setIf)
|
||||
{
|
||||
if (setIf)
|
||||
|
|
@ -816,7 +801,7 @@ public partial class Container : Item
|
|||
}
|
||||
|
||||
using var typedItems = PooledRefList<Item>.Create();
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (type.IsInstanceOfType(item))
|
||||
{
|
||||
|
|
@ -936,7 +921,7 @@ public partial class Container : Item
|
|||
{
|
||||
var type = types[i];
|
||||
using var typedItems = PooledRefList<Item>.Create();
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (type.IsInstanceOfType(item))
|
||||
{
|
||||
|
|
@ -1270,7 +1255,7 @@ public partial class Container : Item
|
|||
using var items = PooledRefQueue<Item>.Create();
|
||||
|
||||
// First pass, compute total
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (type.IsInstanceOfType(item))
|
||||
{
|
||||
|
|
@ -1380,7 +1365,7 @@ public partial class Container : Item
|
|||
var best = 0;
|
||||
|
||||
using var typedItems = PooledRefList<Item>.Create();
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (type.IsInstanceOfType(item))
|
||||
{
|
||||
|
|
@ -1565,7 +1550,7 @@ public partial class Container : Item
|
|||
public int GetAmount(Type type, bool recurse = true)
|
||||
{
|
||||
var total = 0;
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (type.IsInstanceOfType(item))
|
||||
{
|
||||
|
|
@ -1579,9 +1564,9 @@ public partial class Container : Item
|
|||
public int GetAmount(Type[] types, bool recurse = true)
|
||||
{
|
||||
var total = 0;
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (InTypeList(item, types))
|
||||
if (item.InTypeList(types))
|
||||
{
|
||||
total += item.Amount;
|
||||
}
|
||||
|
|
@ -1593,7 +1578,7 @@ public partial class Container : Item
|
|||
public List<Item> FindItemsByType(Type type, bool recurse = true)
|
||||
{
|
||||
var items = new List<Item>();
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (type.IsInstanceOfType(item))
|
||||
{
|
||||
|
|
@ -1607,9 +1592,9 @@ public partial class Container : Item
|
|||
public List<Item> FindItemsByType(Type[] types, bool recurse = true)
|
||||
{
|
||||
var items = new List<Item>();
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (InTypeList(item, types))
|
||||
if (item.InTypeList(types))
|
||||
{
|
||||
items.Add(item);
|
||||
}
|
||||
|
|
@ -1620,7 +1605,7 @@ public partial class Container : Item
|
|||
|
||||
public Item FindItemByType(Type type, bool recurse = true)
|
||||
{
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (type.IsInstanceOfType(item))
|
||||
{
|
||||
|
|
@ -1633,9 +1618,9 @@ public partial class Container : Item
|
|||
|
||||
public Item FindItemByType(Type[] types, bool recurse = true)
|
||||
{
|
||||
foreach (var item in FindItemsByType(recurse))
|
||||
foreach (var item in FindItems(recurse))
|
||||
{
|
||||
if (InTypeList(item, types))
|
||||
if (item.InTypeList(types))
|
||||
{
|
||||
return item;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -342,7 +342,7 @@ public static class TileData
|
|||
bin.Read(buffer);
|
||||
var terminator = buffer.IndexOfTerminator(1);
|
||||
var name = Encoding.ASCII.GetString(buffer[..(terminator < 0 ? buffer.Length : terminator)]);
|
||||
LandTable[i] = new LandData(Utility.Intern(name), flags);
|
||||
LandTable[i] = new LandData(name.Intern(), flags);
|
||||
}
|
||||
|
||||
for (var i = 0; i < itemLength; i++)
|
||||
|
|
@ -367,7 +367,7 @@ public static class TileData
|
|||
var terminator = buffer.IndexOfTerminator(1);
|
||||
var name = Encoding.ASCII.GetString(buffer[..(terminator < 0 ? buffer.Length : terminator)]);
|
||||
ItemTable[i] = new ItemData(
|
||||
Utility.Intern(name),
|
||||
name.Intern(),
|
||||
flags,
|
||||
weight,
|
||||
quality,
|
||||
|
|
|
|||
|
|
@ -1761,4 +1761,20 @@ public static class Utility
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsNullOrWhiteSpace(this ReadOnlySpan<char> span) =>
|
||||
span == default || span.IsEmpty || span.IsWhiteSpace();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InTypeList(this Item item, Type[] types) => item.GetType().InTypeList(types);
|
||||
|
||||
public static bool InTypeList(this Type t, Type[] types)
|
||||
{
|
||||
for (var i = 0; i < types.Length; ++i)
|
||||
{
|
||||
if (types[i].IsAssignableFrom(t))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue