From 7e92d098211ca309416283a55e54d07599b4f70b Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Tue, 2 Jul 2024 20:35:42 +0100 Subject: [PATCH] Revert back to using original caching algorithm. --- .../Metadata/JsonTypeInfo.Cache.cs | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs index 1e87eacc1aabad..03b296670d9658 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.CompilerServices; namespace System.Text.Json.Serialization.Metadata { @@ -72,6 +73,7 @@ internal Dictionary PropertyIndex /// /// Defines the core property lookup logic for a given unescaped UTF-8 encoded property name. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal JsonPropertyInfo? GetProperty(ReadOnlySpan propertyName, ref ReadStackFrame frame, out byte[] utf8PropertyName) { Debug.Assert(IsConfigured); @@ -87,33 +89,55 @@ internal Dictionary 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; } } }