## 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
190 lines
4.5 KiB
C#
190 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using ModernUO.Serialization;
|
|
using Server.Mobiles;
|
|
|
|
namespace Server.Items;
|
|
|
|
public enum CampfireStatus
|
|
{
|
|
Burning,
|
|
Extinguishing,
|
|
Off
|
|
}
|
|
|
|
[SerializationGenerator(0, false)]
|
|
public partial class Campfire : Item
|
|
{
|
|
public const int SecureRange = 7;
|
|
|
|
private static readonly Dictionary<Mobile, CampfireEntry> _table = [];
|
|
|
|
private readonly List<CampfireEntry> _entries;
|
|
|
|
private TimerExecutionToken _timerToken;
|
|
|
|
public Campfire() : base(0xDE3)
|
|
{
|
|
Movable = false;
|
|
Light = LightType.Circle300;
|
|
|
|
_entries = [];
|
|
Timer.StartTimer(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0), OnTick, out _timerToken);
|
|
}
|
|
|
|
public override bool SkipSerialization => true;
|
|
|
|
[CommandProperty(AccessLevel.GameMaster)]
|
|
public CampfireStatus Status
|
|
{
|
|
get
|
|
{
|
|
return ItemID switch
|
|
{
|
|
0xDE3 => CampfireStatus.Burning,
|
|
0xDE9 => CampfireStatus.Extinguishing,
|
|
_ => CampfireStatus.Off
|
|
};
|
|
}
|
|
set
|
|
{
|
|
if (Status == value)
|
|
{
|
|
return;
|
|
}
|
|
|
|
switch (value)
|
|
{
|
|
case CampfireStatus.Burning:
|
|
{
|
|
ItemID = 0xDE3;
|
|
Light = LightType.Circle300;
|
|
break;
|
|
}
|
|
|
|
case CampfireStatus.Extinguishing:
|
|
{
|
|
ItemID = 0xDE9;
|
|
Light = LightType.Circle150;
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
ItemID = 0xDEA;
|
|
Light = LightType.ArchedWindowEast;
|
|
ClearEntries();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static CampfireEntry GetEntry(Mobile player) => _table.GetValueOrDefault(player);
|
|
|
|
public static void RemoveEntry(CampfireEntry entry)
|
|
{
|
|
_table.Remove(entry.Player);
|
|
entry.Fire._entries.Remove(entry);
|
|
}
|
|
|
|
private void OnTick()
|
|
{
|
|
var now = Core.Now;
|
|
var age = now - Created;
|
|
|
|
if (age >= TimeSpan.FromSeconds(100.0))
|
|
{
|
|
Delete();
|
|
}
|
|
else if (age >= TimeSpan.FromSeconds(90.0))
|
|
{
|
|
Status = CampfireStatus.Off;
|
|
}
|
|
else if (age >= TimeSpan.FromSeconds(60.0))
|
|
{
|
|
Status = CampfireStatus.Extinguishing;
|
|
}
|
|
|
|
if (Status == CampfireStatus.Off || Deleted)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (var i = _entries.Count - 1; i >= 0; i--)
|
|
{
|
|
var entry = _entries[i];
|
|
|
|
if (!entry.Valid || entry.Player.NetState == null)
|
|
{
|
|
RemoveEntry(entry);
|
|
}
|
|
else if (!entry.Safe && now - entry.Start >= TimeSpan.FromSeconds(30.0))
|
|
{
|
|
entry.Safe = true;
|
|
entry.Player.SendLocalizedMessage(500621); // The camp is now secure.
|
|
}
|
|
}
|
|
|
|
foreach (var state in GetClientsInRange(SecureRange))
|
|
{
|
|
if (state.Mobile is PlayerMobile pm && GetEntry(pm) == null)
|
|
{
|
|
var entry = new CampfireEntry(pm, this);
|
|
|
|
_table[pm] = entry;
|
|
_entries.Add(entry);
|
|
|
|
pm.SendLocalizedMessage(500620); // You feel it would take a few moments to secure your camp.
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ClearEntries()
|
|
{
|
|
if (_entries == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var entry in _entries)
|
|
{
|
|
_table.Remove(entry.Player);
|
|
}
|
|
|
|
_entries.Clear();
|
|
_entries.TrimExcess();
|
|
}
|
|
|
|
public override void OnAfterDelete()
|
|
{
|
|
_timerToken.Cancel();
|
|
ClearEntries();
|
|
}
|
|
}
|
|
|
|
public class CampfireEntry
|
|
{
|
|
private bool _safe;
|
|
|
|
public CampfireEntry(PlayerMobile player, Campfire fire)
|
|
{
|
|
Player = player;
|
|
Fire = fire;
|
|
Start = Core.Now;
|
|
_safe = false;
|
|
}
|
|
|
|
public PlayerMobile Player { get; }
|
|
public Campfire Fire { get; }
|
|
public DateTime Start { get; }
|
|
|
|
public bool Valid => !Fire.Deleted && Fire.Status != CampfireStatus.Off && Player.Map == Fire.Map &&
|
|
Player.InRange(Fire, Campfire.SecureRange);
|
|
|
|
public bool Safe
|
|
{
|
|
get => Valid && _safe;
|
|
set => _safe = value;
|
|
}
|
|
}
|