Use ReadOnlyMemory<byte> for binary data to eliminate UTF-16 transcoding (#1070)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
Co-authored-by: Eric StJohn <ericstj@microsoft.com>
This commit is contained in:
Copilot 2026-02-19 14:08:05 -08:00 • committed by GitHub
commit 80b1ad21d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,9 @@
using System.Buffers;
using System.Buffers.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
@ -87,7 +90,7 @@ public abstract class ContentBlock
string? type = null;
string? text = null;
string? name = null;
string? data = null;
ReadOnlyMemory<byte>? data = null;
string? mimeType = null;
string? uri = null;
string? description = null;
@ -128,7 +131,7 @@ public abstract class ContentBlock
break;
case "data":
data = reader.GetString();
data = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
break;
case "mimeType":
@ -279,12 +282,12 @@ public abstract class ContentBlock
break;
case ImageContentBlock imageContent:
writer.WriteString("data", imageContent.Data);
writer.WriteString("data", imageContent.Data.Span);
writer.WriteString("mimeType", imageContent.MimeType);
break;
case AudioContentBlock audioContent:
writer.WriteString("data", audioContent.Data);
writer.WriteString("data", audioContent.Data.Span);
writer.WriteString("mimeType", audioContent.MimeType);
break;
@ -376,14 +379,73 @@ public sealed class TextContentBlock : ContentBlock
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class ImageContentBlock : ContentBlock
{
private ReadOnlyMemory<byte>? _decodedData;
private ReadOnlyMemory<byte> _data;
/// <summary>
/// Creates an <see cref="ImageContentBlock"/> from decoded image bytes.
/// </summary>
/// <param name="bytes">The unencoded image bytes.</param>
/// <param name="mimeType">The MIME type of the image.</param>
/// <returns>A new <see cref="ImageContentBlock"/> instance.</returns>
/// <remarks>
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// </remarks>
/// <exception cref="InvalidOperationException"></exception>
public static ImageContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
{
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
return new()
{
_decodedData = bytes,
Data = data,
MimeType = mimeType
};
}
/// <inheritdoc/>
public override string Type => "image";
/// <summary>
/// Gets or sets the base64-encoded image data.
/// Gets or sets the base64-encoded UTF-8 bytes representing the image data.
/// </summary>
/// <remarks>
/// Setting this value will invalidate any cached value of <see cref="DecodedData"/>.
/// </remarks>
[JsonPropertyName("data")]
public required string Data { get; set; }
public required ReadOnlyMemory<byte> Data
{
get => _data;
set
{
_data = value;
_decodedData = null; // Invalidate cache
}
}
/// <summary>
/// Gets the decoded image data represented by <see cref="Data"/>.
/// </summary>
/// <remarks>
/// <para>
/// When getting, this member will decode the value in <see cref="Data"/> and cache the result.
/// Subsequent accesses return the cached value unless <see cref="Data"/> is modified.
/// </para>
/// </remarks>
[JsonIgnore]
public ReadOnlyMemory<byte> DecodedData
{
get
{
if (_decodedData is null)
{
_decodedData = EncodingUtilities.DecodeFromBase64Utf8(Data);
}
return _decodedData.Value;
}
}
/// <summary>
/// Gets or sets the MIME type (or "media type") of the content, specifying the format of the data.
@ -402,14 +464,73 @@ public sealed class ImageContentBlock : ContentBlock
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class AudioContentBlock : ContentBlock
{
private ReadOnlyMemory<byte>? _decodedData;
private ReadOnlyMemory<byte> _data;
/// <summary>
/// Creates an <see cref="AudioContentBlock"/> from decoded audio bytes.
/// </summary>
/// <param name="bytes">The unencoded audio bytes.</param>
/// <param name="mimeType">The MIME type of the audio.</param>
/// <returns>A new <see cref="AudioContentBlock"/> instance.</returns>
/// <remarks>
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// </remarks>
/// <exception cref="InvalidOperationException"></exception>
public static AudioContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
{
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
return new()
{
_decodedData = bytes,
Data = data,
MimeType = mimeType
};
}
/// <inheritdoc/>
public override string Type => "audio";
/// <summary>
/// Gets or sets the base64-encoded audio data.
/// Gets or sets the base64-encoded UTF-8 bytes representing the audio data.
/// </summary>
/// <remarks>
/// Setting this value will invalidate any cached value of <see cref="DecodedData"/>.
/// </remarks>
[JsonPropertyName("data")]
public required string Data { get; set; }
public required ReadOnlyMemory<byte> Data
{
get => _data;
set
{
_data = value;
_decodedData = null; // Invalidate cache
}
}
/// <summary>
/// Gets the decoded audio data represented by <see cref="Data"/>.
/// </summary>
/// <remarks>
/// <para>
/// When getting, this member will decode the value in <see cref="Data"/> and cache the result.
/// Subsequent accesses return the cached value unless <see cref="Data"/> is modified.
/// </para>
/// </remarks>
[JsonIgnore]
public ReadOnlyMemory<byte> DecodedData
{
get
{
if (_decodedData is null)
{
_decodedData = EncodingUtilities.DecodeFromBase64Utf8(Data);
}
return _decodedData.Value;
}
}
/// <summary>
/// Gets or sets the MIME type (or "media type") of the content, specifying the format of the data.