Skip to content

Commit c4b6140

Browse files
committed
avoid a fixed stackalloc
1 parent 8cffce1 commit c4b6140

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/StackExchange.Redis/KeyNotification.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,16 @@ internal static RedisValue ExtractLengthPrefixedValue(in RedisValue value, int o
332332
}
333333

334334
// Slower path for non-contiguous values
335-
Span<byte> buffer = stackalloc byte[256];
335+
const int MAX_STACK = 256;
336+
byte[]? lease = null;
337+
var maxCount = value.GetMaxByteCount();
338+
Span<byte> buffer = maxCount <= MAX_STACK
339+
? stackalloc byte[MAX_STACK]
340+
: (lease = ArrayPool<byte>.Shared.Rent(maxCount));
336341
var bytesWritten = value.CopyTo(buffer);
337-
return ExtractLengthPrefixedValue(buffer.Slice(offset, bytesWritten - offset));
342+
var result = ExtractLengthPrefixedValue(buffer.Slice(offset, bytesWritten - offset));
343+
if (lease is not null) ArrayPool<byte>.Shared.Return(lease);
344+
return result;
338345
}
339346

340347
internal static RedisValue ExtractLengthPrefixedValue(ReadOnlySpan<byte> span)

0 commit comments

Comments
 (0)