From f84dcb773d1e5271f6a25b11f0e388660e8dd5f6 Mon Sep 17 00:00:00 2001 From: Loretta Date: Fri, 13 Feb 2026 09:46:44 +0100 Subject: [PATCH] Refactor Populate and add PopulateMerge for byte[] input Refactored Populate(byte[], T) to add null checks, early returns, and proper context management. Added PopulateMerge(byte[], T) for merge semantics with IId collections, including merge mode and orphaned item removal. --- .../Binaries/AcBinaryDeserializer.cs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/AyCode.Core/Serializers/Binaries/AcBinaryDeserializer.cs b/AyCode.Core/Serializers/Binaries/AcBinaryDeserializer.cs index 96daac3..8e8645b 100644 --- a/AyCode.Core/Serializers/Binaries/AcBinaryDeserializer.cs +++ b/AyCode.Core/Serializers/Binaries/AcBinaryDeserializer.cs @@ -349,7 +349,16 @@ public static partial class AcBinaryDeserializer /// Populate existing object from binary data. /// public static void Populate(byte[] data, T target) where T : class - => Populate(data.AsSpan(), target, AcBinarySerializerOptions.Default); + { + ArgumentNullException.ThrowIfNull(target); + if (data.Length == 0) return; + if (data.Length == 1 && data[0] == BinaryTypeCode.Null) return; + + var context = DeserializationContextPool.Get(AcBinarySerializerOptions.Default); + context.InitInput(new ArrayBinaryInput(data)); + try { PopulateCore(context, target); } + finally { DeserializationContextPool.Return(context); } + } /// /// Populate existing object from binary data with options. @@ -378,6 +387,28 @@ public static partial class AcBinaryDeserializer finally { DeserializationContextPool.Return(context); } } + /// + /// Populate with merge semantics for IId collections from byte[] (zero-copy). + /// + public static void PopulateMerge(byte[] data, T target) where T : class + { + ArgumentNullException.ThrowIfNull(target); + if (data.Length == 0) return; + if (data.Length == 1 && data[0] == BinaryTypeCode.Null) return; + + var context = DeserializationContextPool.Get(AcBinarySerializerOptions.Default); + context.InitInput(new ArrayBinaryInput(data)); + context.IsMergeMode = true; + context.RemoveOrphanedItems = AcBinarySerializerOptions.Default.RemoveOrphanedItems; + try + { + PopulateMergeCore(context, target); + } + finally + { + DeserializationContextPool.Return(context); + } + } /// /// Populate with merge semantics for IId collections. ///