Table of Contents

Method ToPooledBufferStream

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.Core.dll

ToPooledBufferStream(CancellationToken)

Reads the query in a rolling stream of pooled arrays, once, for one consumer: it buffers the result in pooled arrays (from an array pool), not a channel of rows.

public IAsyncEnumerable<ReadOnlyMemory<T>> ToPooledBufferStream(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancels the read: a consumer then sees the cancel after what was already delivered.

Returns

IAsyncEnumerable<ReadOnlyMemory<T>>

The lazy, single-use stream.

Remarks

Lazy: nothing executes until the first MoveNextAsync(). The stream can be enumerated once: a second GetAsyncEnumerator(CancellationToken) throws InvalidOperationException.

Each ReadOnlyMemory<T> holds exactly the rows of one chunk and is valid until the next MoveNextAsync() or the end of the enumeration, when its array goes back to the pool: copy what you keep. Nothing guards a memory held longer (it then reads rows that were reused), so do not buffer the chunks themselves, for example with ToListAsync or Buffer. An enumerator that is never disposed never returns its arrays (there is no finalizer): they go to the garbage collector. A producer reads ahead without waiting for the consumer (the queue is unbounded), so the connection ODX opened is closed when the read ends, not when the consumer is done, while memory grows with the backlog: a slow consumer lets chunks pile up, as with an unbounded channel.

Disposing the enumerator, which a break out of await foreach always does, cancels the query and returns every array still queued.

Use it for sequential, read-only access to the elements through the buffers. ToPooledStream(CancellationToken) is the same without buffers; ToPooledBuffer(CancellationToken) keeps the rows as a list.

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.

Here the clearing happens as each chunk goes back, once the consumer is done with it.