From 6cb303110b26801490a01ba882535c1e893feffc Mon Sep 17 00:00:00 2001 From: Andrew J Said Date: Thu, 27 Jun 2024 17:39:26 +0100 Subject: [PATCH 1/4] Improve perf of JsonValue.CreateFromElement by calling JsonElement.ValueKind only once --- .../src/System/Text/Json/Nodes/JsonValue.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs index 09c21dbd5bb16e..cc2b1ad36d66de 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs @@ -181,13 +181,15 @@ internal static JsonValue CreateFromTypeInfo(T value, JsonTypeInfo jsonTyp internal static JsonValue? CreateFromElement(ref readonly JsonElement element, JsonNodeOptions? options = null) { - if (element.ValueKind is JsonValueKind.Null) + JsonValueKind kind = element.ValueKind; + + if (kind is JsonValueKind.Null) { return null; } // Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue. - if (element.ValueKind is JsonValueKind.Object or JsonValueKind.Array) + if (kind is JsonValueKind.Object or JsonValueKind.Array) { ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray(); } From 50c06bef90a6eb8bfdfc51840e304e0114d18df3 Mon Sep 17 00:00:00 2001 From: Andrew J Said Date: Thu, 27 Jun 2024 17:59:43 +0100 Subject: [PATCH 2/4] Use switch statement instead of multiple ifs --- .../src/System/Text/Json/Nodes/JsonValue.cs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs index cc2b1ad36d66de..fb9cff1ba670e3 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; @@ -181,20 +180,22 @@ internal static JsonValue CreateFromTypeInfo(T value, JsonTypeInfo jsonTyp internal static JsonValue? CreateFromElement(ref readonly JsonElement element, JsonNodeOptions? options = null) { - JsonValueKind kind = element.ValueKind; - - if (kind is JsonValueKind.Null) + switch (element.ValueKind) { - return null; - } + case JsonValueKind.Null: + return null; - // Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue. - if (kind is JsonValueKind.Object or JsonValueKind.Array) - { - ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray(); - } + case JsonValueKind.Object: + case JsonValueKind.Array: + // Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue. + ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray(); + + // The above method will throw, but the compiler doesn't know that. + return default; - return new JsonValueOfElement(element, options); + default: + return new JsonValueOfElement(element, options); + } } } } From a0d762d7e558ec65f23f2b8ff7c4776b1c66580a Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Fri, 28 Jun 2024 00:30:09 +0100 Subject: [PATCH 3/4] Update src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs --- .../System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs index fb9cff1ba670e3..d0405f13f2d92a 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs @@ -189,9 +189,7 @@ internal static JsonValue CreateFromTypeInfo(T value, JsonTypeInfo jsonTyp case JsonValueKind.Array: // Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue. ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray(); - - // The above method will throw, but the compiler doesn't know that. - return default; + return null; default: return new JsonValueOfElement(element, options); From 8d376f9c302365be5192da4c4716643c4cbcd0fb Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Fri, 28 Jun 2024 00:30:30 +0100 Subject: [PATCH 4/4] Update src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs --- .../System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs index d0405f13f2d92a..d93546cdf92e42 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs @@ -185,8 +185,7 @@ internal static JsonValue CreateFromTypeInfo(T value, JsonTypeInfo jsonTyp case JsonValueKind.Null: return null; - case JsonValueKind.Object: - case JsonValueKind.Array: + case JsonValueKind.Object or JsonValueKind.Array: // Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue. ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray(); return null;