Table of Contents

Class PooledBuffer<T>

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

Holds every row of a query in pooled arrays until the buffer is disposed.

public sealed class PooledBuffer<T> : IDisposable, IAsyncDisposable

Type Parameters

T

The type of each row.

Inheritance
PooledBuffer<T>
Implements
Inherited Members

Remarks

Made by ToPooledBuffer() on a typed view. The read starts when that method is called, on a background producer, once, even if nobody reads the buffer yet; it may finish before anyone asks for the rows. The rows are reached through Items, once the read is done: the buffer is not readable while it fills. To read while the query is still running, use ToPooledBufferStream(CancellationToken) or ToPooledStream(CancellationToken). The connection ODX opened is closed by the query when the read ends, which is before the result is complete: slow readers never hold it.

While the buffer is not disposed its rows stay reachable, and every PooledArray<T> it hands out is valid. The arrays go back to the pool when the buffer is disposed, and not before. Dispose the buffer (await using) when you are done. There is no finalizer: a buffer that is never disposed is not a leak, but it holds its arrays (and the finished read) until the garbage collector takes it, and the pool does not get them back.

Returning an array to a custom pool must not throw. If it does, every other array is still returned, and the first exception propagates from DisposeAsync(), and from Dispose() when the read had already finished; from Dispose() while the read is still running it ends in the producer's task, which nobody observes. With clearing on, a large array is cleared and returned on an internal background queue, off the path of the caller that releases it; when that queue is full, the caller that releases it clears and returns it instead. A return that throws on the background queue is swallowed.

When the rows are to be cleared (see UsePool), they are cleared as the arrays go back to the pool, and that happens when the buffer, or the PooledArray<T> view of it, is disposed (not when the read completes).

Disposing the buffer while it is still reading cancels the query, and waits (DisposeAsync()) for the provider call in progress to return: a provider that ignores cancellation delays the dispose by that call. After the read finished, disposing only releases the arrays.

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

Items

Gets a task that completes with the rows, once the read is done.

Methods

Dispose()

Cancels the read if it is still running, and releases the buffer.

DisposeAsync()

Cancels the read if it is still running, waits for the producer to exit, and returns the arrays to the pool.

From(ReadOnlySpan<T>)

Makes a finished buffer from rows you already have, as if a read had produced them.

From(ReadOnlySpan<T>, ArrayPool<T>, bool)

Makes a finished buffer from rows you already have, with an array from a pool you choose.