Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added extension method similar to how the CommunityToolkit.HighPerfor…
…mance project handles the creation of MemoryStreams without an allocation. Restored the use of MemoryMarshal, but throws an exception if the data cannot be accessed now, instead of hanging as it did in a previous iteration.

Tested both paths (string and stream) from example project successfully.

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Jan 15, 2025
commit d65037ebda90cdba01bbc60cdec3782cf2ac56ad
8 changes: 2 additions & 6 deletions src/Dapr.Client/DaprClientGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1670,9 +1670,7 @@ public override async Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResour
ReadOnlyMemory<byte> plaintextBytes, string keyName, EncryptionOptions encryptionOptions,
CancellationToken cancellationToken = default)
{
var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(plaintextBytes, cancellationToken);
memoryStream.Position = 0;
var memoryStream = plaintextBytes.CreateMemoryStream(true);
Comment thread
WhitWaldo marked this conversation as resolved.
Outdated

var encryptionResult =
await EncryptAsync(vaultResourceName, memoryStream, keyName, encryptionOptions, cancellationToken);
Expand Down Expand Up @@ -1891,9 +1889,7 @@ public override async Task<ReadOnlyMemory<byte>> DecryptAsync(string vaultResour
ReadOnlyMemory<byte> ciphertextBytes, string keyName, DecryptionOptions decryptionOptions,
CancellationToken cancellationToken = default)
{
using var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(ciphertextBytes, cancellationToken);
memoryStream.Position = 0;
using var memoryStream = ciphertextBytes.CreateMemoryStream(true);

var decryptionResult =
await DecryptAsync(vaultResourceName, memoryStream, keyName, decryptionOptions, cancellationToken);
Expand Down
36 changes: 36 additions & 0 deletions src/Dapr.Client/Extensions/ReadOnlyMemoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ------------------------------------------------------------------------
// Copyright 2025 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Dapr.Client;

internal static class ReadOnlyMemoryExtensions
{
public static MemoryStream CreateMemoryStream(this ReadOnlyMemory<byte> memory, bool isReadOnly)
{
if (memory.IsEmpty)
Comment thread
philliphoff marked this conversation as resolved.
{
return new MemoryStream(Array.Empty<byte>(), !isReadOnly);
}

if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
{
return new MemoryStream(segment.Array!, segment.Offset, segment.Count, !isReadOnly);
}

throw new ArgumentException(nameof(memory), "Unable to create MemoryStream from provided memory value");
}
}