Skip to content

Commit

Permalink
Revert back to using original caching algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Jul 2, 2024
1 parent ceee5aa commit 7e92d09
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace System.Text.Json.Serialization.Metadata
{
Expand Down Expand Up @@ -72,6 +73,7 @@ internal Dictionary<string, JsonPropertyInfo> PropertyIndex
/// <summary>
/// Defines the core property lookup logic for a given unescaped UTF-8 encoded property name.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal JsonPropertyInfo? GetProperty(ReadOnlySpan<byte> propertyName, ref ReadStackFrame frame, out byte[] utf8PropertyName)
{
Debug.Assert(IsConfigured);
Expand All @@ -87,33 +89,55 @@ internal Dictionary<string, JsonPropertyInfo> PropertyIndex

if (!utf8PropertyCacheSpan.IsEmpty)
{
PropertyRef propertyRef;

// Start with the current property index, and then go forwards\backwards.
int propertyIndex = frame.PropertyIndex;

int count = utf8PropertyCacheSpan.Length;
int iForward = Math.Min(propertyIndex, count);
int iBackward = iForward;
int iBackward = iForward - 1;

while (iForward - iBackward < count)
while (true)
{
if (iForward < count)
{
PropertyRef propertyRef = utf8PropertyCacheSpan[iForward++];
propertyRef = utf8PropertyCacheSpan[iForward];
if (propertyRef.Equals(propertyName, key))
{
utf8PropertyName = propertyRef.Utf8PropertyName;
return propertyRef.Info;
}
}

if (iBackward > 0)
++iForward;

if (iBackward >= 0)
{
propertyRef = utf8PropertyCacheSpan[iBackward];
if (propertyRef.Equals(propertyName, key))
{
utf8PropertyName = propertyRef.Utf8PropertyName;
return propertyRef.Info;
}

--iBackward;
}
}
else if (iBackward >= 0)
{
PropertyRef propertyRef = utf8PropertyCacheSpan[--iBackward];
propertyRef = utf8PropertyCacheSpan[iBackward];
if (propertyRef.Equals(propertyName, key))
{
utf8PropertyName = propertyRef.Utf8PropertyName;
return propertyRef.Info;
}

--iBackward;
}
else
{
// Property was not found.
break;
}
}
}
Expand Down

0 comments on commit 7e92d09

Please sign in to comment.