From fe7e5fbe37bd04d1400350478236a7a5be8c68c9 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 13 Sep 2026 16:22:54 +0200 Subject: [PATCH] Delete the parked CultCache SoA table Removes CultSoaTable, CultSoaColumn, CultCacheSoaStore, CultCacheSoaTypeTable, CultCacheSoaMember, CultCache.Soa, the _soa field and its four hooks, and the two tests that exercised them (with their TransformEntry and MeshManagedPlayer fixtures). The last working form is at parked/cultcache-soa. Co-Authored-By: Claude Opus 5 --- src/GameCult.Caching/CultCache.cs | 13 - src/GameCult.Caching/CultManagedDocument.cs | 283 ------------------ .../BackingStoreTests.cs | 55 ---- .../CultMeshStreamingTests.cs | 58 ---- 4 files changed, 409 deletions(-) diff --git a/src/GameCult.Caching/CultCache.cs b/src/GameCult.Caching/CultCache.cs index cf24c47a..47c2beea 100644 --- a/src/GameCult.Caching/CultCache.cs +++ b/src/GameCult.Caching/CultCache.cs @@ -1278,7 +1278,6 @@ namespace GameCult.Caching private readonly ConcurrentDictionary _globalKeys = new(); private readonly ConditionalWeakTable _documentHandles = new(); private readonly Subject _changes = new(); - private readonly CultCacheSoaStore _soa = new(); private readonly SemaphoreSlim _transactionGate = new(1, 1); private readonly AsyncLocal _ambientTransaction = new(); private readonly object _stateGate = new(); @@ -1413,14 +1412,6 @@ namespace GameCult.Caching return Watch().Where(change => change.Key.Equals(key)); } - /// - /// Gets the cache-owned structure-of-arrays table for a document type. - /// - public CultSoaTable Soa() where T : class - { - return _soa.Snapshot(); - } - /// /// Attaches a backing store to this cache. /// @@ -1967,7 +1958,6 @@ namespace GameCult.Caching { RemoveIndexes(existing); _documentHandles.Remove(existing.Document); - _soa.Remove(existing); } if (pair.Value == null) @@ -1984,7 +1974,6 @@ namespace GameCult.Caching _entries[pair.Key] = stored; _documentHandles.Remove(stored.Document); _documentHandles.Add(stored.Document, new DocumentHandleBox(stored.Key)); - _soa.Upsert(stored); AddIndexes(stored); changes.Add((stored, existing?.Document, false)); } @@ -2091,7 +2080,6 @@ namespace GameCult.Caching _entries[stored.Key.Value] = stored; _documentHandles.Remove(stored.Document); _documentHandles.Add(stored.Document, new DocumentHandleBox(stored.Key)); - _soa.Upsert(stored); AddIndexes(stored); foreach (var store in _backingStores) @@ -2130,7 +2118,6 @@ namespace GameCult.Caching RemoveIndexes(existing); _documentHandles.Remove(existing.Document); - _soa.Remove(existing); foreach (var store in _backingStores) { diff --git a/src/GameCult.Caching/CultManagedDocument.cs b/src/GameCult.Caching/CultManagedDocument.cs index 100cb628..96f021bd 100644 --- a/src/GameCult.Caching/CultManagedDocument.cs +++ b/src/GameCult.Caching/CultManagedDocument.cs @@ -129,287 +129,4 @@ namespace GameCult.Caching return _commit(value); } } - - /// - /// CPU-local structure-of-arrays table owned by CultCache for one document type. - /// - public sealed class CultSoaTable where TDocument : class - { - private readonly Dictionary _columns; - - internal CultSoaTable( - CultRecordKey[] keys, - TDocument[] documents, - Dictionary columns) - { - Keys = keys; - Documents = documents; - _columns = columns; - } - - /// - /// Gets record keys in row order. - /// - public IReadOnlyList Keys { get; } - - /// - /// Gets POCO presentations in row order. - /// - public IReadOnlyList Documents { get; } - - /// - /// Gets the row count. - /// - public int Count => Keys.Count; - - /// - /// Gets a contiguous unmanaged field/property column. - /// - public CultSoaColumn Column(string name) where TValue : unmanaged - { - if (!_columns.TryGetValue(name, out var column)) - { - throw new KeyNotFoundException($"SoA table for {typeof(TDocument).Name} has no '{name}' column."); - } - - if (column is not TValue[] values) - { - throw new InvalidOperationException( - $"SoA table column '{name}' stores {column.GetType().GetElementType()?.FullName}, not {typeof(TValue).FullName}."); - } - - return new CultSoaColumn(name, values); - } - } - - /// - /// Contiguous typed SoA column. - /// - public readonly struct CultSoaColumn where TValue : unmanaged - { - internal CultSoaColumn(string name, TValue[] values) - { - Name = name; - Values = values; - } - - /// - /// Gets the column name. - /// - public string Name { get; } - - /// - /// Gets the backing array. - /// - public TValue[] Values { get; } - - /// - /// Gets the backing array as a span. - /// - public Span Span => Values.AsSpan(); - } - - internal sealed class CultCacheSoaStore - { - private readonly Dictionary _tables = new(); - - public void Upsert(CultStoredDocument stored) - { - Table(stored.Descriptor.DocumentType).Upsert(stored.Key, stored.Document); - } - - public void Remove(CultStoredDocument stored) - { - if (_tables.TryGetValue(stored.Descriptor.DocumentType, out var table)) - { - table.Remove(stored.Key); - } - } - - public CultSoaTable Snapshot() where TDocument : class - { - return Table(typeof(TDocument)).Snapshot(); - } - - private CultCacheSoaTypeTable Table(Type documentType) - { - if (!_tables.TryGetValue(documentType, out var table)) - { - table = new CultCacheSoaTypeTable(documentType); - _tables[documentType] = table; - } - - return table; - } - } - - internal sealed class CultCacheSoaTypeTable - { - private readonly Type _documentType; - private readonly List _keys = new(); - private readonly List _documents = new(); - private readonly Dictionary _members; - private readonly Dictionary _columns; - private readonly Dictionary _rowsByKey = new(StringComparer.Ordinal); - - public CultCacheSoaTypeTable(Type documentType) - { - _documentType = documentType; - _members = CultCacheSoaMember.Discover(documentType) - .ToDictionary(member => member.Name, StringComparer.Ordinal); - _columns = _members.Values.ToDictionary( - member => member.Name, - member => Array.CreateInstance(member.ValueType, 0) as object, - StringComparer.Ordinal); - } - - public void Upsert(CultRecordKey key, object document) - { - if (_rowsByKey.TryGetValue(key.Value, out var row)) - { - _documents[row] = document; - } - else - { - row = _keys.Count; - _rowsByKey[key.Value] = row; - _keys.Add(key); - _documents.Add(document); - EnsureColumnCapacity(_keys.Count); - } - - WriteRow(row, document); - } - - public void Remove(CultRecordKey key) - { - if (!_rowsByKey.TryGetValue(key.Value, out var row)) - { - return; - } - - var lastRow = _keys.Count - 1; - if (row != lastRow) - { - _keys[row] = _keys[lastRow]; - _documents[row] = _documents[lastRow]; - _rowsByKey[_keys[row].Value] = row; - CopyRow(lastRow, row); - } - - _rowsByKey.Remove(key.Value); - _keys.RemoveAt(lastRow); - _documents.RemoveAt(lastRow); - } - - public CultSoaTable Snapshot() where TDocument : class - { - if (!typeof(TDocument).IsAssignableFrom(_documentType)) - { - throw new InvalidOperationException($"{_documentType.FullName} is not assignable to {typeof(TDocument).FullName}."); - } - - var count = _keys.Count; - var columns = new Dictionary(StringComparer.Ordinal); - foreach (var pair in _columns) - { - var member = _members[pair.Key]; - var source = (Array)pair.Value; - var target = Array.CreateInstance(member.ValueType, count); - Array.Copy(source, target, count); - columns[pair.Key] = target; - } - - return new CultSoaTable( - _keys.ToArray(), - _documents.Cast().ToArray(), - columns); - } - - private void EnsureColumnCapacity(int count) - { - foreach (var member in _members.Values) - { - var column = (Array)_columns[member.Name]; - if (column.Length >= count) - { - continue; - } - - var nextLength = Math.Max(count, Math.Max(4, column.Length * 2)); - var next = Array.CreateInstance(member.ValueType, nextLength); - Array.Copy(column, next, column.Length); - _columns[member.Name] = next; - } - } - - private void WriteRow(int row, object document) - { - foreach (var member in _members.Values) - { - ((Array)_columns[member.Name]).SetValue(member.Get(document), row); - } - } - - private void CopyRow(int sourceRow, int targetRow) - { - foreach (var member in _members.Values) - { - var column = (Array)_columns[member.Name]; - column.SetValue(column.GetValue(sourceRow), targetRow); - } - } - } - - internal sealed class CultCacheSoaMember - { - private CultCacheSoaMember(string name, Type valueType, Func getter) - { - Name = name; - ValueType = valueType; - Get = getter; - } - - public string Name { get; } - public Type ValueType { get; } - public Func Get { get; } - - public static IEnumerable Discover(Type documentType) - { - const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public; - foreach (var field in documentType.GetFields(flags).OrderBy(field => field.MetadataToken)) - { - if (IsColumnType(field.FieldType)) - { - yield return new CultCacheSoaMember(field.Name, field.FieldType, field.GetValue); - } - } - - foreach (var property in documentType.GetProperties(flags).OrderBy(property => property.MetadataToken)) - { - if (property.GetMethod != null && - property.GetIndexParameters().Length == 0 && - IsColumnType(property.PropertyType)) - { - yield return new CultCacheSoaMember(property.Name, property.PropertyType, property.GetValue); - } - } - } - - private static bool IsColumnType(Type type) - { - return type.IsEnum || - type == typeof(byte) || - type == typeof(sbyte) || - type == typeof(short) || - type == typeof(ushort) || - type == typeof(int) || - type == typeof(uint) || - type == typeof(long) || - type == typeof(ulong) || - type == typeof(float) || - type == typeof(double) || - type == typeof(bool); - } - } } diff --git a/tests/GameCult.Caching.Tests/BackingStoreTests.cs b/tests/GameCult.Caching.Tests/BackingStoreTests.cs index 31b37b40..6624b9f2 100644 --- a/tests/GameCult.Caching.Tests/BackingStoreTests.cs +++ b/tests/GameCult.Caching.Tests/BackingStoreTests.cs @@ -225,44 +225,6 @@ namespace GameCult.Caching.Tests } } - [Test] - public async Task CultCacheManagedDocument_Keeps_Poco_Surface_And_Updates_Soa_Storage() - { - var cache = new CultCache(); - var aliceKey = new CultRecordKey("entity:alice"); - var managed = cache.Document(aliceKey); - TransformEntry? observed = null; - using var subscription = managed.Watch().Subscribe(value => observed = value); - - await managed.ReplaceAsync(new TransformEntry - { - Name = "alice", - PositionX = 1.5f, - PositionY = 2.5f, - Health = 90 - }); - await cache.UpsertAsync(new TransformEntry - { - Name = "bob", - PositionX = 3.5f, - PositionY = 4.5f, - Health = 80 - }, new CultRecordHandle(new CultRecordKey("entity:bob"))); - - managed.Value!.Health = 70; - await managed.CommitAsync(); - - var table = cache.Soa(); - - Assert.That(table.Count, Is.EqualTo(2)); - Assert.That(table.Keys.Select(key => key.Value).ToArray(), Is.EqualTo(new[] { "entity:alice", "entity:bob" })); - Assert.That(table.Column(nameof(TransformEntry.PositionX)).Span.ToArray(), Is.EqualTo(new[] { 1.5f, 3.5f })); - Assert.That(table.Column(nameof(TransformEntry.PositionY)).Span.ToArray(), Is.EqualTo(new[] { 2.5f, 4.5f })); - Assert.That(table.Column(nameof(TransformEntry.Health)).Span.ToArray(), Is.EqualTo(new[] { 70, 80 })); - Assert.That(observed, Is.Not.Null); - Assert.That(observed!.Health, Is.EqualTo(70)); - } - [Test] public async Task DirectoryMessagePackBackingStore_Writes_Record_Pages_Without_Rewriting_Cold_Records() { @@ -1645,23 +1607,6 @@ namespace GameCult.Caching.Tests public CultRecordRef Parent = new(new CultRecordKey("parent")); } - [CultDocument("tests.transform_entry", "tests.transform_entry.v1")] - internal sealed class TransformEntry - { - [Key(0)] - [CultName] - public string Name = string.Empty; - - [Key(1)] - public float PositionX; - - [Key(2)] - public float PositionY; - - [Key(3)] - public int Health; - } - [CultDocument("tests.schema_stamped_entry", "tests.schema_stamped_entry.v1")] internal sealed class SchemaStampedTestEntry { diff --git a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs index 8ebccf57..87f1a6b5 100644 --- a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs +++ b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs @@ -3521,51 +3521,6 @@ public sealed class CultMeshStreamingTests diagnostic.Columns.Should().NotBeSameAs(descriptor.Columns); } - [Test] - public async Task ManagedDocument_Commits_Through_MeshDatabase_And_Watches_Networked_Updates() - { - var filePath = Path.Combine(Path.GetTempPath(), $"cultmesh-managed-{Guid.NewGuid():N}.ccmp"); - - try - { - using var node = await CultMesh.CreateNodeAsync( - filePath, - new CultMeshNodeOptions { StartServer = false }); - var key = new CultRecordKey("player:alice"); - var document = node.Database.Document(key); - MeshManagedPlayer observed = null!; - using var subscription = document.Watch().Subscribe(value => observed = value); - - await document.ReplaceAsync(new MeshManagedPlayer - { - Name = "alice", - PositionX = 4, - Health = 100 - }); - await node.Database.PutAsync(key, new MeshManagedPlayer - { - Name = "alice", - PositionX = 8, - Health = 75 - }); - - document.Value.Should().NotBeNull(); - document.Value!.Health.Should().Be(75); - observed.Should().NotBeNull(); - observed!.PositionX.Should().Be(8); - node.Cache.Soa().Column(nameof(MeshManagedPlayer.Health)).Span.ToArray() - .Should() - .Equal(75); - } - finally - { - if (File.Exists(filePath)) - { - File.Delete(filePath); - } - } - } - [Test] public void NegotiatesGpuTextureStreamsWithoutForcingCopies() { @@ -3800,19 +3755,6 @@ public sealed class CultMeshStreamingTests } } - [CultDocument("tests.mesh_managed_player", "tests.mesh_managed_player.v1")] - private sealed class MeshManagedPlayer - { - [MessagePack.Key(0)] - public string Name = string.Empty; - - [MessagePack.Key(1)] - public float PositionX; - - [MessagePack.Key(2)] - public int Health; - } - [CultDocument("tests.mesh_indexed_player", "tests.mesh_indexed_player.v1")] [MessagePackObject(AllowPrivate = true)] internal sealed class MeshIndexedPlayer