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

GC pressure fixes #598

Merged
merged 2 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/StreamJsonRpc/MessageHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ public virtual async Task DisposeAsync()
// If they're active, they'll take care of themselves when they finish since we signaled disposal.
lock (this.syncObject)
{
if (!this.state.HasFlag(MessageHandlerState.Reading))
if ((this.state & MessageHandlerState.Reading) != MessageHandlerState.Reading)
{
this.readingCompleted.Set();
}

if (!this.state.HasFlag(MessageHandlerState.Writing))
if ((this.state & MessageHandlerState.Writing) != MessageHandlerState.Writing)
{
this.writingCompleted.Set();
}
Expand Down Expand Up @@ -347,7 +347,7 @@ private void SetState(MessageHandlerState startingOperation)
{
Verify.NotDisposed(this);
MessageHandlerState state = this.state;
Assumes.False(state.HasFlag(startingOperation));
Assumes.False((state & startingOperation) == startingOperation);
this.state |= startingOperation;
}
}
Expand Down
59 changes: 53 additions & 6 deletions src/StreamJsonRpc/MessagePackFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public class MessagePackFormatter : IJsonRpcMessageFormatter, IJsonRpcInstanceCo

private readonly PipeFormatterResolver pipeFormatterResolver;

private readonly ToStringHelper serializationToStringHelper = new ToStringHelper();

private readonly ToStringHelper deserializationToStringHelper = new ToStringHelper();

/// <summary>
/// Backing field for the <see cref="MultiplexingStream"/> property.
/// </summary>
Expand Down Expand Up @@ -286,7 +290,15 @@ public JsonRpcMessage Deserialize(ReadOnlySequence<byte> contentBuffer)
JsonRpcMessage message = MessagePackSerializer.Deserialize<JsonRpcMessage>(contentBuffer, this.messageSerializationOptions);

IJsonRpcTracingCallbacks? tracingCallbacks = this.rpc;
tracingCallbacks?.OnMessageDeserialized(message, new ToStringHelper(contentBuffer, this.messageSerializationOptions));
this.deserializationToStringHelper.Activate(contentBuffer, this.messageSerializationOptions);
try
{
tracingCallbacks?.OnMessageDeserialized(message, this.deserializationToStringHelper);
}
finally
{
this.deserializationToStringHelper.Deactivate();
}

return message;
}
Expand Down Expand Up @@ -323,7 +335,15 @@ public void Serialize(IBufferWriter<byte> contentBuffer, JsonRpcMessage message)
void IJsonRpcFormatterTracingCallbacks.OnSerializationComplete(JsonRpcMessage message, ReadOnlySequence<byte> encodedMessage)
{
IJsonRpcTracingCallbacks? tracingCallbacks = this.rpc;
tracingCallbacks?.OnMessageSerialized(message, new ToStringHelper(encodedMessage, this.messageSerializationOptions));
this.serializationToStringHelper.Activate(encodedMessage, this.messageSerializationOptions);
try
{
tracingCallbacks?.OnMessageSerialized(message, this.serializationToStringHelper);
}
finally
{
this.serializationToStringHelper.Deactivate();
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -743,18 +763,45 @@ public object Convert(object value, TypeCode typeCode)
public ulong ToUInt64(object value) => ((RawMessagePack)value).Deserialize<ulong>(this.options);
}

/// <summary>
/// A recyclable object that can serialize a message to JSON on demand.
/// </summary>
/// <remarks>
/// In perf traces, creation of this object used to show up as one of the most allocated objects.
/// It is used even when tracing isn't active. So we changed its design it to be reused,
/// since its lifetime is only required during a synchronous call to a trace API.
/// </remarks>
private class ToStringHelper
{
private readonly ReadOnlySequence<byte> encodedMessage;
private readonly MessagePackSerializerOptions options;
private ReadOnlySequence<byte>? encodedMessage;
private MessagePackSerializerOptions? options;
private string? jsonString;

internal ToStringHelper(ReadOnlySequence<byte> encodedMessage, MessagePackSerializerOptions options)
public override string ToString()
{
Verify.Operation(this.encodedMessage.HasValue, "This object has not been activated. It may have already been recycled.");

return this.jsonString ??= MessagePackSerializer.ConvertToJson(this.encodedMessage.Value, this.options);
}

/// <summary>
/// Initializes this object to represent a message.
/// </summary>
internal void Activate(ReadOnlySequence<byte> encodedMessage, MessagePackSerializerOptions options)
{
this.encodedMessage = encodedMessage;
this.options = options;
}

public override string ToString() => MessagePackSerializer.ConvertToJson(this.encodedMessage, this.options);
/// <summary>
/// Cleans out this object to release memory and ensure <see cref="ToString"/> throws if someone uses it after deactivation.
/// </summary>
internal void Deactivate()
{
this.encodedMessage = null;
this.options = null;
this.jsonString = null;
}
}

private class RequestIdFormatter : IMessagePackFormatter<RequestId>
Expand Down