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:
parent
146abad36e
commit
a4cabe2fa4
23 changed files with 655 additions and 390 deletions
|
|
@ -385,7 +385,14 @@ namespace Server.Factions
|
|||
|
||||
// Ordinarily, through normal faction removal, this will never find any sigils.
|
||||
// Only with a leave delay less than the ReturnPeriod or a Faction Kick/Ban, will this ever do anything
|
||||
mob.Backpack?.FindItemsByType<Sigil>().ForEach(sigil => sigil.ReturnHome());
|
||||
|
||||
if (mob.Backpack != null)
|
||||
{
|
||||
foreach (var sigil in mob.Backpack.EnumerateItemsByType<Sigil>())
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
}
|
||||
}
|
||||
|
||||
if (pl.RankIndex != -1)
|
||||
{
|
||||
|
|
@ -1039,35 +1046,36 @@ namespace Server.Factions
|
|||
|
||||
var killerState = PlayerState.Find(killer);
|
||||
var killerPack = killer?.Backpack;
|
||||
victim.Backpack?.FindItemsByType<Sigil>()
|
||||
.ForEach(
|
||||
sigil =>
|
||||
{
|
||||
if (killerState == null || killerPack == null)
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
return;
|
||||
}
|
||||
|
||||
if (killer?.GetDistanceToSqrt(victim) > 64)
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
killer.SendLocalizedMessage(1042230); // The sigil has gone back to its home location.
|
||||
}
|
||||
else if (Sigil.ExistsOn(killer))
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
// The sigil has gone back to its home location because you already have a sigil.
|
||||
killer?.SendLocalizedMessage(1010258);
|
||||
}
|
||||
else if (!killerPack.TryDropItem(killer, sigil, false))
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
// The sigil has gone home because your backpack is full.
|
||||
killer?.SendLocalizedMessage(1010259);
|
||||
}
|
||||
if (victim.Backpack != null)
|
||||
{
|
||||
foreach (var sigil in victim.Backpack.EnumerateItemsByType<Sigil>())
|
||||
{
|
||||
if (killerState == null || killerPack == null)
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
continue;
|
||||
}
|
||||
);
|
||||
|
||||
if (killer?.GetDistanceToSqrt(victim) > 64)
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
killer.SendLocalizedMessage(1042230); // The sigil has gone back to its home location.
|
||||
}
|
||||
else if (Sigil.ExistsOn(killer))
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
// The sigil has gone back to its home location because you already have a sigil.
|
||||
killer?.SendLocalizedMessage(1010258);
|
||||
}
|
||||
else if (!killerPack.TryDropItem(killer, sigil, false))
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
// The sigil has gone home because your backpack is full.
|
||||
killer?.SendLocalizedMessage(1010259);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (killerState == null)
|
||||
{
|
||||
|
|
@ -1227,7 +1235,13 @@ namespace Server.Factions
|
|||
|
||||
private static void EventSink_Logout(Mobile m)
|
||||
{
|
||||
m.Backpack?.FindItemsByType<Sigil>().ForEach(sigil => sigil.ReturnHome());
|
||||
if (m.Backpack != null)
|
||||
{
|
||||
foreach (var sigil in m.Backpack.EnumerateItemsByType<Sigil>())
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void EventSink_Login(Mobile m) => CheckLeaveTimer(m);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using ModernUO.Serialization;
|
||||
using Server.Factions.AI;
|
||||
using Server.Items;
|
||||
using Server.Mobiles;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue