-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
JsonConsoleFormatter memory optimization opportunity #98702
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsThere is an opportunity to remove memory application by replacing string with shared buffer: runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs Line 79 in 2df640c
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);
|
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsThere is an opportunity to remove memory application by replacing string with shared buffer: runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs Line 79 in 2df640c
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);
|
@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? |
Will do |
* Optimize memory allocations (dotnet#98702) * Use `UTF8.GetMaxCharCount` * Optimize memory allocations (dotnet#98702) * Use `UTF8.GetMaxCharCount` * Optimize netfx/netstandard 2.0 cases too * Fix formatting * Fix build error --------- Co-authored-by: Tarek Mahmoud Sayed <tarekms@microsoft.com>
There is an opportunity to remove memory application by replacing string with shared buffer:
runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs
Line 79 in 2df640c
textWriter.Write(Encoding.UTF8.GetString(output.WrittenMemory.Span));
can be replaced with
The text was updated successfully, but these errors were encountered: