From 2decee42ec1b8ee05ed08285290cc9b317bdc0f4 Mon Sep 17 00:00:00 2001 From: David Bures <12524436+PiDiBi@users.noreply.github.com> Date: Tue, 12 Apr 2022 01:43:15 -0700 Subject: [PATCH] performance patch of JsonConverterHelper.cs (#26448) * performance update on regex parsing --- .../Serialization/JsonConverterHelper.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/sdk/mgmtcommon/ClientRuntime/ClientRuntime/Serialization/JsonConverterHelper.cs b/sdk/mgmtcommon/ClientRuntime/ClientRuntime/Serialization/JsonConverterHelper.cs index e5eaee3cf114..b48de950ff1c 100644 --- a/sdk/mgmtcommon/ClientRuntime/ClientRuntime/Serialization/JsonConverterHelper.cs +++ b/sdk/mgmtcommon/ClientRuntime/ClientRuntime/Serialization/JsonConverterHelper.cs @@ -19,6 +19,8 @@ namespace Microsoft.Rest.Serialization /// public static class JsonConverterHelper { + private static readonly Regex splitCompiledRegex = new Regex(@"(? /// Serializes properties of the value object into JsonWriter. /// @@ -113,19 +115,24 @@ public static string GetPropertyName(this JsonProperty property, out string[] pa if (!string.IsNullOrEmpty(propertyName)) { - string[] hierarchy = Regex.Split(propertyName, @"(? 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; } -} \ No newline at end of file +}