From 0bfd72c2267a963beb480f67c3131e9babbf6009 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Fri, 5 Apr 2024 09:11:01 +0200 Subject: [PATCH] Make CompositeNodeIdValueSerializer more efficient by removing nullables. --- .../CompositeNodeIdValueSerialize.cs | 22 +++++++++---------- .../Relay/DefaultNodeIdSerializerTests.cs | 8 +++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/HotChocolate/Core/src/Types/Types/Relay/Serialization/CompositeNodeIdValueSerialize.cs b/src/HotChocolate/Core/src/Types/Types/Relay/Serialization/CompositeNodeIdValueSerialize.cs index 152bd898010..c77b12b5c68 100644 --- a/src/HotChocolate/Core/src/Types/Types/Relay/Serialization/CompositeNodeIdValueSerialize.cs +++ b/src/HotChocolate/Core/src/Types/Types/Relay/Serialization/CompositeNodeIdValueSerialize.cs @@ -376,7 +376,7 @@ protected static unsafe bool TryParseIdPart( /// protected static bool TryParseIdPart( ReadOnlySpan buffer, - [NotNullWhen(true)] out Guid? value, + out Guid value, out int consumed, bool compress = true) { @@ -387,7 +387,7 @@ protected static bool TryParseIdPart( { if (valueSpan.Length != 16) { - value = null; + value = default; consumed = 0; return false; } @@ -408,7 +408,7 @@ protected static bool TryParseIdPart( return true; } - value = null; + value = default; consumed = 0; return false; } @@ -430,7 +430,7 @@ protected static bool TryParseIdPart( /// protected static bool TryParseIdPart( ReadOnlySpan buffer, - [NotNullWhen(true)] out short? value, + out short value, out int consumed) { var index = buffer.IndexOf(_partSeparator); @@ -443,7 +443,7 @@ protected static bool TryParseIdPart( return true; } - value = null; + value = default; consumed = 0; return false; } @@ -465,7 +465,7 @@ protected static bool TryParseIdPart( /// protected static bool TryParseIdPart( ReadOnlySpan buffer, - [NotNullWhen(true)] out int? value, + out int value, out int consumed) { var index = buffer.IndexOf(_partSeparator); @@ -478,7 +478,7 @@ protected static bool TryParseIdPart( return true; } - value = null; + value = default; consumed = 0; return false; } @@ -500,7 +500,7 @@ protected static bool TryParseIdPart( /// protected static bool TryParseIdPart( ReadOnlySpan buffer, - [NotNullWhen(true)] out long? value, + out long value, out int consumed) { var index = buffer.IndexOf(_partSeparator); @@ -513,7 +513,7 @@ protected static bool TryParseIdPart( return true; } - value = null; + value = default; consumed = 0; return false; } @@ -535,7 +535,7 @@ protected static bool TryParseIdPart( /// protected static bool TryParseIdPart( ReadOnlySpan buffer, - [NotNullWhen(true)] out bool? value, + out bool value, out int consumed) { var index = buffer.IndexOf(_partSeparator); @@ -548,7 +548,7 @@ protected static bool TryParseIdPart( return true; } - value = null; + value = default; consumed = 0; return false; } diff --git a/src/HotChocolate/Core/test/Types.Tests/Types/Relay/DefaultNodeIdSerializerTests.cs b/src/HotChocolate/Core/test/Types.Tests/Types/Relay/DefaultNodeIdSerializerTests.cs index 317bcaa910a..43782e7deae 100644 --- a/src/HotChocolate/Core/test/Types.Tests/Types/Relay/DefaultNodeIdSerializerTests.cs +++ b/src/HotChocolate/Core/test/Types.Tests/Types/Relay/DefaultNodeIdSerializerTests.cs @@ -182,11 +182,11 @@ protected override NodeIdFormatterResult Format(Span buffer, CompositeId v protected override bool TryParse(ReadOnlySpan buffer, out CompositeId value) { if(TryParseIdPart(buffer, out string a, out var ac) && - TryParseIdPart(buffer.Slice(ac), out int? b, out var bc) && - TryParseIdPart(buffer.Slice(ac + bc), out Guid? c, out var cc) && - TryParseIdPart(buffer.Slice(ac + bc + cc), out bool? d, out _)) + TryParseIdPart(buffer.Slice(ac), out int b, out var bc) && + TryParseIdPart(buffer.Slice(ac + bc), out Guid c, out var cc) && + TryParseIdPart(buffer.Slice(ac + bc + cc), out bool d, out _)) { - value = new CompositeId(a, b.Value, c.Value, d.Value); + value = new CompositeId(a, b, c, d); return true; }