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>
|
/// <summary>
|
||||||
/// Sorts write plan by VisitIndex for sequential cursor consumption in write pass.
|
/// 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>
|
/// </summary>
|
||||||
internal void SortWritePlan()
|
internal void SortWritePlan()
|
||||||
{
|
{
|
||||||
if (_writePlanCount > 1)
|
if (_writePlanCount > 1)
|
||||||
_writePlan.AsSpan(0, _writePlanCount).Sort(static (a, b) => a.VisitIndex.CompareTo(b.VisitIndex));
|
_writePlan.AsSpan(0, _writePlanCount).Sort(static (a, b) => a.VisitIndex.CompareTo(b.VisitIndex));
|
||||||
|
|
||||||
if (_writePlanCount > 0)
|
_nextWritePlanVisitIndex = _writePlanCount > 0 ? _writePlan![0].VisitIndex : int.MaxValue;
|
||||||
_nextWritePlanEntry = _writePlan![0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Write pass cursor index into sorted _writePlan array.</summary>
|
/// <summary>Write pass cursor index into sorted _writePlan array.</summary>
|
||||||
|
|
@ -150,10 +149,9 @@ public static partial class AcBinarySerializer
|
||||||
internal int WriteVisitIndex;
|
internal int WriteVisitIndex;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pre-loaded next write plan entry. Null when plan is empty or exhausted.
|
/// Pre-cached VisitIndex of the next write plan entry, or int.MaxValue when exhausted.
|
||||||
/// Avoids array access on hit path — entry is already cached.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private WriteDuplicateEntry? _nextWritePlanEntry;
|
private int _nextWritePlanVisitIndex = int.MaxValue;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set per-property in WritePropertyOrSkip before calling WriteString.
|
/// Set per-property in WritePropertyOrSkip before calling WriteString.
|
||||||
|
|
@ -171,13 +169,12 @@ public static partial class AcBinarySerializer
|
||||||
internal bool TryConsumeWritePlanEntry(out WriteDuplicateEntry entry)
|
internal bool TryConsumeWritePlanEntry(out WriteDuplicateEntry entry)
|
||||||
{
|
{
|
||||||
var visitIndex = WriteVisitIndex++;
|
var visitIndex = WriteVisitIndex++;
|
||||||
var next = _nextWritePlanEntry;
|
if (visitIndex == _nextWritePlanVisitIndex)
|
||||||
if (next != null && visitIndex == next.Value.VisitIndex)
|
|
||||||
{
|
{
|
||||||
entry = next.Value;
|
entry = _writePlan![WritePlanCursor++];
|
||||||
_nextWritePlanEntry = ++WritePlanCursor < _writePlanCount
|
_nextWritePlanVisitIndex = WritePlanCursor < _writePlanCount
|
||||||
? _writePlan![WritePlanCursor]
|
? _writePlan[WritePlanCursor].VisitIndex
|
||||||
: null;
|
: int.MaxValue;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
entry = default;
|
entry = default;
|
||||||
|
|
@ -300,7 +297,7 @@ public static partial class AcBinarySerializer
|
||||||
ScanVisitIndex = 0;
|
ScanVisitIndex = 0;
|
||||||
WritePlanCursor = 0;
|
WritePlanCursor = 0;
|
||||||
WriteVisitIndex = 0;
|
WriteVisitIndex = 0;
|
||||||
_nextWritePlanEntry = null;
|
_nextWritePlanVisitIndex = int.MaxValue;
|
||||||
StringInternEligible = false;
|
StringInternEligible = false;
|
||||||
|
|
||||||
// Clear write plan string references to avoid GC pinning, keep array if small enough
|
// Clear write plan string references to avoid GC pinning, keep array if small enough
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue