Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
don't allocate the Stack unless it's necessary
  • Loading branch information
adamsitnik committed Aug 19, 2024
commit f89177c8b9edd88c72d73ef86dbfc49486872284
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,28 @@ public override long TotalElementsCount
{
if (_totalElementsCount < 0)
{
Debug.Assert(ArrayInfo.ArrayType == BinaryArrayType.Jagged);

Stack<BinaryArrayRecord> jaggedArrayRecords = new();
jaggedArrayRecords.Push(this);

_totalElementsCount = GetTotalElementsCount(jaggedArrayRecords);
_totalElementsCount = GetJaggedArrayTotalElementsCount(this);
}

return _totalElementsCount;
}
}

private static long GetTotalElementsCount(Stack<BinaryArrayRecord> jaggedArrayRecords)
private static long GetJaggedArrayTotalElementsCount(BinaryArrayRecord jaggedArrayRecord)
{
long result = 0;
while (jaggedArrayRecords.Count != 0)
Stack<BinaryArrayRecord>? jaggedArrayRecords = null;

do
{
BinaryArrayRecord current = jaggedArrayRecords.Pop();
Debug.Assert(current.ArrayInfo.ArrayType == BinaryArrayType.Jagged);
if (jaggedArrayRecords is not null)
{
jaggedArrayRecord = jaggedArrayRecords.Pop();
}

Debug.Assert(jaggedArrayRecord.ArrayInfo.ArrayType == BinaryArrayType.Jagged);

foreach (object value in current.Values)
foreach (object value in jaggedArrayRecord.Values)
{
object item = value is MemberReferenceRecord referenceRecord
? referenceRecord.GetReferencedRecord()
Expand All @@ -93,7 +94,7 @@ private static long GetTotalElementsCount(Stack<BinaryArrayRecord> jaggedArrayRe
ArrayRecord nestedArrayRecord = (ArrayRecord)record;
if (nestedArrayRecord.ArrayInfo.ArrayType == BinaryArrayType.Jagged)
{
jaggedArrayRecords.Push((BinaryArrayRecord)nestedArrayRecord);
(jaggedArrayRecords ??= new()).Push((BinaryArrayRecord)nestedArrayRecord);
}
else
{
Expand All @@ -112,6 +113,7 @@ private static long GetTotalElementsCount(Stack<BinaryArrayRecord> jaggedArrayRe
}
}
}
while (jaggedArrayRecords is not null && jaggedArrayRecords.Count > 0);

return result;
}
Expand Down