diff --git a/LuaScript/SceneSharedValues.cs b/LuaScript/SceneSharedValues.cs index 60b1c4e..73cc03e 100644 --- a/LuaScript/SceneSharedValues.cs +++ b/LuaScript/SceneSharedValues.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Runtime.CompilerServices; using System.Text; namespace LuaScript @@ -29,71 +30,124 @@ namespace LuaScript public const int MaxNameBytes = 4095; public const int MaxTextBytes = 4095; public const int MaxEntries = 4096; + public const long MaxHistoryCostBytes = 16 * 1024 * 1024; - private static readonly ConcurrentDictionary s_scenes = new(StringComparer.Ordinal); + private const long VersionBaseCost = 64; - private readonly Dictionary _committed = new(StringComparer.Ordinal); - private readonly Dictionary _pending = new(StringComparer.Ordinal); + private readonly record struct SceneVersion(long Frame, int Layer, SceneValue Value); + + private static readonly ConditionalWeakTable> s_scopes = new(); + + private readonly Dictionary> _names = new(StringComparer.Ordinal); private readonly object _gate = new(); - private long _generation = long.MinValue; - private bool _exporting; + private long _historyCost; - public static SceneSharedValues ForScene(string sceneId) => - s_scenes.GetOrAdd(sceneId, static _ => new SceneSharedValues()); + public static SceneSharedValues ForScene(object scope, string sceneId) => + s_scopes.GetOrCreateValue(scope).GetOrAdd(sceneId, static _ => new SceneSharedValues()); - public SceneValue Read(long generation, bool exporting, string name) + public SceneValue Read(long generation, string name) { lock (_gate) { - Advance(generation, exporting); - return _committed.TryGetValue(name, out var value) ? value : SceneValue.Nil; + if (!_names.TryGetValue(name, out var versions)) + return SceneValue.Nil; + int index = LatestBefore(versions, generation); + return index >= 0 ? versions[index].Value : SceneValue.Nil; } } - public void Publish(long generation, bool exporting, Dictionary writes) + public void Publish(long generation, int layer, Dictionary writes) { lock (_gate) { - Advance(generation, exporting); foreach (var pair in writes) - { - if (pair.Value.Kind == SceneValueKind.Nil || - _pending.ContainsKey(pair.Key) || - _pending.Count < MaxEntries) - { - _pending[pair.Key] = pair.Value; - } - } + Upsert(generation, layer, pair.Key, pair.Value); + EnforceHistoryBudget(); } } - private void Advance(long generation, bool exporting) + private void Upsert(long generation, int layer, string name, SceneValue value) { - if (exporting && (!_exporting || generation < _generation)) + if (!_names.TryGetValue(name, out var versions)) { - _committed.Clear(); - _pending.Clear(); - _generation = generation; + if (_names.Count >= MaxEntries) + return; + if (value == SceneValue.Nil) + return; + _names[name] = [new SceneVersion(generation, layer, value)]; + return; } - else if (generation != _generation) + + int index = LatestBefore(versions, generation + 1); + if (index >= 0 && versions[index].Frame == generation) { - foreach (var pair in _pending) + if (layer < versions[index].Layer) + return; + if (index < versions.Count - 1) + _historyCost += CostOf(value) - CostOf(versions[index].Value); + versions[index] = new SceneVersion(generation, layer, value); + return; + } + + var previous = index >= 0 ? versions[index].Value : SceneValue.Nil; + if (value == previous) + return; + + versions.Insert(index + 1, new SceneVersion(generation, layer, value)); + if (index + 1 < versions.Count - 1) + _historyCost += CostOf(value); + else if (versions.Count > 1) + _historyCost += CostOf(versions[^2].Value); + } + + private void EnforceHistoryBudget() + { + while (_historyCost > MaxHistoryCostBytes) + { + List? oldest = null; + string oldestName = string.Empty; + foreach (var pair in _names) { - if (pair.Value.Kind == SceneValueKind.Nil) - _committed.Remove(pair.Key); - } - foreach (var pair in _pending) - { - if (pair.Value.Kind != SceneValueKind.Nil && - (_committed.ContainsKey(pair.Key) || _committed.Count < MaxEntries)) + var versions = pair.Value; + if (versions.Count < 2) + continue; + if (oldest is null || + versions[0].Frame < oldest[0].Frame || + (versions[0].Frame == oldest[0].Frame && string.CompareOrdinal(pair.Key, oldestName) < 0)) { - _committed[pair.Key] = pair.Value; + oldest = versions; + oldestName = pair.Key; } } - _pending.Clear(); - _generation = generation; + if (oldest is null) + return; + _historyCost -= CostOf(oldest[0].Value); + oldest.RemoveAt(0); } - _exporting = exporting; + } + + private static long CostOf(SceneValue value) => + VersionBaseCost + (value.Text is null ? 0 : value.Text.Length * 2L); + + private static int LatestBefore(List versions, long generation) + { + int low = 0; + int high = versions.Count - 1; + int result = -1; + while (low <= high) + { + int mid = (low + high) >> 1; + if (versions[mid].Frame < generation) + { + result = mid; + low = mid + 1; + } + else + { + high = mid - 1; + } + } + return result; } public static string NormalizeName(string name) => TruncateUtf8(name, MaxNameBytes);