## Summary - Replaces scan-based decay checking during world saves with an event-driven timer wheel scheduler - Removes virtual property checks from serialization hot path, achieving 6-8x faster world saves ## Changes ### DecayScheduler (new): - Timer wheel with 12 HashSet buckets (5-min intervals) + PriorityQueue for active processing - O(1) register/unregister vs O(n) scan of all items - Auto start/stop when items exist/empty - Configurable tick interval with jitter to prevent system synchronization ### Item.cs: - Added ScheduledDecayTime computed property - Added UpdateDecayRegistration() called from SetLastMoved(), property setters, AddItem()/RemoveItem() - Hooks in Visible, Movable, Spawner setters and Delete() ### World.cs: - Removed _decayQueue, EnqueueForDecay(), ProcessDecay() - ItemPersistence.Serialize() now tight loop without virtual calls ## Performance | Metric | Before | After | Improvement | |---------------|------------|-----------|-------------| | Items (230K) | 245K ticks | 34K ticks | 8x faster | | Mobiles (43K) | 56K ticks | 6K ticks | 9x faster | | Total | 302K ticks | 40K ticks | 7.5x faster | ## Configuration decay.maxItemsPerTick = 250 # Items processed per tick decay.tickInterval = 256ms # Base processing interval decay.bucketInterval = 5min # Timer wheel bucket size decay.jitterMaxMs = 25 # ±25ms tick jitter
94 lines
2.1 KiB
C#
94 lines
2.1 KiB
C#
using System;
|
|
using ModernUO.Serialization;
|
|
using Server.Collections;
|
|
using Server.Mobiles;
|
|
|
|
namespace Server.Items;
|
|
|
|
[SerializationGenerator(0)]
|
|
public partial class Acid : Item
|
|
{
|
|
private TimeSpan _duration;
|
|
private int _maxDamage;
|
|
private int _minDamage;
|
|
private TimerExecutionToken _timerToken;
|
|
private bool _drying;
|
|
|
|
[Constructible]
|
|
public Acid() : this(TimeSpan.FromSeconds(10.0), 2, 5)
|
|
{
|
|
}
|
|
|
|
[Constructible]
|
|
public Acid(TimeSpan duration, int minDamage, int maxDamage) : this(duration, TimeSpan.FromSeconds(1), minDamage, maxDamage)
|
|
{
|
|
}
|
|
|
|
[Constructible]
|
|
public Acid(TimeSpan duration, TimeSpan tickRate, int minDamage, int maxDamage) : base(0x122A)
|
|
{
|
|
Hue = 0x3F;
|
|
Movable = false;
|
|
|
|
_minDamage = minDamage;
|
|
_maxDamage = maxDamage;
|
|
_duration = duration;
|
|
|
|
Timer.StartTimer(TimeSpan.Zero, tickRate, OnTick, out _timerToken);
|
|
}
|
|
|
|
public override string DefaultName => "a pool of acid";
|
|
|
|
public override bool SkipSerialization => true;
|
|
|
|
public override void OnDelete()
|
|
{
|
|
_timerToken.Cancel();
|
|
}
|
|
|
|
private void OnTick()
|
|
{
|
|
var now = Core.Now;
|
|
var age = now - Created;
|
|
|
|
if (age > _duration)
|
|
{
|
|
Delete();
|
|
return;
|
|
}
|
|
|
|
// Dries half way through duration
|
|
if (!_drying && age > _duration - age)
|
|
{
|
|
_drying = true;
|
|
ItemID = 0x122B;
|
|
}
|
|
|
|
using var queue = PooledRefQueue<Mobile>.Create();
|
|
|
|
foreach (var m in GetMobilesAt())
|
|
{
|
|
if (m.AccessLevel == AccessLevel.Player &&
|
|
m.Alive && !m.IsDeadBondedPet && (m is not BaseCreature bc || bc.Controlled || bc.Summoned))
|
|
{
|
|
queue.Enqueue(m);
|
|
}
|
|
}
|
|
|
|
while (queue.Count > 0)
|
|
{
|
|
Damage(queue.Dequeue());
|
|
}
|
|
}
|
|
|
|
public override bool OnMoveOver(Mobile m)
|
|
{
|
|
Damage(m);
|
|
return true;
|
|
}
|
|
|
|
public void Damage(Mobile m)
|
|
{
|
|
m.Damage(Utility.RandomMinMax(_minDamage, _maxDamage));
|
|
}
|
|
}
|