RentedListCore 0.2.3
dotnet add package RentedListCore --version 0.2.3
NuGet\Install-Package RentedListCore -Version 0.2.3
<PackageReference Include="RentedListCore" Version="0.2.3" />
<PackageVersion Include="RentedListCore" Version="0.2.3" />
<PackageReference Include="RentedListCore" />
paket add RentedListCore --version 0.2.3
#r "nuget: RentedListCore, 0.2.3"
#:package RentedListCore@0.2.3
#addin nuget:?package=RentedListCore&version=0.2.3
#tool nuget:?package=RentedListCore&version=0.2.3
RentedListCore
Disposable List structure allocating memory from ArrayPool
Usage
RentedList<T>: a disposable struct backed byArrayPool<T>.Shared, with list operations, collection interfaces, andSpan,Memory, andSegmentviews.ValueRentedList<T>: a disposableref structthat starts empty with a scratchSpan<T>and switches to the pool when it outgrows it. Supports the same list operations and span views.
using RentedList<int> rented = [1, 2, 3];
rented.Add(4);
using var value = new ValueRentedList<int>(stackalloc int[4]);
value.AddRange(1, 2, 3, 4);
value.Add(5); // Grows into a rented array.
For both types, Clear() and Dispose() release storage and reset count/capacity to zero.
Copies share storage; avoid disposing multiple copies or retaining views across growth or disposal.
Rented buffers
RentedBuffer<T> is a disposable struct for a fixed rental from ArrayPool<T>.Shared.
The constructor accepts a minimum capacity; Capacity reports the actual rental size.
Array, Span, Memory, and Segment expose the entire rental without copying.
Implicit conversions are available to T[], Span<T>, ReadOnlySpan<T>, Memory<T>,
ReadOnlyMemory<T>, and ArraySegment<T>, along with ref and range indexers.
using var buffer = new RentedBuffer<byte>(1024);
Span<byte> data = buffer;
data.Clear(); // Pooled storage is not guaranteed to be zeroed.
buffer[0] = 42;
Memory<byte> firstKilobyte = buffer.Memory[..1024];
Negative capacities throw; zero capacity and default buffers have empty views.
Dispose() returns the rental and resets capacity to zero; repeated disposal of the
same instance is harmless. Copies share storage, so dispose only one owner and stop
using all borrowed views (including the array) before disposal. Arrays are returned
without clearing their contents.
Stack-or-pool buffers
ValueRentedBuffer<T> is a disposable ref struct for a fixed buffer. Its constructor
accepts a minimum capacity and an optional scratch Span<T>. It borrows the entire
scratch span if large enough; otherwise it rents directly from ArrayPool<T>.Shared,
without copying or modifying the scratch storage. There is no growth.
Allocate stack storage conditionally in the caller so larger requests rent without executing a stack allocation:
ArgumentOutOfRangeException.ThrowIfNegative(length);
const int StackLimit = 1024; // Bytes, since T is byte.
using var buffer = new ValueRentedBuffer<byte>(
length,
length <= StackLimit ? stackalloc byte[length] : default);
Span<byte> data = buffer.Span[..length];
data.Clear(); // Storage is not guaranteed to be zeroed.
Stack storage must be allocated in the caller's scope; a constructor or factory
cannot return a buffer referencing its own stackalloc storage. Choose stack limits
in bytes, accounting for the element size. T is unconstrained, but stackalloc
requires unmanaged elements; reference types can use array-backed scratch spans or rentals.
Omitting the scratch span rents directly for positive capacities.
Capacity and Span expose the entire selected storage, potentially larger than
requested. Ref and range indexers and implicit conversions to Span<T> and
ReadOnlySpan<T> share that storage. Negative capacities throw; zero capacity,
even with scratch storage, and default buffers have empty views.
Dispose() returns only owned rentals, without clearing their contents, and resets
the buffer to empty. Repeated disposal of the same instance is harmless. Copies share
storage: dispose only one owner and stop using borrowed views before disposal.
Caller-owned scratch storage remains owned by the caller.
Benchmarks
Adding elements
Code
Results
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|
| List_Add | 797.3 ns | 185.45 ns | 10.17 ns | 1.00 | 0.02 | 0.4473 | 0.0076 | 8424 B | 1.00 |
| RentedList_Add | 516.5 ns | 61.73 ns | 3.38 ns | 0.65 | 0.01 | 0.2174 | 0.0029 | 4120 B | 0.49 |
| RentedList_Add_Dispose | 467.9 ns | 55.01 ns | 3.02 ns | 0.59 | 0.01 | - | - | - | 0.00 |
Enumerating elements
Code
Results
| Method | Mean | Error | StdDev | Ratio |
|---|---|---|---|---|
| Array_Enumerator | 1.770 us | 0.0873 us | 0.0048 us | 1.00 |
| List_Enumerator | 2.506 us | 0.2307 us | 0.0126 us | 1.42 |
| RentedList_Enumerator | 2.364 us | 0.4903 us | 0.0269 us | 1.34 |
Indexing elements
Code
Results
| Method | Mean | Error | StdDev | Ratio | RatioSD |
|---|---|---|---|---|---|
| Array_Indexer | 1.793 us | 0.4019 us | 0.0220 us | 1.00 | 0.02 |
| List_Indexer | 2.650 us | 0.1405 us | 0.0077 us | 1.48 | 0.02 |
| RentedList_Indexer | 1.915 us | 0.0873 us | 0.0048 us | 1.07 | 0.01 |
Creating collection
Code
Results
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|
| Array_Create | 119.7 ns | 23.92 ns | 1.31 ns | 1.00 | 0.01 | 0.2136 | - | 3.93 KB | 1.00 |
| List_Create | 130.5 ns | 45.66 ns | 2.50 ns | 1.09 | 0.02 | 0.2153 | 0.0031 | 3.96 KB | 1.01 |
| RentedList_Create | 250.2 ns | 23.45 ns | 1.29 ns | 2.09 | 0.02 | 0.4325 | 0.0067 | 7.95 KB | 2.02 |
| RentedList_Create_Dispose | 146.4 ns | 42.75 ns | 2.34 ns | 1.22 | 0.02 | 0.2136 | - | 3.93 KB | 1.00 |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Bcl.Memory (>= 10.0.12)
- System.Collections.Immutable (>= 10.0.12)
- System.Reflection.Metadata (>= 10.0.12)
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on RentedListCore:
| Package | Downloads |
|---|---|
|
DnsCore
.NET lightweight DNS server & client |
GitHub repositories
This package is not used by any popular GitHub repositories.