fix: Optimizes FindItemsByType by removing allocations. (#1515)

### Summary
Container enumeration is in dire need of optimization. Thanks to @stefanomerotta for initiating this work with PR #1443. This PR handles a small part of what Stefan started. Also included are some bug fixes.

### Method Signatures

```cs
// Use with foreach without moving/deleting items
FindItemsByTypeEnumerator<T> FindItemsByType<T>(bool recurse = true, Predicate<T> predicate = null)

// Use with foreach when moving/deleting items
QueuedItemsEnumerator<T> EnumerateItemsByType<T>(bool recurse = true, Predicate<T> predicate = null)

// Use when iterating multiple times or queuing
PooledRefQueue<T> QueueItemsByType<T>(bool recurse = true, Predicate<T> predicate = null)

// Use when iterating multiples times or manipulating elements without traversing
PooledRefList<T> ListItemsByType<T>(bool recurse = true, Predicate<T> predicate = null)
```

* `FindItemsByType<T>` has changed from returning `List<T>` to `FindItemsByTypeEnumerator<T>` - This method is not safe to use in situations where an item may get consumed, deleted, or moved.

* `EnumerateItemsByType<T>` was added as a safe way to iterate and manipulate items.
  * **Note**: EnumerateItemsByType will _completely traverse the container_ before iteration starts because it uses `QueueItemsByType` under the hood.

* `QueueItemsByType<T>`  and `ListItemsByType<T>` was added to return a queue or list of items to iterate multiple times and manipulate the items. This isn't the most efficient since it uses a predicate and can result in 2 or 3 total iterations unnecessarily.

### Bug Fixes
- [X] Fishing had an error in the random check that may have caused slight bias.
This commit is contained in:
Kamron Batman 2023-09-28 19:36:45 -07:00 • committed by GitHub
parent 146abad36e
commit a4cabe2fa4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 655 additions and 390 deletions

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ModernUO.Serialization;
using Server.ContextMenus;
using Server.Engines.Craft;
@ -173,7 +172,20 @@ public partial class SalvageBag : Bag
private void SalvageIngots(Mobile from)
{
if (from.Backpack.FindItemsByType<BaseTool>().All(tool => tool.CraftSystem != DefBlacksmithy.CraftSystem))
var hasTool = false;
if (from.Backpack != null)
{
foreach (var tool in from.Backpack.FindItemsByType<BaseTool>())
{
if (tool.CraftSystem == DefBlacksmithy.CraftSystem)
{
hasTool = true;
break;
}
}
}
if (!hasTool)
{
from.SendLocalizedMessage(1079822); // You need a blacksmithing tool in order to salvage ingots.
return;
@ -190,11 +202,7 @@ public partial class SalvageBag : Bag
var salvaged = 0;
var notSalvaged = 0;
Container sBag = this;
var smeltables = sBag.FindItemsByType<Item>();
foreach (var item in smeltables)
foreach (var item in FindItemsByType())
{
if (item?.Deleted != false)
{
@ -220,10 +228,8 @@ public partial class SalvageBag : Bag
}
else
{
from.SendLocalizedMessage(
1079973,
$"{salvaged}\t{salvaged + notSalvaged}"
); // Salvaged: ~1_COUNT~/~2_NUM~ blacksmithed items
// Salvaged: ~1_COUNT~/~2_NUM~ blacksmithed items
from.SendLocalizedMessage(1079973, $"{salvaged}\t{salvaged + notSalvaged}");
}
}
@ -245,14 +251,8 @@ public partial class SalvageBag : Bag
var salvaged = 0;
var notSalvaged = 0;
Container sBag = this;
var scissorables = sBag.FindItemsByType<Item>();
for (var i = scissorables.Count - 1; i >= 0; --i)
foreach (var item in EnumerateItemsByType<Item>())
{
var item = scissorables[i];
if (item is not IScissorable scissorable)
{
continue;