Skip to content

Add Encode(ReadOnlySpan<char>) method to TextEncoder for performance #30274

@steveharter

Description

@steveharter

With the recent custom encoder work, it was found that the existing System.Text.Encodings.Web.TextEncoder class does not have an overload of an "Encode" method taking a ReadOnlySpan<char>. The causes a perf hit because the callers (System.Text.Json's writer and serializer) must convert utf16->utf8 (in order to call EncodeUtf8)->utf16.

Proposed API:

namespace System.Text.Encodings.Web
{
    public class TextEncoder // existing class
    {
        // Existing method as a reference:
        public virtual OperationStatus EncodeUtf8(ReadOnlySpan<byte> utf8Source, Span<byte> utf8Destination, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true);

        // Proposed method:
        public virtual OperationStatus Encode(ReadOnlySpan<char> source, Span<char> destination, out int charsConsumed, out int charsWritten, bool isFinalBlock = true);
    }
}

In addition, there may be some overrides of this new virtual method on derived classes pending discussion\implementation.

cc @GrabYourPitchforks @ahsonkhan

(Method signatures edited by @GrabYourPitchforks on Jul. 16, 2019.)

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions