< Summary

Information
Class: Lattice.ECS.Session.SessionFrameHistory
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionFrameHistory.cs
Line coverage
86%
Covered lines: 182
Uncovered lines: 28
Coverable lines: 210
Total lines: 469
Line coverage: 86.6%
Branch coverage
84%
Covered branches: 101
Total branches: 120
Branch coverage: 84.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionFrameHistory.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Lattice.ECS.Core;
 4
 5namespace Lattice.ECS.Session
 6{
 7    /// <summary>
 8    /// SessionRuntime 的历史帧、采样快照、物化缓存与帧池管理。
 9    /// 负责保留策略与缓存复用,不直接决定运行时公开语义。
 10    /// </summary>
 11    internal sealed class SessionFrameHistory
 12    {
 13        private readonly Dictionary<int, Frame> _historyByTick;
 14        private readonly Queue<int> _historyOrder;
 15        private readonly Dictionary<int, PackedFrameSnapshot> _historySnapshotsByTick;
 16        private readonly List<int> _historySnapshotTicks;
 17        private readonly Stack<Frame> _recycledFrames;
 18        private readonly Dictionary<int, Frame> _materializedHistoryByTick;
 19        private readonly List<int> _materializedHistoryOrder;
 20        private Frame? _historicalScratchFrame;
 11321        private int _highestHistoryTick = -1;
 22
 11323        public SessionFrameHistory()
 24        {
 11325            _historyByTick = new Dictionary<int, Frame>(SessionRuntimeDataDefaults.HistorySize);
 11326            _historyOrder = new Queue<int>(SessionRuntimeDataDefaults.HistorySize);
 11327            _historySnapshotsByTick = new Dictionary<int, PackedFrameSnapshot>(
 11328                SessionRuntimeDataDefaults.HistorySize / SessionRuntimeDataDefaults.HistorySnapshotInterval + 2);
 11329            _historySnapshotTicks = new List<int>(
 11330                SessionRuntimeDataDefaults.HistorySize / SessionRuntimeDataDefaults.HistorySnapshotInterval + 2);
 11331            _recycledFrames = new Stack<Frame>(SessionRuntimeDataDefaults.RecycledFrameLimit);
 11332            _materializedHistoryByTick = new Dictionary<int, Frame>(SessionRuntimeDataDefaults.MaterializedHistoryCacheS
 11333            _materializedHistoryOrder = new List<int>(SessionRuntimeDataDefaults.MaterializedHistoryCacheSize);
 11334        }
 35
 036        public int HighestHistoryTick => _highestHistoryTick;
 37
 38        public bool TryGetLiveFrame(int tick, out Frame? frame)
 39        {
 9540            return _historyByTick.TryGetValue(tick, out frame);
 41        }
 42
 43        public bool TryGetMaterializedFrame(int tick, out Frame? frame)
 44        {
 5245            if (_materializedHistoryByTick.TryGetValue(tick, out frame))
 46            {
 547                TouchMaterializedHistoryTick(tick);
 548                return true;
 49            }
 50
 4751            return false;
 52        }
 53
 54        public bool TryGetScratchFrame(int tick, out Frame? frame)
 55        {
 4756            if (_historicalScratchFrame != null && _historicalScratchFrame.Tick == tick)
 57            {
 058                frame = _historicalScratchFrame;
 059                return true;
 60            }
 61
 4762            frame = null;
 4763            return false;
 64        }
 65
 66        public bool IsTickWithinRetentionWindow(int tick)
 67        {
 2568            if (tick < 0 || _highestHistoryTick < 0)
 69            {
 170                return false;
 71            }
 72
 2473            if (tick > _highestHistoryTick)
 74            {
 175                return false;
 76            }
 77
 2378            int minTick = _highestHistoryTick - SessionRuntimeDataDefaults.HistorySize + 1;
 2379            return tick >= minTick;
 80        }
 81
 82        public bool TryFindClosestLiveFrame(int tick, out int baseTick, out Frame? frame)
 83        {
 2284            baseTick = -1;
 2285            frame = null;
 86
 57287            foreach ((int historicalTick, Frame historicalFrame) in _historyByTick)
 88            {
 26489                if (historicalTick <= tick && historicalTick > baseTick)
 90                {
 091                    baseTick = historicalTick;
 092                    frame = historicalFrame;
 93                }
 94            }
 95
 2296            return frame != null;
 97        }
 98
 99        public bool TryFindClosestMaterializedFrame(int tick, out int baseTick, out Frame? frame)
 100        {
 22101            baseTick = -1;
 22102            frame = null;
 103
 66104            for (int i = 0; i < _materializedHistoryOrder.Count; i++)
 105            {
 11106                int candidateTick = _materializedHistoryOrder[i];
 11107                if (candidateTick <= tick && candidateTick > baseTick)
 108                {
 7109                    baseTick = candidateTick;
 110                }
 111            }
 112
 22113            if (baseTick < 0 || !_materializedHistoryByTick.TryGetValue(baseTick, out frame))
 114            {
 15115                baseTick = -1;
 15116                frame = null;
 15117                return false;
 118            }
 119
 7120            TouchMaterializedHistoryTick(baseTick);
 7121            return true;
 122        }
 123
 124        public bool TryGetScratchAsBaseFrame(int tick, out int baseTick, out Frame? frame)
 125        {
 22126            if (_historicalScratchFrame != null && _historicalScratchFrame.Tick <= tick)
 127            {
 7128                baseTick = _historicalScratchFrame.Tick;
 7129                frame = _historicalScratchFrame;
 7130                return true;
 131            }
 132
 15133            baseTick = -1;
 15134            frame = null;
 15135            return false;
 136        }
 137
 138        public bool TryFindClosestSnapshotTick(int tick, out int snapshotTick)
 139        {
 15140            snapshotTick = -1;
 15141            if (_historySnapshotTicks.Count == 0)
 142            {
 0143                return false;
 144            }
 145
 15146            int index = _historySnapshotTicks.BinarySearch(tick);
 15147            if (index < 0)
 148            {
 15149                index = ~index - 1;
 150            }
 151
 15152            if ((uint)index >= (uint)_historySnapshotTicks.Count)
 153            {
 0154                return false;
 155            }
 156
 15157            snapshotTick = _historySnapshotTicks[index];
 15158            return snapshotTick >= 0;
 159        }
 160
 161        public PackedFrameSnapshot GetSnapshot(int tick)
 162        {
 15163            return _historySnapshotsByTick[tick];
 164        }
 165
 166        public void UpdateHistory(Frame frame, Action<Frame?> releaseDetachedFrame)
 167        {
 688168            ArgumentNullException.ThrowIfNull(frame);
 688169            ArgumentNullException.ThrowIfNull(releaseDetachedFrame);
 170
 688171            InvalidateMaterializedCache(releaseDetachedFrame);
 688172            RemoveHistorySnapshot(frame.Tick);
 173
 688174            if (_historyByTick.TryGetValue(frame.Tick, out Frame? existing))
 175            {
 26176                if (!ReferenceEquals(existing, frame))
 177                {
 26178                    _historyByTick[frame.Tick] = frame;
 26179                    releaseDetachedFrame(existing);
 180                }
 181
 26182                return;
 183            }
 184
 662185            AddHistoryFrame(frame, releaseDetachedFrame);
 662186        }
 187
 188        public void InvalidateMaterializedCache(Action<Frame?> releaseDetachedFrame)
 189        {
 691190            ArgumentNullException.ThrowIfNull(releaseDetachedFrame);
 191
 691192            Frame? scratchFrame = _historicalScratchFrame;
 691193            _historicalScratchFrame = null;
 194
 691195            if (_materializedHistoryByTick.Count > 0)
 196            {
 44197                for (int i = 0; i < _materializedHistoryOrder.Count; i++)
 198                {
 15199                    int tick = _materializedHistoryOrder[i];
 15200                    if (_materializedHistoryByTick.Remove(tick, out Frame? cachedFrame) &&
 15201                        !ReferenceEquals(cachedFrame, scratchFrame))
 202                    {
 8203                        releaseDetachedFrame(cachedFrame);
 204                    }
 205                }
 206
 7207                _materializedHistoryOrder.Clear();
 208            }
 209
 691210            releaseDetachedFrame(scratchFrame);
 691211        }
 212
 213        public void StoreMaterializedFrame(Frame frame, Action<Frame?> releaseDetachedFrame)
 214        {
 22215            ArgumentNullException.ThrowIfNull(frame);
 22216            ArgumentNullException.ThrowIfNull(releaseDetachedFrame);
 217
 22218            int tick = frame.Tick;
 219
 22220            if (_materializedHistoryByTick.TryGetValue(tick, out Frame? existing))
 221            {
 0222                if (!ReferenceEquals(existing, frame))
 223                {
 0224                    _materializedHistoryByTick[tick] = frame;
 0225                    releaseDetachedFrame(existing);
 226                }
 227
 0228                TouchMaterializedHistoryTick(tick);
 0229                return;
 230            }
 231
 22232            _materializedHistoryByTick[tick] = frame;
 22233            _materializedHistoryOrder.Add(tick);
 234
 22235            if (_materializedHistoryOrder.Count > SessionRuntimeDataDefaults.MaterializedHistoryCacheSize)
 236            {
 0237                int evictedTick = _materializedHistoryOrder[0];
 0238                _materializedHistoryOrder.RemoveAt(0);
 0239                if (_materializedHistoryByTick.Remove(evictedTick, out Frame? evictedFrame))
 240                {
 0241                    releaseDetachedFrame(evictedFrame);
 242                }
 243            }
 22244        }
 245
 246        public void SetScratchFrame(Frame frame, Action<Frame?> releaseDetachedFrame)
 247        {
 22248            ArgumentNullException.ThrowIfNull(frame);
 22249            ArgumentNullException.ThrowIfNull(releaseDetachedFrame);
 250
 22251            if (!ReferenceEquals(_historicalScratchFrame, frame))
 252            {
 22253                releaseDetachedFrame(_historicalScratchFrame);
 22254                _historicalScratchFrame = frame;
 255            }
 22256        }
 257
 258        public Frame RentFrameBuffer(int requiredEntityCapacity)
 259        {
 635260            while (_recycledFrames.Count > 0)
 261            {
 412262                Frame recycled = _recycledFrames.Pop();
 412263                if (recycled.EntityCapacity == requiredEntityCapacity)
 264                {
 412265                    return recycled;
 266                }
 267
 0268                recycled.Dispose();
 269            }
 270
 223271            return new Frame(requiredEntityCapacity);
 272        }
 273
 274        public void RecycleFrame(Frame? frame)
 275        {
 435276            if (frame == null)
 277            {
 0278                return;
 279            }
 280
 435281            if (_recycledFrames.Count < SessionRuntimeDataDefaults.RecycledFrameLimit)
 282            {
 435283                _recycledFrames.Push(frame);
 435284                return;
 285            }
 286
 0287            frame.Dispose();
 0288        }
 289
 290        public bool Owns(Frame frame)
 291        {
 949292            if (ReferenceEquals(frame, _historicalScratchFrame))
 293            {
 9294                return true;
 295            }
 296
 940297            if (_materializedHistoryByTick.TryGetValue(frame.Tick, out Frame? materializedFrame) &&
 940298                ReferenceEquals(frame, materializedFrame))
 299            {
 0300                return true;
 301            }
 302
 940303            if (_historyByTick.TryGetValue(frame.Tick, out Frame? historicalFrame) &&
 940304                ReferenceEquals(frame, historicalFrame))
 305            {
 506306                return true;
 307            }
 308
 434309            return false;
 310        }
 311
 312        public void CopyOwnedFramesTo(ISet<Frame> frames)
 313        {
 192314            ArgumentNullException.ThrowIfNull(frames);
 315
 192316            if (_historicalScratchFrame != null)
 317            {
 6318                frames.Add(_historicalScratchFrame);
 319            }
 320
 398321            foreach (Frame materializedFrame in _materializedHistoryByTick.Values)
 322            {
 7323                frames.Add(materializedFrame);
 324            }
 325
 994326            foreach (Frame historicalFrame in _historyByTick.Values)
 327            {
 305328                frames.Add(historicalFrame);
 329            }
 330
 430331            foreach (Frame recycledFrame in _recycledFrames)
 332            {
 23333                frames.Add(recycledFrame);
 334            }
 192335        }
 336
 337        public void Clear()
 338        {
 192339            _historicalScratchFrame = null;
 192340            _highestHistoryTick = -1;
 192341            _historyByTick.Clear();
 192342            _historyOrder.Clear();
 192343            _historySnapshotsByTick.Clear();
 192344            _historySnapshotTicks.Clear();
 192345            _materializedHistoryByTick.Clear();
 192346            _materializedHistoryOrder.Clear();
 192347            _recycledFrames.Clear();
 192348        }
 349
 350        private void AddHistoryFrame(Frame frame, Action<Frame?> releaseDetachedFrame)
 351        {
 662352            _historyByTick.Add(frame.Tick, frame);
 662353            _historyOrder.Enqueue(frame.Tick);
 662354            _highestHistoryTick = System.Math.Max(_highestHistoryTick, frame.Tick);
 355
 662356            TrimExpiredHistory(releaseDetachedFrame);
 662357            DemoteOldLiveHistory(releaseDetachedFrame);
 662358        }
 359
 360        private void TrimExpiredHistory(Action<Frame?> releaseDetachedFrame)
 361        {
 662362            int minTick = _highestHistoryTick - SessionRuntimeDataDefaults.HistorySize + 1;
 363
 662364            while (_historyOrder.Count > 0 && _historyOrder.Peek() < minTick)
 365            {
 0366                int expiredTick = _historyOrder.Dequeue();
 0367                if (_historyByTick.Remove(expiredTick, out Frame? expiredFrame))
 368                {
 0369                    RemoveHistorySnapshot(expiredTick);
 0370                    releaseDetachedFrame(expiredFrame);
 371                }
 372            }
 373
 662374            TrimExpiredSnapshots(minTick);
 662375        }
 376
 377        private void DemoteOldLiveHistory(Action<Frame?> releaseDetachedFrame)
 378        {
 1019379            while (_historyByTick.Count > SessionRuntimeDataDefaults.LiveHistorySize && _historyOrder.Count > 0)
 380            {
 357381                int demotedTick = _historyOrder.Dequeue();
 357382                if (!_historyByTick.Remove(demotedTick, out Frame? demotedFrame))
 383                {
 384                    continue;
 385                }
 386
 357387                CaptureHistorySnapshotIfNeeded(demotedTick, demotedFrame);
 357388                releaseDetachedFrame(demotedFrame);
 389            }
 662390        }
 391
 392        private void CaptureHistorySnapshotIfNeeded(int tick, Frame frame)
 393        {
 357394            if (tick != 0 && (tick % SessionRuntimeDataDefaults.HistorySnapshotInterval) != 0)
 395            {
 260396                return;
 397            }
 398
 97399            bool isNewSnapshot = !_historySnapshotsByTick.ContainsKey(tick);
 97400            _historySnapshotsByTick[tick] = frame.CapturePackedSnapshot(ComponentSerializationMode.Prediction);
 97401            if (isNewSnapshot)
 402            {
 97403                InsertHistorySnapshotTick(tick);
 404            }
 97405        }
 406
 407        private void RemoveHistorySnapshot(int tick)
 408        {
 688409            if (_historySnapshotsByTick.Remove(tick))
 410            {
 15411                RemoveHistorySnapshotTick(tick);
 412            }
 688413        }
 414
 415        private void TrimExpiredSnapshots(int minTick)
 416        {
 662417            if (_historySnapshotsByTick.Count == 0)
 418            {
 315419                _historySnapshotTicks.Clear();
 315420                return;
 421            }
 422
 347423            int firstWithinRangeIndex = _historySnapshotTicks.BinarySearch(minTick);
 347424            if (firstWithinRangeIndex < 0)
 425            {
 346426                firstWithinRangeIndex = ~firstWithinRangeIndex;
 427            }
 428
 347429            int anchorIndex = firstWithinRangeIndex - 1;
 694430            for (int i = anchorIndex - 1; i >= 0; i--)
 431            {
 0432                _historySnapshotsByTick.Remove(_historySnapshotTicks[i]);
 0433                _historySnapshotTicks.RemoveAt(i);
 434            }
 347435        }
 436
 437        private void TouchMaterializedHistoryTick(int tick)
 438        {
 12439            int index = _materializedHistoryOrder.IndexOf(tick);
 12440            if (index < 0 || index == _materializedHistoryOrder.Count - 1)
 441            {
 9442                return;
 443            }
 444
 3445            _materializedHistoryOrder.RemoveAt(index);
 3446            _materializedHistoryOrder.Add(tick);
 3447        }
 448
 449        private void InsertHistorySnapshotTick(int tick)
 450        {
 97451            int index = _historySnapshotTicks.BinarySearch(tick);
 97452            if (index >= 0)
 453            {
 0454                return;
 455            }
 456
 97457            _historySnapshotTicks.Insert(~index, tick);
 97458        }
 459
 460        private void RemoveHistorySnapshotTick(int tick)
 461        {
 15462            int index = _historySnapshotTicks.BinarySearch(tick);
 15463            if (index >= 0)
 464            {
 15465                _historySnapshotTicks.RemoveAt(index);
 466            }
 15467        }
 468    }
 469}

Methods/Properties

.ctor()
get_HighestHistoryTick()
TryGetLiveFrame(System.Int32,Lattice.ECS.Core.Frame&)
TryGetMaterializedFrame(System.Int32,Lattice.ECS.Core.Frame&)
TryGetScratchFrame(System.Int32,Lattice.ECS.Core.Frame&)
IsTickWithinRetentionWindow(System.Int32)
TryFindClosestLiveFrame(System.Int32,System.Int32&,Lattice.ECS.Core.Frame&)
TryFindClosestMaterializedFrame(System.Int32,System.Int32&,Lattice.ECS.Core.Frame&)
TryGetScratchAsBaseFrame(System.Int32,System.Int32&,Lattice.ECS.Core.Frame&)
TryFindClosestSnapshotTick(System.Int32,System.Int32&)
GetSnapshot(System.Int32)
UpdateHistory(Lattice.ECS.Core.Frame,System.Action`1<Lattice.ECS.Core.Frame>)
InvalidateMaterializedCache(System.Action`1<Lattice.ECS.Core.Frame>)
StoreMaterializedFrame(Lattice.ECS.Core.Frame,System.Action`1<Lattice.ECS.Core.Frame>)
SetScratchFrame(Lattice.ECS.Core.Frame,System.Action`1<Lattice.ECS.Core.Frame>)
RentFrameBuffer(System.Int32)
RecycleFrame(Lattice.ECS.Core.Frame)
Owns(Lattice.ECS.Core.Frame)
CopyOwnedFramesTo(System.Collections.Generic.ISet`1<Lattice.ECS.Core.Frame>)
Clear()
AddHistoryFrame(Lattice.ECS.Core.Frame,System.Action`1<Lattice.ECS.Core.Frame>)
TrimExpiredHistory(System.Action`1<Lattice.ECS.Core.Frame>)
DemoteOldLiveHistory(System.Action`1<Lattice.ECS.Core.Frame>)
CaptureHistorySnapshotIfNeeded(System.Int32,Lattice.ECS.Core.Frame)
RemoveHistorySnapshot(System.Int32)
TrimExpiredSnapshots(System.Int32)
TouchMaterializedHistoryTick(System.Int32)
InsertHistorySnapshotTick(System.Int32)
RemoveHistorySnapshotTick(System.Int32)