perf: keep damage entries in an inline intrusive list (#2605)
## Summary `Mobile.DamageEntries` was a `List<DamageEntry>` allocated for every mobile, including the ~99% that never take damage. It is now an inline `ValueLinkList<DamageEntry>` (24 bytes in the `Mobile` object, no separate allocation) ordered least recent → most recent. - `DamageEntry` implements `IValueLinkListNode<DamageEntry>`. - `RegisterDamage` moves the entry to the tail in O(1) instead of `Remove` + `Add` on a list. - Expired entries are always a head prefix, so pruning walks from the head and stops at the first live entry. The `DamageEntries` getter prunes on access. - `DamageEntries` is exposed as `ref readonly`; enumerate with `foreach` or `.ByDescending()`. Mutation goes through `RegisterDamage` / `ClearDamageEntries`. - `BaseCreature.GetLootingRights` and `BaseCreature.ComputeBonusDamage` take `in ValueLinkList<DamageEntry>`; all callers compile unchanged. Files that `foreach` over `DamageEntries` need `using Server.Collections;` for the enumerator extension. - RunUO migration docs (`dev-docs/runuo-migration-docs/09`, `11`) and the `migrate-items-mobiles` skill document the change. Saves one object and 16 bytes per mobile (~8 MB and 500k gen2 objects on a 500k world). Second of three PRs from the lazy per-mobile collections design (first: #2604). Branched from `main`; the two diffs touch disjoint hunks of `Mobile.cs`. ## Breaking change - `Mobile.DamageEntries` is no longer a `List<DamageEntry>`. Indexing, `.Clear()`, `.Add()`, `.Remove()` no longer compile; use `foreach`, `.ByDescending()`, `.Count`, `ClearDamageEntries()`, and `RegisterDamage`. Calling a `ValueLinkList` mutator on the `ref readonly` property compiles but operates on a copy while still unlinking the real nodes; do not. - `BaseCreature.GetLootingRights` and `BaseCreature.ComputeBonusDamage` signatures changed to `(in ValueLinkList<DamageEntry>, …)`. Save format is untouched: damage entries are not serialized. ## Behavior Recency order, `allowSelf`, tie-breaking in `FindMostTotal`/`FindLeastTotal` (most recent wins), `Responsible` accounting, and loot-rights ordering are unchanged and covered by the new `DamageEntryTests` and `LootingRightsTests`. ## Testing - `dotnet build -c Release` clean. - New `DamageEntryTests` and `LootingRightsTests` plus full `Server.Tests` and `UOContent.Tests`.
This commit is contained in:
parent
708a354337
commit
e52d54b7da
9 changed files with 603 additions and 103 deletions
|
|
@ -18,6 +18,7 @@ using System.Net;
|
|||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using ModernUO.Serialization;
|
||||
using Server.Collections;
|
||||
using Server.Engines.Virtues;
|
||||
using Server.Gumps;
|
||||
using Server.Items;
|
||||
|
|
@ -1181,11 +1182,6 @@ public partial class ChampionSpawn : Item
|
|||
|
||||
foreach (var de in m.DamageEntries)
|
||||
{
|
||||
if (de.HasExpired)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var damager = de.Damager;
|
||||
var master = damager.GetDamageMaster(m);
|
||||
|
||||
|
|
|
|||
|
|
@ -3123,14 +3123,12 @@ namespace Server.Mobiles
|
|||
return base.OnBeforeDeath();
|
||||
}
|
||||
|
||||
public int ComputeBonusDamage(List<DamageEntry> list, Mobile m)
|
||||
public int ComputeBonusDamage(in ValueLinkList<DamageEntry> list, Mobile m)
|
||||
{
|
||||
var bonus = 0;
|
||||
|
||||
for (var i = list.Count - 1; i >= 0; --i)
|
||||
foreach (var de in list.ByDescending())
|
||||
{
|
||||
var de = list[i];
|
||||
|
||||
if (de.Damager == m || de.Damager is not BaseCreature bc)
|
||||
{
|
||||
continue;
|
||||
|
|
@ -3167,26 +3165,15 @@ namespace Server.Mobiles
|
|||
Combatant is PlayerMobile ||
|
||||
Combatant is BaseCreature { Controlled: true } bc && bc.GetMaster() is PlayerMobile;
|
||||
|
||||
public static List<DamageStore> GetLootingRights(List<DamageEntry> damageEntries, int hitsMax)
|
||||
// Iterates most recent first, matching the previous reverse-indexed loop. The list is
|
||||
// already pruned of expired entries by the Mobile.DamageEntries getter.
|
||||
public static List<DamageStore> GetLootingRights(in ValueLinkList<DamageEntry> damageEntries, int hitsMax)
|
||||
{
|
||||
var rights = new List<DamageStore>();
|
||||
DamageStore firstDamager = null;
|
||||
|
||||
for (var i = damageEntries.Count - 1; i >= 0; --i)
|
||||
foreach (var de in damageEntries.ByDescending())
|
||||
{
|
||||
if (i >= damageEntries.Count)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var de = damageEntries[i];
|
||||
|
||||
if (de.HasExpired)
|
||||
{
|
||||
damageEntries.RemoveAt(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
var damage = de.DamageGiven;
|
||||
|
||||
var respList = de.Responsible;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ModernUO.Serialization;
|
||||
using Server.Collections;
|
||||
using Server.Engines.CannedEvil;
|
||||
using Server.Engines.Virtues;
|
||||
using Server.Items;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue