Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/Core/ChunkChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Open.Database.Extensions.Core;
/// dispose it once, with a <see langword="using"/> declaration; after that it
/// must not be used.
/// The arrays come from, and go back to, the pool of the call's
/// <see cref="ChunkConfig{T}"/>; only the rows that were written are cleared.
/// <see cref="ChunkConfig{T}"/>; only the rows that were written are cleared, by <see cref="ChunkConfig{T}.CopyOutClearOnReturn"/>.
/// </remarks>
/// <include file="Internals.docs.xml" path="docs/members[@name='row']/*" />
// The read runs on the caller's own flow: there is no producer task (that is
Expand Down Expand Up @@ -305,7 +305,7 @@ public void Dispose()
try
{
int used = i == count - 1 ? lastUsed : chunks[i].Length;
config.Seal(chunks[i], used).Release();
config.SealCopyOut(chunks[i], used).Release();
}
catch (Exception ex)
{
Expand Down
47 changes: 37 additions & 10 deletions Source/Core/ChunkConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace Open.Database.Extensions.Core;
/// <typeparam name="T">The type of each row.</typeparam>
/// <param name="pool">The pool the arrays come from and go back to.</param>
/// <param name="clearOnReturn">
/// What was asked: <see langword="null"/> for the app-wide default,
/// <see langword="true"/> or <see langword="false"/> for the caller's choice
/// (see <see cref="ClearOnReturn"/>).
/// What was asked: <see langword="null"/> when no pool was offered,
/// <see langword="true"/> or <see langword="false"/> for the caller's choice.
/// A buffer or a stream resolves it by <see cref="ClearOnReturn"/> (<see langword="null"/>
/// is the app-wide default); a collecting terminal by <see cref="CopyOutClearOnReturn"/>
/// (<see langword="null"/> always clears).
/// </param>
/// <param name="sizes">The chunk sizes of the collectors, buffers and streams.</param>
/// <param name="bin">The recycle bin that clears deferred chunks.</param>
Expand Down Expand Up @@ -65,17 +67,30 @@ internal sealed class ChunkConfig<T>(
/// <summary>Gets the pool the arrays come from and go back to.</summary>
public ArrayPool<T> Pool { get; } = pool;

/// <summary>Gets whether the rows are cleared before an array goes back.</summary>
/// <summary>Gets whether the rows of a buffer or a stream are cleared before an array goes back.</summary>
/// <remarks>
/// A row type that holds references is always cleared, whatever was asked
/// (see <see cref="PoolClearing"/>).
/// The collecting terminals use <see cref="CopyOutClearOnReturn"/> instead.
/// </remarks>
// Resolved once, here, from what was asked (the constructor takes the raw
// intent: null is the app-wide default, true and false are what the caller
// chose), and the floor is enforced here too, not trusted to the caller:
// such rows are never given ClearMode.None.
public bool ClearOnReturn { get; } = PoolClearing.Resolve<T>(clearOnReturn);

/// <summary>
/// Gets whether the rows of a chunk that a collecting terminal rented are
/// cleared before it goes back.
/// </summary>
/// <remarks>
/// A collector copies its rows out, so its chunks are never seen by the
/// caller: with no pool offered it always clears, and with
/// <c>UsePool</c> it follows <c>clearOnReturn</c>
/// (see <see cref="PoolClearing.ResolveCopyOut{T}(bool?)"/>).
/// </remarks>
public bool CopyOutClearOnReturn { get; } = PoolClearing.ResolveCopyOut<T>(clearOnReturn);

/// <summary>Gets the chunk sizes of the collectors, buffers and streams.</summary>
public ChunkSizes Sizes { get; } = sizes;

Expand Down Expand Up @@ -118,23 +133,35 @@ public T[] Rent(int size)
/// <include file="Internals.docs.xml" path="docs/members[@name='used']/*" />
/// <returns>The lease, to be released once.</returns>
public ArrayLease<T> Seal(T[] array, int used)
=> new(array, used, this, ModeFor(used));
=> new(array, used, this, ModeFor(used, ClearOnReturn));

/// <summary>
/// Makes the lease of a chunk of a collecting terminal that holds
/// <paramref name="used"/> rows: the same as <see cref="Seal"/>, but it
/// clears by <see cref="CopyOutClearOnReturn"/>.
/// </summary>
/// <param name="array">The array, rented from <see cref="Pool"/>.</param>
/// <include file="Internals.docs.xml" path="docs/members[@name='used']/*" />
/// <returns>The lease, to be released once.</returns>
public ArrayLease<T> SealCopyOut(T[] array, int used)
=> new(array, used, this, ModeFor(used, CopyOutClearOnReturn));

/// <summary>The size of a row in the array (a reference is a pointer).</summary>
static readonly int RowBytes = Unsafe.SizeOf<T>();

/// <summary>Chooses what happens to the rows of a chunk on release.</summary>
/// <remarks>
/// <see cref="ClearMode.None"/>: only when nothing is to be cleared (no
/// clearing asked and no references to keep alive, see the constructor), or
/// no rows were written.
/// <see cref="ClearMode.None"/>: only when <paramref name="clear"/> is
/// <see langword="false"/> (the caller, <see cref="Seal"/> or <see cref="SealCopyOut"/>,
/// resolved no clearing and no references to keep alive), or no rows were written.
/// <see cref="ClearMode.Deferred"/>: a chunk of at least
/// <see cref="DeferFromClearedBytes"/> bytes of rows, of any kind of row;
/// everything else that clears does it right away.
/// </remarks>
/// <param name="clear">Whether the rows are to be cleared (what the caller of the seal resolved).</param>
/// <include file="Internals.docs.xml" path="docs/members[@name='used']/*" />
ClearMode ModeFor(int used)
=> !ClearOnReturn || used == 0
ClearMode ModeFor(int used, bool clear)
=> !clear || used == 0
? ClearMode.None
: (long)used * RowBytes >= DeferFromClearedBytes
? ClearMode.Deferred
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/RowReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ public Dictionary<TKey, TValue> Build(ChunkChain<T> chain)
// The arrays come from the pool of the ChunkConfig (the shared array pool by default)
// and are as long as the pool makes them: the sink uses all of the array it holds and
// grows by renting one twice as long. A row is one array store.
// ChunkConfig.ClearOnReturn is what PoolClearing decided: the rows that were written
// ChunkConfig.CopyOutClearOnReturn is what PoolClearing decided for a collector: the rows that were written
// are cleared before an array goes back, and nothing else (an array it outgrew is
// released the same way, since the rows were copied out of it).
internal struct ArraySink<T>(ChunkConfig<T> config) : IRowSink<T>
Expand Down Expand Up @@ -968,7 +968,7 @@ void AddSlow(T item)
// The rows were copied: the old array goes back, its rows cleared
// first when the rule says so (they are alive in the new one, so the
// old must not keep them).
if (old is not null) config.Seal(old, rows).Release();
if (old is not null) config.SealCopyOut(old, rows).Release();
}

public readonly T[] ToArray()
Expand All @@ -986,6 +986,6 @@ public void Dispose()
if (items is null) return;

_items = null;
config.Seal(items, Count).Release();
config.SealCopyOut(items, Count).Release();
}
}
2 changes: 2 additions & 0 deletions Source/Core/MappedQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,8 @@ public readonly record struct MappedQuery<T>
/// An explicit <c>clearOnReturn: false</c> wins over the app-wide switch for this
/// query, and an explicit <c>clearOnReturn: true</c> clears whatever the switch
/// says.
/// A query that does not call <c>UsePool</c> has no say, and its collecting terminals
/// (the ones that copy the rows out) clear the value rows too, whatever the switch says.
/// </para>
/// <para>
/// It applies to every terminal of this query that rents pooled arrays,
Expand Down
31 changes: 29 additions & 2 deletions Source/Core/PoolClearing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Open.Database.Extensions.Core;
/// </summary>
/// <remarks>
/// <para>
/// One rule for every pooled array ODX rents (the collectors' chunk chain,
/// the buffers and the streams):
/// One rule for every pooled array ODX rents that the caller can hold (the
/// buffers and the streams):
/// </para>
/// <list type="number">
/// <item>
Expand All @@ -33,6 +33,13 @@ namespace Open.Database.Extensions.Core;
/// <para>
/// The switch is the AppContext setting named by <see cref="SwitchName"/>.
/// </para>
/// <para>
/// The collecting terminals (<c>ToList</c>, <c>ToArray</c>, <c>ToImmutableArray</c>,
/// <c>ToHashSet</c> and the dictionaries, on a view or a reader) copy their rows out
/// and hide their pooling from the caller, so they have a rule of their own
/// (<see cref="ResolveCopyOut{T}(bool?)"/>): with no pool offered they always clear,
/// whatever the switch says; with <c>UsePool</c> they follow <c>clearOnReturn</c>.
/// </para>
/// </remarks>
internal static class PoolClearing
{
Expand Down Expand Up @@ -89,6 +96,26 @@ public static bool Resolve<T>(bool? clear)
public static bool Resolve<T>(bool? clear, bool all)
=> (clear ?? all) || Holds<T>.References;

/// <summary>
/// Determines whether the used rows of a chunk that a collecting terminal
/// rented must be cleared: the terminal copies its rows out, so its chunks
/// are not the caller's to see.
/// </summary>
/// <remarks>
/// What <c>UsePool</c> said wins; with no pool offered the rows are
/// cleared. The app-wide switch plays no part, so it cannot turn this off.
/// </remarks>
/// <include file="Internals.docs.xml" path="docs/members[@name='row']/*" />
/// <param name="clear">
/// What the query said: <see langword="null"/> when it offered no pool.
/// </param>
/// <returns>
/// <see langword="true"/> if the rows must be cleared; otherwise,
/// <see langword="false"/>.
/// </returns>
public static bool ResolveCopyOut<T>(bool? clear)
=> Resolve<T>(clear, all: true);

/// <summary>
/// The floor: whether a row type is, or contains, a reference.
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion Source/Core/PooledMemory.docs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@
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 <c>int</c>, a plain struct) go back as they
Rows with no references (an <c>int</c>, a plain struct) depend on the
terminal.
The collecting terminals (<c>ToList</c>, <c>ToArray</c>, <c>ToImmutableArray</c>,
<c>ToHashSet</c> and the dictionaries, on a view or a reader) copy their rows out
and hide their pooling from the caller: when the query offers no pool
they always clear, whatever the app-wide setting says, and with
<c>UsePool</c> they follow it
(<c>clearOnReturn: true</c> clears, <c>clearOnReturn: false</c> keeps the rows as they are).
The buffers and the streams hand their arrays to the caller, so there the rows go back as they
are, unless asked:
<c>UsePool(pool, clearOnReturn: true)</c> clears them,
<c>clearOnReturn: false</c> keeps them as they are whatever the app-wide setting
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/RecycleBin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Open.Database.Extensions.Core;
/// <remarks>
/// <para>
/// Rows that hold references are always cleared; other rows are cleared when <c>UsePool</c> or
/// the app-wide switch asks for it.
/// the app-wide switch asks for it, and always by a collecting terminal that was offered no pool.
/// A large chunk is cleared here; a smaller one is cleared where it is released.
/// The bin is internal only: nothing outside <see cref="ArrayLease{T}"/> can feed it.
/// It is lazy: no loop exists until the first post.
Expand Down
Loading