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

Add NerdbankMessagePackFormatter #1100

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e934175
Update package versions and target frameworks
Dec 18, 2024
6819e6b
Continue implementing NerdbankMessagePackFormatter.
Dec 20, 2024
052592e
Refactor NerdbankMessagePackFormatter for converters
Dec 20, 2024
259558a
Add type converter registration methods to interface
Dec 21, 2024
e87da44
Enhance serialization context and type shape providers
Dec 21, 2024
ca439ea
Enhance NerdbankMessagePackFormatter with new methods
Dec 21, 2024
b12c357
Replaced the `IFormatterContextBuilder` interface with a concrete `Fo…
Dec 21, 2024
345a39c
Downgrade package versions for compatibility
Dec 21, 2024
ad436f7
User RawMessagePack from Nerdbank.MessagePack
Dec 21, 2024
1a795c7
Remove namespace aliases
Dec 21, 2024
0e6b4aa
Add unit tests
Dec 21, 2024
bca2af4
Some tests are now passing.
Dec 22, 2024
d693a1c
Cleaning up formatters. Rename `FormatterContext` to `FormatterProfile`.
Dec 22, 2024
0c06ae5
Adopt MessagePackString and configure test formatters.
Dec 24, 2024
166ae49
Adopt simpler MessagePackString write API
Dec 26, 2024
3d3cb90
Fix bug when searching property names from peek reader
Dec 26, 2024
d8430f4
Begin process of making converters stateless
Dec 27, 2024
6444f55
Remove state from converters.
Dec 27, 2024
bb22ee8
Refactor and add comments for Profile and Profile.Builder.
Dec 27, 2024
e171cdb
Add additional profile configuration to test suite.
Dec 28, 2024
d36d607
Update Benchmarks
Dec 28, 2024
949b3ab
Upgrade NB.MB version; Update tests.
Dec 29, 2024
f94bf1d
Refactor serialization to use ReadOnlySequence<byte>
Jan 1, 2025
eecd92c
Enhance NerdbankMessagePackFormatter with new types
Jan 1, 2025
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
5 changes: 3 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="$(VisualStudioThreadingVersion)" />
<PackageVersion Include="Microsoft.VisualStudio.Threading" Version="$(VisualStudioThreadingVersion)" />
<PackageVersion Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
<PackageVersion Include="Nerdbank.MessagePack" Version="0.3.54-beta" />
<PackageVersion Include="Nerdbank.Streams" Version="2.11.74" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="System.Collections.Immutable" Version="6.0.0" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="6.0.1" />
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
<PackageVersion Include="System.IO.Pipelines" Version="9.0.0" />
<PackageVersion Include="System.IO.Pipes" Version="4.3.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
Expand Down
15 changes: 15 additions & 0 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@
</config>
<packageSources>
<clear />
<add key="PublicCI" value="https://pkgs.dev.azure.com/andrewarnott/OSS/_packaging/PublicCI/nuget/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="msft_consumption" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/msft_consumption/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources>
<!-- Defend against user or machine level disabling of sources that we list in this file. -->
<clear />
</disabledPackageSources>
<packageSourceMapping>
<!-- key value for <packageSource> should match key values from <packageSources> element -->
<packageSource key="msft_consumption">
<package pattern="*" />
</packageSource>
<packageSource key="nuget.org">
<package pattern="System.*" />
<package pattern="Microsoft.Bcl.*" />
</packageSource>
<packageSource key="PublicCI">
<package pattern="Nerdbank.MessagePack" />
</packageSource>
</packageSourceMapping>
</configuration>
1 change: 1 addition & 0 deletions src/StreamJsonRpc/FormatterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO.Pipelines;
using System.Reflection;
using System.Runtime.Serialization;
using Nerdbank.MessagePack;
using Nerdbank.Streams;
using StreamJsonRpc.Protocol;
using StreamJsonRpc.Reflection;
Expand Down
2 changes: 1 addition & 1 deletion src/StreamJsonRpc/JsonRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ protected virtual async ValueTask<JsonRpcMessage> DispatchRequestAsync(JsonRpcRe
}

/// <summary>
/// Sends the JSON-RPC message to <see cref="IJsonRpcMessageHandler"/> intance to be transmitted.
/// Sends the JSON-RPC message to <see cref="IJsonRpcMessageHandler"/> instance to be transmitted.
/// </summary>
/// <param name="message">The message to send.</param>
/// <param name="cancellationToken">A token to cancel the send request.</param>
Expand Down
104 changes: 104 additions & 0 deletions src/StreamJsonRpc/NerdbankMessagePackFormatter.CommonString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using NBMP = Nerdbank.MessagePack;

namespace StreamJsonRpc;

public partial class NerdbankMessagePackFormatter
{
[DebuggerDisplay("{" + nameof(Value) + "}")]
private struct CommonString
trippwill marked this conversation as resolved.
Show resolved Hide resolved
{
internal CommonString(string value)
{
Requires.Argument(value.Length > 0 && value.Length <= 16, nameof(value), "Length must be >0 and <=16.");
this.Value = value;
ReadOnlyMemory<byte> encodedBytes = MessagePack.Internal.CodeGenHelpers.GetEncodedStringBytes(value);
this.EncodedBytes = encodedBytes;

ReadOnlySpan<byte> span = this.EncodedBytes.Span.Slice(1);
this.Key = MessagePack.Internal.AutomataKeyGen.GetKey(ref span); // header is 1 byte because string length <= 16
this.Key2 = span.Length > 0 ? (ulong?)MessagePack.Internal.AutomataKeyGen.GetKey(ref span) : null;
}

/// <summary>
/// Gets the original string.
/// </summary>
internal string Value { get; }

/// <summary>
/// Gets the 64-bit integer that represents the string without decoding it.
/// </summary>
private ulong Key { get; }

/// <summary>
/// Gets the next 64-bit integer that represents the string without decoding it.
/// </summary>
private ulong? Key2 { get; }

/// <summary>
/// Gets the messagepack header and UTF-8 bytes for this string.
/// </summary>
private ReadOnlyMemory<byte> EncodedBytes { get; }

/// <summary>
/// Writes out the messagepack binary for this common string, if it matches the given value.
/// </summary>
/// <param name="writer">The writer to use.</param>
/// <param name="value">The value to be written, if it matches this <see cref="CommonString"/>.</param>
/// <returns><see langword="true"/> if <paramref name="value"/> matches this <see cref="Value"/> and it was written; <see langword="false"/> otherwise.</returns>
internal bool TryWrite(ref NBMP::MessagePackWriter writer, string value)
{
if (value == this.Value)
{
this.Write(ref writer);
return true;
}

return false;
}

internal readonly void Write(ref NBMP::MessagePackWriter writer) => writer.WriteRaw(this.EncodedBytes.Span);

/// <summary>
/// Checks whether a span of UTF-8 bytes equal this common string.
/// </summary>
/// <param name="utf8String">The UTF-8 string.</param>
/// <returns><see langword="true"/> if the UTF-8 bytes are the encoding of this common string; <see langword="false"/> otherwise.</returns>
internal readonly bool TryRead(ReadOnlySpan<byte> utf8String)
{
if (utf8String.Length != this.EncodedBytes.Length - 1)
{
return false;
}

ulong key1 = MessagePack.Internal.AutomataKeyGen.GetKey(ref utf8String);
if (key1 != this.Key)
{
return false;
}

if (utf8String.Length > 0)
{
if (!this.Key2.HasValue)
{
return false;
}

ulong key2 = MessagePack.Internal.AutomataKeyGen.GetKey(ref utf8String);
if (key2 != this.Key2.Value)
{
return false;
}
}
else if (this.Key2.HasValue)
{
return false;
}

return true;
}
}
}
55 changes: 55 additions & 0 deletions src/StreamJsonRpc/NerdbankMessagePackFormatter.FormatterContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Nerdbank.MessagePack;
using PolyType;
using PolyType.Abstractions;

namespace StreamJsonRpc;

/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
/// <remarks>
/// The MessagePack implementation used here comes from https://github.com/AArnott/Nerdbank.MessagePack.
/// </remarks>
public sealed partial class NerdbankMessagePackFormatter
{
internal class FormatterContext(MessagePackSerializer serializer, ITypeShapeProvider shapeProvider)
{
public MessagePackSerializer Serializer => serializer;

public ITypeShapeProvider ShapeProvider => shapeProvider;

public T? Deserialize<T>(ref MessagePackReader reader, CancellationToken cancellationToken = default)
{
return serializer.Deserialize<T>(ref reader, shapeProvider, cancellationToken);
}

public T Deserialize<T>(in RawMessagePack pack, CancellationToken cancellationToken = default)
{
// TODO: Improve the exception
return serializer.Deserialize<T>(pack, shapeProvider, cancellationToken)
?? throw new InvalidOperationException("Deserialization failed.");
}

public object? DeserializeObject(in RawMessagePack pack, Type objectType, CancellationToken cancellationToken = default)
{
MessagePackReader reader = new(pack);
return serializer.DeserializeObject(
ref reader,
shapeProvider.Resolve(objectType),
cancellationToken);
}

public void Serialize<T>(ref MessagePackWriter writer, T? value, CancellationToken cancellationToken = default)
{
serializer.Serialize(ref writer, value, shapeProvider, cancellationToken);
}

internal void SerializeObject(ref MessagePackWriter writer, object? value, Type objectType, CancellationToken cancellationToken = default)
{
serializer.SerializeObject(ref writer, value, shapeProvider.Resolve(objectType), cancellationToken);
}
}
}
Loading