Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of async deserialization #130

Merged
merged 24 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Nerdbank.MessagePack/DecodeResultExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using static Nerdbank.MessagePack.MessagePackPrimitives;

namespace Nerdbank.MessagePack;

/// <summary>
/// Extension methods for the <see cref="DecodeResult"/> enum.
/// </summary>
public static class DecodeResultExtensions
{
/// <summary>
/// Processes a given <see cref="DecodeResult"/> value and returns a boolean indicating whether more bytes are needed.
/// </summary>
/// <param name="result">The <see cref="DecodeResult"/> value.</param>
/// <returns>
/// <see langword="true"/> if the value is <see cref="DecodeResult.InsufficientBuffer"/>;
/// <see langword="false"/> if the value is <see cref="DecodeResult.Success"/>.
/// </returns>
/// <exception cref="EndOfStreamException">Thrown if the value is <see cref="DecodeResult.EmptyBuffer"/>.</exception>
/// <exception cref="MessagePackSerializationException">Thrown if the value is <see cref="DecodeResult.TokenMismatch"/>.</exception>
public static bool NeedsMoreBytes(this DecodeResult result)
{
return result switch
{
DecodeResult.Success => false,
DecodeResult.InsufficientBuffer => true,
DecodeResult.EmptyBuffer => throw new EndOfStreamException(),
DecodeResult.TokenMismatch => throw new MessagePackSerializationException("Unexpected token encountered."), // TODO: Include the unexpected token into the exception message by packing MessagePackType into the DecodeResult.
_ => throw new UnreachableException(),
};
}
}
3 changes: 2 additions & 1 deletion src/Nerdbank.MessagePack/MessagePackPrimitives.Writers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -1100,7 +1101,7 @@ private static bool TryWriteNegativeFixIntUnsafe(Span<byte> destination, sbyte v
}

[DoesNotReturn]
private static Exception ThrowUnreachable() => throw new Exception("Presumed unreachable point in code reached.");
private static Exception ThrowUnreachable() => throw new UnreachableException();

/// <summary>
/// Writes an integer value in big-endian order to the specified buffer.
Expand Down
Loading
Loading