Refactor write plan entry tracking to use VisitIndex
Switch from caching next entry object to caching VisitIndex for write plan tracking in binary serialization. Updates TryConsumeWritePlanEntry and related logic for improved simplicity and performance. Comments revised to match new approach.
This commit is contained in:
parent
15da68fe25
commit
97ece85ee1
|
|
@ -132,15 +132,14 @@ public static partial class AcBinarySerializer
|
|||
|
||||
/// <summary>
|
||||
/// Sorts write plan by VisitIndex for sequential cursor consumption in write pass.
|
||||
/// Called once after scan pass completes. Pre-loads first entry for TryConsumeWritePlanEntry.
|
||||
/// Called once after scan pass completes.
|
||||
/// </summary>
|
||||
internal void SortWritePlan()
|
||||
{
|
||||
if (_writePlanCount > 1)
|
||||
_writePlan.AsSpan(0, _writePlanCount).Sort(static (a, b) => a.VisitIndex.CompareTo(b.VisitIndex));
|
||||
|
||||
if (_writePlanCount > 0)
|
||||
_nextWritePlanEntry = _writePlan![0];
|
||||
_nextWritePlanVisitIndex = _writePlanCount > 0 ? _writePlan![0].VisitIndex : int.MaxValue;
|
||||
}
|
||||
|
||||
/// <summary>Write pass cursor index into sorted _writePlan array.</summary>
|
||||
|
|
@ -150,10 +149,9 @@ public static partial class AcBinarySerializer
|
|||
internal int WriteVisitIndex;
|
||||
|
||||
/// <summary>
|
||||
/// Pre-loaded next write plan entry. Null when plan is empty or exhausted.
|
||||
/// Avoids array access on hit path — entry is already cached.
|
||||
/// Pre-cached VisitIndex of the next write plan entry, or int.MaxValue when exhausted.
|
||||
/// </summary>
|
||||
private WriteDuplicateEntry? _nextWritePlanEntry;
|
||||
private int _nextWritePlanVisitIndex = int.MaxValue;
|
||||
|
||||
/// <summary>
|
||||
/// Set per-property in WritePropertyOrSkip before calling WriteString.
|
||||
|
|
@ -171,13 +169,12 @@ public static partial class AcBinarySerializer
|
|||
internal bool TryConsumeWritePlanEntry(out WriteDuplicateEntry entry)
|
||||
{
|
||||
var visitIndex = WriteVisitIndex++;
|
||||
var next = _nextWritePlanEntry;
|
||||
if (next != null && visitIndex == next.Value.VisitIndex)
|
||||
if (visitIndex == _nextWritePlanVisitIndex)
|
||||
{
|
||||
entry = next.Value;
|
||||
_nextWritePlanEntry = ++WritePlanCursor < _writePlanCount
|
||||
? _writePlan![WritePlanCursor]
|
||||
: null;
|
||||
entry = _writePlan![WritePlanCursor++];
|
||||
_nextWritePlanVisitIndex = WritePlanCursor < _writePlanCount
|
||||
? _writePlan[WritePlanCursor].VisitIndex
|
||||
: int.MaxValue;
|
||||
return true;
|
||||
}
|
||||
entry = default;
|
||||
|
|
@ -300,7 +297,7 @@ public static partial class AcBinarySerializer
|
|||
ScanVisitIndex = 0;
|
||||
WritePlanCursor = 0;
|
||||
WriteVisitIndex = 0;
|
||||
_nextWritePlanEntry = null;
|
||||
_nextWritePlanVisitIndex = int.MaxValue;
|
||||
StringInternEligible = false;
|
||||
|
||||
// Clear write plan string references to avoid GC pinning, keep array if small enough
|
||||
|
|
|
|||
Loading…
Reference in New Issue