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

JsonConsoleFormatter memory optimization opportunity #98702

Open
cd21h opened this issue Feb 20, 2024 · 4 comments
Open

JsonConsoleFormatter memory optimization opportunity #98702

cd21h opened this issue Feb 20, 2024 · 4 comments

Comments

@cd21h
Copy link
Contributor

cd21h commented Feb 20, 2024

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

textWriter.Write(Encoding.UTF8.GetString(output.WrittenMemory.Span));

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 ghost added the untriaged New issue has not been triaged by the area owner label Feb 20, 2024
@ghost
Copy link

ghost commented Feb 20, 2024

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:

textWriter.Write(Encoding.UTF8.GetString(output.WrittenMemory.Span));

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);
Author: shatl
Assignees: -
Labels:

area-System.Memory

Milestone: -

@ghost
Copy link

ghost commented Feb 21, 2024

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:

textWriter.Write(Encoding.UTF8.GetString(output.WrittenMemory.Span));

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);
Author: shatl
Assignees: -
Labels:

untriaged, area-Extensions-Logging

Milestone: -

@tarekgh
Copy link
Member

tarekgh commented Feb 21, 2024

@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?

@tarekgh tarekgh added this to the Future milestone Feb 21, 2024
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Feb 21, 2024
@cd21h
Copy link
Contributor Author

cd21h commented Feb 22, 2024

Will do

cd21h added a commit to cd21h/dotnet-runtime that referenced this issue Mar 11, 2024
cd21h added a commit to cd21h/dotnet-runtime that referenced this issue Mar 11, 2024
cd21h added a commit to cd21h/dotnet-runtime that referenced this issue Mar 11, 2024
tarekgh pushed a commit to cd21h/dotnet-runtime that referenced this issue May 14, 2024
tarekgh added a commit that referenced this issue May 15, 2024
* Optimize memory allocations (#98702)

* Use `UTF8.GetMaxCharCount`

* Optimize memory allocations (#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>
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this issue May 30, 2024
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants