Class PooledArray<T>
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
The complete result of a PooledBuffer<T>: a read-only list over its pooled arrays.
public sealed class PooledArray<T> : IReadOnlyList<T>, IReadOnlyCollection<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IDisposable
Type Parameters
TThe type of each row.
- Inheritance
-
PooledArray<T>
- Implements
-
ICollection<T>IEnumerable<T>
- Inherited Members
Remarks
It is also a read-only ICollection<T>
(explicitly: Add, Clear and Remove throw
NotSupportedException).
It is valid until it is disposed. Disposing it disposes the
buffer it came from, which returns the arrays to the pool (and,
if the rows are to be cleared (see UsePool), clears
the rows: that is when the clear takes effect).
After that, every member throws
ObjectDisposedException, an enumeration in
progress included, except Dispose(), which does
nothing again and does not throw.
The one exception is the view of a read that found no rows, Empty:
it holds nothing and has no buffer to dispose, so its members work after the buffer that gave it is disposed.
It is not safe to use while another thread disposes it.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Properties
- Count
Gets the number of rows.
- Empty
Gets the view of no rows: one shared instance per row type, which a read that found no rows completes with.
- this[int]
Gets the row at the specified index.
Methods
- CopyTo(Span<T>)
Copies every row, in order, to the span.
- Dispose()
Disposes the buffer this came from: the arrays go back to the pool.
- GetEnumerator()
Enumerates the rows in order.
- ToList()
Copies every row, in order, into a new list sized to the rows.