Skip to content

Commit

Permalink
performance patch of JsonConverterHelper.cs (#26448)
Browse files Browse the repository at this point in the history
* performance update on regex parsing
  • Loading branch information
PiDiBi authored Apr 12, 2022
1 parent 64b55ac commit 2decee4
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace Microsoft.Rest.Serialization
/// </summary>
public static class JsonConverterHelper
{
private static readonly Regex splitCompiledRegex = new Regex(@"(?<!\\)\.", RegexOptions.Compiled);

/// <summary>
/// Serializes properties of the value object into JsonWriter.
/// </summary>
Expand Down Expand Up @@ -113,19 +115,24 @@ public static string GetPropertyName(this JsonProperty property, out string[] pa

if (!string.IsNullOrEmpty(propertyName))
{
string[] hierarchy = Regex.Split(propertyName, @"(?<!\\)\.")
.Select(p => p?.Replace("\\.", ".")).ToArray();
string[] hierarchy = splitCompiledRegex.Split(propertyName);
for (int i = 0; i < hierarchy.Length; i++)
{
hierarchy[i] = hierarchy[i]?.Replace("\\.", ".");
}

if (hierarchy.Length > 1)
{
propertyName = hierarchy.Last();
parentPath = hierarchy.Take(hierarchy.Length - 1).ToArray();
propertyName = hierarchy[hierarchy.Length - 1];
Array.Resize(ref hierarchy, hierarchy.Length - 1);
parentPath = hierarchy;
}
}

return propertyName;
return propertyName;
}

public static bool IsJsonExtensionData(this JsonProperty property)
=> property.AttributeProvider.GetAttributes(typeof(JsonExtensionDataAttribute), true).Count != 0;
}
}
}

0 comments on commit 2decee4

Please sign in to comment.