ModernUO/Projects/UOContent/Multis/Houses/PreviewHouse.cs
Kamron Batman ee2cd1d18d
feat: Adds new decay system and SkipSerialization (#2311)
## 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
2026-01-08 11:43:51 -08:00

113 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Items;
namespace Server.Multis;
[SerializationGenerator(0, false)]
public partial class PreviewHouse : BaseMulti
{
[SerializableField(0)]
private List<Item> _previewComponents;
private Timer _timer;
public PreviewHouse(int multiID) : base(multiID)
{
var mcl = Components;
for (var i = 1; i < mcl.List.Length; ++i)
{
var entry = mcl.List[i];
if (entry.Flags == 0)
{
Item item = new Static(entry.ItemId);
item.MoveToWorld(new Point3D(X + entry.OffsetX, Y + entry.OffsetY, Z + entry.OffsetZ), Map);
_previewComponents ??= [];
_previewComponents.Add(item);
}
}
_timer = new DecayTimer(this);
_timer.Start();
}
public override bool SkipSerialization => true;
public override void OnLocationChange(Point3D oldLocation)
{
base.OnLocationChange(oldLocation);
if (_previewComponents == null)
{
return;
}
var xOffset = X - oldLocation.X;
var yOffset = Y - oldLocation.Y;
var zOffset = Z - oldLocation.Z;
for (var i = 0; i < _previewComponents.Count; ++i)
{
var item = _previewComponents[i];
item.MoveToWorld(new Point3D(item.X + xOffset, item.Y + yOffset, item.Z + zOffset), Map);
}
}
public override void OnMapChange()
{
base.OnMapChange();
if (_previewComponents == null)
{
return;
}
for (var i = 0; i < _previewComponents.Count; ++i)
{
_previewComponents[i].Map = Map;
}
}
public override void OnDelete()
{
base.OnDelete();
if (_previewComponents == null)
{
return;
}
for (var i = 0; i < _previewComponents.Count; ++i)
{
_previewComponents[i]?.Delete();
}
}
public override void OnAfterDelete()
{
_timer?.Stop();
_timer = null;
base.OnAfterDelete();
}
private class DecayTimer : Timer
{
private readonly Item _item;
public DecayTimer(Item item) : base(TimeSpan.FromSeconds(20.0))
{
_item = item;
}
protected override void OnTick()
{
_item.Delete();
}
}
}