Convert ObjectTable enumerator to struct

This commit is contained in:
Haselnussbomber 2025-11-13 18:43:49 +01:00
parent 6340afb692
commit 2b2f628096
No known key found for this signature in database
GPG key ID: BB905BB49E7295D1

View file

@ -12,8 +12,6 @@ using Dalamud.Utility;
using FFXIVClientStructs.Interop;
using Microsoft.Extensions.ObjectPool;
using CSGameObject = FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject;
using CSGameObjectManager = FFXIVClientStructs.FFXIV.Client.Game.Object.GameObjectManager;
@ -34,8 +32,6 @@ internal sealed partial class ObjectTable : IServiceType, IObjectTable
private readonly ClientState clientState;
private readonly CachedEntry[] cachedObjectTable;
private readonly Enumerator?[] frameworkThreadEnumerators = new Enumerator?[4];
[ServiceManager.ServiceConstructor]
private unsafe ObjectTable(ClientState clientState)
{
@ -47,9 +43,6 @@ internal sealed partial class ObjectTable : IServiceType, IObjectTable
this.cachedObjectTable = new CachedEntry[objectTableLength];
for (var i = 0; i < this.cachedObjectTable.Length; i++)
this.cachedObjectTable[i] = new(nativeObjectTable.GetPointer(i));
for (var i = 0; i < this.frameworkThreadEnumerators.Length; i++)
this.frameworkThreadEnumerators[i] = new(this, i);
}
/// <inheritdoc/>
@ -239,30 +232,14 @@ internal sealed partial class ObjectTable
public IEnumerator<IGameObject> GetEnumerator()
{
ThreadSafety.AssertMainThread();
// If we're on the framework thread, see if there's an already allocated enumerator available for use.
foreach (ref var x in this.frameworkThreadEnumerators.AsSpan())
{
if (x is not null)
{
var t = x;
x = null;
t.Reset();
return t;
}
}
// No reusable enumerator is available; allocate a new temporary one.
return new Enumerator(this, -1);
return new Enumerator(this);
}
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
private sealed class Enumerator(ObjectTable owner, int slotId) : IEnumerator<IGameObject>, IResettable
private struct Enumerator(ObjectTable owner) : IEnumerator<IGameObject>
{
private ObjectTable? owner = owner;
private int index = -1;
public IGameObject Current { get; private set; } = null!;
@ -274,7 +251,7 @@ internal sealed partial class ObjectTable
if (this.index == objectTableLength)
return false;
var cache = this.owner!.cachedObjectTable.AsSpan();
var cache = owner.cachedObjectTable.AsSpan();
for (this.index++; this.index < objectTableLength; this.index++)
{
if (cache[this.index].Update() is { } ao)
@ -291,17 +268,6 @@ internal sealed partial class ObjectTable
public void Dispose()
{
if (this.owner is not { } o)
return;
if (slotId != -1)
o.frameworkThreadEnumerators[slotId] = this;
}
public bool TryReset()
{
this.Reset();
return true;
}
}
}