Skip to content

Commit

Permalink
Rename MemoryRental to SpanOwner
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jan 13, 2024
1 parent 5759bd3 commit 815c03c
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/DotNext.IO/Buffers/BufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static long Format<T>(this IBufferWriter<byte> writer, T value, in Encodi
where T : notnull, ISpanFormattable
{
// attempt to allocate char buffer on the stack
Span<char> charBuffer = stackalloc char[MemoryRental<char>.StackallocThreshold];
Span<char> charBuffer = stackalloc char[SpanOwner<char>.StackallocThreshold];
if (!TryFormat(writer, value, charBuffer, in context, lengthFormat, format, provider, out var bytesWritten))
{
for (var charBufferSize = charBuffer.Length << 1; ; charBufferSize = charBufferSize <= MaxBufferSize ? charBufferSize * 2 : throw new InsufficientMemoryException())
Expand Down
6 changes: 3 additions & 3 deletions src/DotNext.IO/Buffers/EncodingInterpolatedStringHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void AppendFormatted<T>(T value, string? format = null)
case ISpanFormattable:
for (bufferSize = 0; ; bufferSize = bufferSize <= MaxBufferSize ? bufferSize * 2 : throw new InsufficientMemoryException())
{
using var tempBuffer = bufferSize <= charBuffer.Length ? charBuffer : new MemoryRental<char>(bufferSize, false);
using var tempBuffer = bufferSize <= charBuffer.Length ? charBuffer : new SpanOwner<char>(bufferSize, false);

// constrained call avoiding boxing for value types
if (((ISpanFormattable)value).TryFormat(tempBuffer.Span, out charsWritten, format, provider))
Expand Down Expand Up @@ -140,7 +140,7 @@ private void AppendFormatted(ReadOnlySpan<char> value, int alignment, bool leftA
return;
}

using var tempBuffer = alignment <= charBuffer.Length ? charBuffer.Slice(0, alignment) : new MemoryRental<char>(alignment);
using var tempBuffer = alignment <= charBuffer.Length ? charBuffer.Slice(0, alignment) : new SpanOwner<char>(alignment);
var span = tempBuffer.Span;
var filler = leftAlign
? span.Slice(value.Length, padding)
Expand Down Expand Up @@ -230,7 +230,7 @@ public void AppendFormatted<T>(T value, int alignment, string? format = null)
case ISpanFormattable:
for (bufferSize = alignment; ; bufferSize = bufferSize <= MaxBufferSize ? bufferSize * 2 : throw new InsufficientMemoryException())
{
using var tempBuffer = bufferSize <= charBuffer.Length ? charBuffer : new MemoryRental<char>(bufferSize, false);
using var tempBuffer = bufferSize <= charBuffer.Length ? charBuffer : new SpanOwner<char>(bufferSize, false);
var span = tempBuffer.Span;

if (((ISpanFormattable)value).TryFormat(span, out charsWritten, format, provider))
Expand Down
2 changes: 1 addition & 1 deletion src/DotNext.IO/IO/DecodingTextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public override int Read(Span<char> buffer)
var newLineBufferPosition = 0;
var newLine = Environment.NewLine.AsSpan();

var result = new BufferWriterSlim<char>(stackalloc char[MemoryRental<char>.StackallocThreshold], allocator);
var result = new BufferWriterSlim<char>(stackalloc char[SpanOwner<char>.StackallocThreshold], allocator);
try
{
do
Expand Down
2 changes: 1 addition & 1 deletion src/DotNext.IO/IO/IAsyncBinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ internal static async ValueTask<int> FormatAsync<T>(IAsyncBinaryWriter writer, T
where T : notnull, IUtf8SpanFormattable
{
const int maxBufferSize = int.MaxValue / 2;
for (var bufferSize = MemoryRental<byte>.StackallocThreshold; ; bufferSize = bufferSize <= maxBufferSize ? bufferSize << 1 : throw new InternalBufferOverflowException())
for (var bufferSize = SpanOwner<byte>.StackallocThreshold; ; bufferSize = bufferSize <= maxBufferSize ? bufferSize << 1 : throw new InternalBufferOverflowException())
{
using var buffer = Memory.AllocateAtLeast<byte>(bufferSize);

Expand Down
4 changes: 2 additions & 2 deletions src/DotNext.IO/IO/Pipelines/PipeExtensions.Readers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ static TResult Parse(PipeReader reader, TArg arg, ReadOnlySpanFunc<byte, TArg, T
if (source.TryGetBlock(length, out var block))
return parser(block.Span, arg);

using var destination = (uint)length <= (uint)MemoryRental<byte>.StackallocThreshold
using var destination = (uint)length <= (uint)SpanOwner<byte>.StackallocThreshold
? stackalloc byte[length]
: new MemoryRental<byte>(length);
: new SpanOwner<byte>(length);

source.CopyTo(destination.Span, out length);
return parser(destination.Span.Slice(0, length), arg);
Expand Down
2 changes: 1 addition & 1 deletion src/DotNext.IO/IO/StreamExtensions.Writers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static async ValueTask<long> FormatAsync<T>(this Stream stream, T value,

const int maxBufferSize = int.MaxValue / 2;

for (var charBufferSize = MemoryRental<char>.StackallocThreshold; ; charBufferSize = charBufferSize <= maxBufferSize ? charBufferSize * 2 : throw new InsufficientMemoryException())
for (var charBufferSize = SpanOwner<char>.StackallocThreshold; ; charBufferSize = charBufferSize <= maxBufferSize ? charBufferSize * 2 : throw new InsufficientMemoryException())
{
using var owner = allocator.AllocateAtLeast(charBufferSize);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace DotNext.Buffers;

public sealed class MemoryRentalTests : Test
public sealed class SpanOwnerTests : Test
{
[Fact]
public static unsafe void StackAllocationTest()
{
using MemoryRental<int> vector = stackalloc int[4];
using SpanOwner<int> vector = stackalloc int[4];
False(vector.IsEmpty);
Equal(4, vector.Length);
Equal(4, vector.Span.Length);
Expand All @@ -22,7 +22,7 @@ public static unsafe void StackAllocationTest()
Equal(40, vector.Span[3]);
}

private static void MemoryAccess(in MemoryRental<int> vector)
private static void MemoryAccess(in SpanOwner<int> vector)
{
False(vector.IsEmpty);
vector[0] = 10;
Expand All @@ -38,7 +38,7 @@ private static void MemoryAccess(in MemoryRental<int> vector)
[Fact]
public static void ArrayAllocation()
{
using var vector = new MemoryRental<int>(4);
using var vector = new SpanOwner<int>(4);
Equal(4, vector.Length);
Equal(4, vector.Span.Length);
MemoryAccess(vector);
Expand All @@ -47,7 +47,7 @@ public static void ArrayAllocation()
[Fact]
public static void MemoryAllocation()
{
using var vector = new MemoryRental<int>(MemoryPool<int>.Shared, 4);
using var vector = new SpanOwner<int>(MemoryPool<int>.Shared, 4);
Equal(4, vector.Length);
Equal(4, vector.Span.Length);
MemoryAccess(vector);
Expand All @@ -56,15 +56,15 @@ public static void MemoryAllocation()
[Fact]
public static void MemoryAllocationDefaultSize()
{
using var vector = new MemoryRental<int>(MemoryPool<int>.Shared);
using var vector = new SpanOwner<int>(MemoryPool<int>.Shared);
MemoryAccess(vector);
}

[Fact]
public static unsafe void WrapArray()
{
int[] array = { 10, 20 };
using var rental = new MemoryRental<int>(array);
using var rental = new SpanOwner<int>(array);
False(rental.IsEmpty);
fixed (int* ptr = rental)
{
Expand All @@ -76,7 +76,7 @@ public static unsafe void WrapArray()
[Fact]
public static void Default()
{
var rental = new MemoryRental<int>(Array.Empty<int>());
var rental = new SpanOwner<int>(Array.Empty<int>());
True(rental.IsEmpty);
Equal(0, rental.Length);
True(rental.Span.IsEmpty);
Expand Down
2 changes: 1 addition & 1 deletion src/DotNext.Tests/SpanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public static void SplitSpanByLength()
[Fact]
public static void SwapElements()
{
Span<byte> expected = RandomBytes(MemoryRental<byte>.StackallocThreshold * 4 + 2);
Span<byte> expected = RandomBytes(SpanOwner<byte>.StackallocThreshold * 4 + 2);
Span<byte> actual = expected.ToArray();
var midpoint = actual.Length >> 1;
actual.Slice(0, midpoint).Swap(actual.Slice(midpoint));
Expand Down
4 changes: 2 additions & 2 deletions src/DotNext/Buffers/Binary/IBinaryFormattable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public static TSelf Parse(in ReadOnlySequence<byte> source)
[MethodImpl(MethodImplOptions.NoInlining)]
static TSelf ParseSlow(in ReadOnlySequence<byte> input)
{
using var buffer = (uint)TSelf.Size <= (uint)MemoryRental<byte>.StackallocThreshold
using var buffer = (uint)TSelf.Size <= (uint)SpanOwner<byte>.StackallocThreshold
? stackalloc byte[TSelf.Size]
: new MemoryRental<byte>(TSelf.Size);
: new SpanOwner<byte>(TSelf.Size);

input.CopyTo(buffer.Span, out var writtenCount);
return TSelf.Parse(buffer.Span.Slice(0, writtenCount));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ namespace DotNext.Buffers;
/// <example>
/// <code>
/// const int stackallocThreshold = 20;
/// var memory = size &lt;=stackallocThreshold ? new MemoryRental&lt;byte&gt;(stackalloc byte[stackallocThreshold], size) : new MemoryRental&lt;byte&gt;(size);
/// var memory = size &lt;=stackallocThreshold ? new SpanOwner&lt;byte&gt;(stackalloc byte[stackallocThreshold], size) : new SpanOwner&lt;byte&gt;(size);
/// </code>
/// </example>
/// <typeparam name="T">The type of the elements in the rented memory.</typeparam>
[StructLayout(LayoutKind.Auto)]
public ref struct MemoryRental<T>
public ref struct SpanOwner<T>
{
/// <summary>
/// Global recommended number of elements that can be allocated on the stack.
Expand All @@ -46,7 +46,7 @@ public ref struct MemoryRental<T>
/// </summary>
/// <param name="span">The span that references the memory to rent.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MemoryRental(Span<T> span)
public SpanOwner(Span<T> span)
{
memory = span;
owner = null;
Expand All @@ -57,7 +57,7 @@ public MemoryRental(Span<T> span)
/// </summary>
/// <param name="span">The span that references the memory to rent.</param>
/// <param name="length">The actual length of the data.</param>
public MemoryRental(Span<T> span, int length)
public SpanOwner(Span<T> span, int length)
: this(span.Slice(0, length))
{
}
Expand All @@ -70,7 +70,7 @@ public MemoryRental(Span<T> span, int length)
/// <param name="exactSize"><see langword="true"/> to return the buffer of <paramref name="minBufferSize"/> length; otherwise, the returned buffer is at least of <paramref name="minBufferSize"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="pool"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="minBufferSize"/> is less than or equal to zero.</exception>
public MemoryRental(MemoryPool<T> pool, int minBufferSize, bool exactSize = true)
public SpanOwner(MemoryPool<T> pool, int minBufferSize, bool exactSize = true)
{
ArgumentNullException.ThrowIfNull(pool);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(minBufferSize);
Expand All @@ -87,7 +87,7 @@ public MemoryRental(MemoryPool<T> pool, int minBufferSize, bool exactSize = true
/// </summary>
/// <param name="pool">The memory pool.</param>
/// <exception cref="ArgumentNullException"><paramref name="pool"/> is <see langword="null"/>.</exception>
public MemoryRental(MemoryPool<T> pool)
public SpanOwner(MemoryPool<T> pool)
{
ArgumentNullException.ThrowIfNull(pool);
var owner = pool.Rent();
Expand All @@ -101,7 +101,7 @@ public MemoryRental(MemoryPool<T> pool)
/// <param name="minBufferSize">The minimum size of the memory to rent.</param>
/// <param name="exactSize"><see langword="true"/> to return the buffer of <paramref name="minBufferSize"/> length; otherwise, the returned buffer is at least of <paramref name="minBufferSize"/>.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="minBufferSize"/> is less than or equal to zero.</exception>
public MemoryRental(int minBufferSize, bool exactSize = true)
public SpanOwner(int minBufferSize, bool exactSize = true)
{
var owner = ArrayPool<T>.Shared.Rent(minBufferSize);
memory = exactSize ? new(owner, 0, minBufferSize) : new(owner);
Expand All @@ -125,7 +125,7 @@ public MemoryRental(int minBufferSize, bool exactSize = true)
/// </summary>
/// <param name="span">The allocated memory to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator MemoryRental<T>(Span<T> span)
public static implicit operator SpanOwner<T>(Span<T> span)
=> new(span);

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/DotNext/RandomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace DotNext;

using Numerics;
using UInt32LocalBuffer = Buffers.MemoryRental<uint>;
using UInt32LocalBuffer = Buffers.SpanOwner<uint>;

/// <summary>
/// Provides random data generation.
Expand Down
10 changes: 5 additions & 5 deletions src/DotNext/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ private static void SwapCore<T>(Span<T> x, Span<T> y)
{
Debug.Assert(x.Length == y.Length);

var bufferSize = Math.Min(MemoryRental<T>.StackallocThreshold, x.Length);
var bufferSize = Math.Min(SpanOwner<T>.StackallocThreshold, x.Length);
if (bufferSize is 0)
{
return;
Expand Down Expand Up @@ -664,8 +664,8 @@ static void SwapCore(Span<T> span, int start1, int length1, int start2, int leng
Debug.Assert(sourceSmall.Length == destSmall.Length);

// prepare buffer
MemoryRental<T> buffer;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>() || sourceLarge.Length > MemoryRental<T>.StackallocThreshold)
SpanOwner<T> buffer;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>() || sourceLarge.Length > SpanOwner<T>.StackallocThreshold)
{
buffer = new(sourceLarge.Length);
}
Expand Down Expand Up @@ -754,8 +754,8 @@ static void MoveCore(Span<T> span, int sourceIndex, int destinationIndex, int le
}

// prepare buffer
MemoryRental<T> buffer;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>() || length > MemoryRental<T>.StackallocThreshold)
SpanOwner<T> buffer;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>() || length > SpanOwner<T>.StackallocThreshold)
{
buffer = new(length);
}
Expand Down
10 changes: 5 additions & 5 deletions src/cluster/DotNext.Net.Cluster/Net/EndPointFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ private static UnixDomainSocketEndPoint DeserializeDomainSocket(ref SequenceRead
{
var length = reader.ReadLittleEndian<int>();

using var pathBuffer = (uint)length <= (uint)MemoryRental<byte>.StackallocThreshold
using var pathBuffer = (uint)length <= (uint)SpanOwner<byte>.StackallocThreshold
? stackalloc byte[length]
: new MemoryRental<byte>(length, true);
: new SpanOwner<byte>(length, true);

reader.Read(pathBuffer.Span);
return new(HostNameEncoding.GetString(pathBuffer.Span));
Expand All @@ -182,9 +182,9 @@ private static UriEndPoint DeserializeUri(ref SequenceReader reader)
{
var length = reader.ReadLittleEndian<int>();

using var pathBuffer = (uint)length <= (uint)MemoryRental<byte>.StackallocThreshold
using var pathBuffer = (uint)length <= (uint)SpanOwner<byte>.StackallocThreshold
? stackalloc byte[length]
: new MemoryRental<byte>(length, true);
: new SpanOwner<byte>(length, true);

reader.Read(pathBuffer.Span);
return new(new Uri(HostNameEncoding.GetString(pathBuffer.Span), UriKind.Absolute));
Expand All @@ -207,7 +207,7 @@ private static void DeserializeHost(ref SequenceReader reader, out string hostNa
family = (AddressFamily)reader.ReadLittleEndian<int>();
var length = reader.ReadLittleEndian<int>();

using var hostNameBuffer = (uint)length <= (uint)MemoryRental<byte>.StackallocThreshold ? stackalloc byte[length] : new MemoryRental<byte>(length, true);
using var hostNameBuffer = (uint)length <= (uint)SpanOwner<byte>.StackallocThreshold ? stackalloc byte[length] : new SpanOwner<byte>(length, true);
reader.Read(hostNameBuffer.Span);
hostName = HostNameEncoding.GetString(hostNameBuffer.Span);
}
Expand Down

0 comments on commit 815c03c

Please sign in to comment.