dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.26k stars 4.73k forks source link

JsonConsoleFormatter memory optimization opportunity #98702

Open cd21h opened 8 months ago

cd21h commented 8 months ago

There is an opportunity to remove memory application by replacing string with shared buffer:

https://github.com/dotnet/runtime/blob/2df640c9cb9cd3b50225b9a4fa65e8f414f50bdf/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs#L79 textWriter.Write(Encoding.UTF8.GetString(output.WrittenMemory.Span));

can be replaced with

var messageBytes = writerBuffer.WrittenMemory.Span;
buffer = ArrayPool<char>.Shared.Rent(Encoding.UTF8.GetCharCount(messageBytes));
var charsWritten = Encoding.UTF8.GetChars(messageBytes, buffer);
textWriter.Write(buffer, 0, charsWritten);
ArrayPool<char>.Shared.Return(buffer);
ghost commented 8 months ago

Tagging subscribers to this area: @dotnet/area-system-memory See info in area-owners.md if you want to be subscribed.

Issue Details
There is an opportunity to remove memory application by replacing string with shared buffer: https://github.com/dotnet/runtime/blob/2df640c9cb9cd3b50225b9a4fa65e8f414f50bdf/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs#L79 `textWriter.Write(Encoding.UTF8.GetString(output.WrittenMemory.Span));` can be replaced with ```csharp var messageBytes = writerBuffer.WrittenMemory.Span; buffer = ArrayPool.Shared.Rent(Encoding.UTF8.GetCharCount(messageBytes)); var charsWritten = Encoding.UTF8.GetChars(messageBytes, buffer); textWriter.Write(buffer, 0, charsWritten); ArrayPool.Shared.Return(buffer); ```
Author: shatl
Assignees: -
Labels: `area-System.Memory`
Milestone: -
ghost commented 8 months ago

Tagging subscribers to this area: @dotnet/area-extensions-logging See info in area-owners.md if you want to be subscribed.

Issue Details
There is an opportunity to remove memory application by replacing string with shared buffer: https://github.com/dotnet/runtime/blob/2df640c9cb9cd3b50225b9a4fa65e8f414f50bdf/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs#L79 `textWriter.Write(Encoding.UTF8.GetString(output.WrittenMemory.Span));` can be replaced with ```csharp var messageBytes = writerBuffer.WrittenMemory.Span; buffer = ArrayPool.Shared.Rent(Encoding.UTF8.GetCharCount(messageBytes)); var charsWritten = Encoding.UTF8.GetChars(messageBytes, buffer); textWriter.Write(buffer, 0, charsWritten); ArrayPool.Shared.Return(buffer); ```
Author: shatl
Assignees: -
Labels: `untriaged`, `area-Extensions-Logging`
Milestone: -
tarekgh commented 8 months ago

@shatl the suggested code will not compile for netstandard2.0/NET Framework. But it is possible to optimize it for these two targets by using unsafe code. Are you interested to submit a PR for that?

cd21h commented 8 months ago

Will do