Method UsePool
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
UsePool(ArrayPool<T>, bool)
Chooses the pool this query's pooled arrays are rented from, and whether their rows are cleared when they go back.
public MappedQueryWithSkip<T> UsePool(ArrayPool<T> pool, bool clearOnReturn)
Parameters
poolArrayPool<T>The pool to rent from and give back to. It is yours: ODX never disposes it. Shared is what a query without
UsePooluses.clearOnReturnbooltrue to clear the used rows of every array before it goes back, value rows included; otherwise, false, which does not clear rows that hold no references, whatever the app-wide switch says.
Returns
- MappedQueryWithSkip<T>
The same query, renting from
pool.
Remarks
Call it after Map (the pool is typed by the row).
Both arguments are required: .UsePool(ArrayPool<Post>.Shared, clearOnReturn: true).
clearOnReturn controls the rows that hold no references;
rows that hold references are always cleared.
An explicit clearOnReturn: false wins over the app-wide switch for this
query, and an explicit clearOnReturn: true clears whatever the switch
says.
It applies to every terminal of this query that rents pooled arrays, the synchronous ones of SyncMappedViewExtensions included:
- ToArrayAsync(CancellationToken)
- ToListAsync(CancellationToken)
- ToImmutableArrayAsync(CancellationToken)
- ToHashSetAsync(IEqualityComparer<T>?, CancellationToken)
- ToDictionaryAsync<TKey>(Func<T, TKey>, IEqualityComparer<TKey>?, CancellationToken)
- ToDictionaryAsync<TKey, TValue>(Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>?, CancellationToken)
- ToPooledBuffer(CancellationToken)
- ToPooledBufferStream(CancellationToken)
- ToPooledStream(CancellationToken)
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.
Exceptions
- ArgumentNullException
poolis null.