From b94e14137f946e277e2e3f14f9846d221c3c30bf Mon Sep 17 00:00:00 2001 From: Christopher Scott Date: Wed, 30 Jul 2025 16:42:03 -0500 Subject: [PATCH] improve performance of BinaryData construction --- examples/Chat/Example08_ChatSerialization.cs | 2 +- src/Custom/Embeddings/EmbeddingClient.cs | 6 +++--- src/Custom/Moderations/ModerationClient.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/Chat/Example08_ChatSerialization.cs b/examples/Chat/Example08_ChatSerialization.cs index 2fd2df195..da3467cc4 100644 --- a/examples/Chat/Example08_ChatSerialization.cs +++ b/examples/Chat/Example08_ChatSerialization.cs @@ -39,7 +39,7 @@ public static BinaryData SerializeMessages(IEnumerable messages) writer.WriteEndArray(); writer.Flush(); - return BinaryData.FromBytes(stream.ToArray()); + return BinaryData.FromBytes(stream.GetBuffer().AsMemory(0, (int)stream.Length)); } #endregion diff --git a/src/Custom/Embeddings/EmbeddingClient.cs b/src/Custom/Embeddings/EmbeddingClient.cs index cf99c7405..f58018a50 100644 --- a/src/Custom/Embeddings/EmbeddingClient.cs +++ b/src/Custom/Embeddings/EmbeddingClient.cs @@ -217,7 +217,7 @@ private void CreateEmbeddingGenerationOptions(string input, ref EmbeddingGenerat writer.WriteStringValue(input); writer.Flush(); - options.Input = BinaryData.FromBytes(stream.ToArray()); + options.Input = BinaryData.FromBytes(stream.GetBuffer().AsMemory(0, (int)stream.Length)); options.Model = _model; options.EncodingFormat = InternalCreateEmbeddingRequestEncodingFormat.Base64; } @@ -237,7 +237,7 @@ private void CreateEmbeddingGenerationOptions(IEnumerable inputs, ref Em writer.WriteEndArray(); writer.Flush(); - options.Input = BinaryData.FromBytes(stream.ToArray()); + options.Input = BinaryData.FromBytes(stream.GetBuffer().AsMemory(0, (int)stream.Length)); options.Model = _model; options.EncodingFormat = InternalCreateEmbeddingRequestEncodingFormat.Base64; } @@ -264,7 +264,7 @@ private void CreateEmbeddingGenerationOptions(IEnumerable> i writer.WriteEndArray(); writer.Flush(); - options.Input = BinaryData.FromBytes(stream.ToArray()); + options.Input = BinaryData.FromBytes(stream.GetBuffer().AsMemory(0, (int)stream.Length)); options.Model = _model; options.EncodingFormat = InternalCreateEmbeddingRequestEncodingFormat.Base64; } diff --git a/src/Custom/Moderations/ModerationClient.cs b/src/Custom/Moderations/ModerationClient.cs index e450ec874..beae28782 100644 --- a/src/Custom/Moderations/ModerationClient.cs +++ b/src/Custom/Moderations/ModerationClient.cs @@ -170,7 +170,7 @@ private void CreateModerationOptions(string input, ref ModerationOptions options writer.WriteStringValue(input); writer.Flush(); - options.Input = BinaryData.FromBytes(stream.ToArray()); + options.Input = BinaryData.FromBytes(stream.GetBuffer().AsMemory(0, (int)stream.Length)); options.Model = _model; } @@ -189,7 +189,7 @@ private void CreateModerationOptions(IEnumerable inputs, ref ModerationO writer.WriteEndArray(); writer.Flush(); - options.Input = BinaryData.FromBytes(stream.ToArray()); + options.Input = BinaryData.FromBytes(stream.GetBuffer().AsMemory(0, (int)stream.Length)); options.Model = _model; } }