Skip to content

JsonConsoleFormatter memory optimization opportunity #98702

Open
@cd21h

Description

@cd21h

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);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions