Remove property name index caching from binary serializer

Simplifies property name registration by removing per-accessor
index caching and related reset logic. Deletes
RegisterPropertyNameAndCache and CachedPropertyNameIndex,
switching to direct property name registration. Also comments
out the global metadata cache in TypeMetadataBase, signaling
a move away from global caching. This reduces complexity and
potential for stale cache issues during context reuse.
This commit is contained in:
Loretta 2026-01-26 11:37:31 +01:00
parent e73fd7ae4a
commit ff73901ba8
4 changed files with 3 additions and 38 deletions

View File

@ -148,9 +148,6 @@ public static partial class AcBinarySerializer
// Reset intern buffer position (no deallocation - buffer is reused!)
_internedStringBufferPos = 0;
// Reset cached property indices
ResetCachedPropertyIndices();
if (_propertyIndexBuffer != null && _propertyIndexBuffer.Length > PropertyIndexBufferMaxCache)
{
@ -165,13 +162,6 @@ public static partial class AcBinarySerializer
}
}
private static void ResetCachedPropertyIndices()
{
// Note: BinaryPropertyAccessor.CachedPropertyNameIndex is per-context,
// but metadata is cached globally. We reset it during Clear to avoid
// stale indices. The next serialization will re-populate them.
// This is a minor cost as it only happens on context reuse.
}
public void Dispose()
{
@ -288,24 +278,6 @@ public static partial class AcBinarySerializer
}
}
/// <summary>
/// Registers property name and caches the index in the accessor for future lookups.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RegisterPropertyNameAndCache(BinaryPropertyAccessor accessor)
{
_propertyNames ??= new Dictionary<string, int>(InitialPropertyNameCapacity, StringComparer.Ordinal);
_propertyNameList ??= new List<string>(InitialPropertyNameCapacity);
ref var index = ref CollectionsMarshal.GetValueRefOrAddDefault(_propertyNames, accessor.Name, out var exists);
if (!exists)
{
index = _propertyNameList.Count;
_propertyNameList.Add(accessor.Name);
}
accessor.CachedPropertyNameIndex = index;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetPropertyNameIndex(string name)
=> _propertyNames != null && _propertyNames.TryGetValue(name, out var index) ? index : -1;

View File

@ -403,8 +403,7 @@ public static partial class AcBinarySerializer
continue;
}
// Use caching registration to avoid dictionary lookup during serialization
context.RegisterPropertyNameAndCache(prop);
context.RegisterPropertyName(prop.Name);
if (TryResolveNestedMetadataType(prop.PropertyType, out var nestedType))
{

View File

@ -7,16 +7,10 @@ namespace AyCode.Core.Serializers.Binaries;
/// <summary>
/// Binary-specific property accessor.
/// Inherits typed getters from PropertyAccessorBase.
/// Adds Binary-specific properties: PropertyIndex, CachedPropertyNameIndex.
/// Adds Binary-specific property: PropertyIndex.
/// </summary>
public abstract class BinaryPropertyAccessorBase : PropertyAccessorBase
{
/// <summary>
/// Cached property name index for metadata mode. Set by context during registration.
/// -1 means not yet cached.
/// </summary>
internal int CachedPropertyNameIndex = -1;
/// <summary>
/// Deterministic property index based on alphabetical ordering of property names.
/// This is computed once during metadata creation and is consistent across all platforms.

View File

@ -22,7 +22,7 @@ public abstract class TypeMetadataBase
/// Value: TypeMetadataBase instance (BinarySerializeTypeMetadata, JsonTypeMetadata, etc.)
/// This single cache is shared across Binary/JSON Serializers/Deserializers.
/// </summary>
protected static readonly ConcurrentDictionary<(Type, Type), TypeMetadataBase> GlobalMetadataCache = new();
//protected static readonly ConcurrentDictionary<(Type, Type), TypeMetadataBase> GlobalMetadataCache = new();
/// <summary>
/// Maximum ThreadLocal cache size before clearing.