Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Don't try to copy the blob builder's contents to a stack-allocated bu…
…ffer.

The blob builder's default starting size is 256 bytes; the same as the stack-alllocated buffer, and making it bigger does not have any benefit since such large blobs are rare.
  • Loading branch information
teo-tsirpanis committed Apr 11, 2023
commit 9c62c03c156c136afa6bfb66fa06fcaf1287c51a
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public ImmutableArray<byte> ToImmutableArray(int start, int byteCount)
return ImmutableByteArrayInterop.DangerousCreateFromUnderlyingArray(ref array);
}

internal bool TryWriteTo(Span<byte> scratchBuffer, out ReadOnlySpan<byte> buffer)
internal bool TryGetSpan(out ReadOnlySpan<byte> buffer)
{
if (_nextOrPrevious == this)
{
Expand All @@ -328,20 +328,6 @@ internal bool TryWriteTo(Span<byte> scratchBuffer, out ReadOnlySpan<byte> buffer
return true;
}

int count = Count;
if (scratchBuffer.Length <= count)
{
int i = 0;
foreach (var chunk in GetChunks())
{
chunk.Span.CopyTo(scratchBuffer.Slice(i));
i += chunk.Length;
}

buffer = scratchBuffer.Slice(0, count);
return true;
}

buffer = default;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public BlobHandle GetOrAddBlob(BlobBuilder value)
Throw.ArgumentNull(nameof(value));
}

if (value.TryWriteTo(stackalloc byte[256], out ReadOnlySpan<byte> buffer))
if (value.TryGetSpan(out ReadOnlySpan<byte> buffer))
{
return GetOrAddBlob(buffer);
}
Expand Down