Skip to content

Benchmarks: the copy-out collectors against a growing list, a builder and a stack scratch - #288

Merged
electricessence merged 2 commits into
masterfrom
bench/copy-out-collectors
Dec 2, 2026
Merged

electricessence merged 2 commits into
masterfrom
bench/copy-out-collectors

Conversation

@electricessence

@electricessence electricessence commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

A BenchmarkDotNet class family for the copy-out collectors, to find any small-count loss of the chunk chain with its clear against the hand-written ways. Build-only here: nothing was run as a timed benchmark (that needs a quiet machine), and no BenchmarkDotNet run was made.

What it measures

CopyOutInt, CopyOutSmallStruct (a 16-byte struct) and CopyOutClass (a class with a string), each over an in-memory DataTableReader (no database) at 0, 1, 5, 10, 100, 1,000 and 100,000 rows (ODX_COPYOUT_ROWS changes them). Every variant reads through the same selector delegate and returns its collection, so the harness consumes it; the memory diagnoser is on.

Variant Sync Async
the library's ToList, ToArray, ToImmutableArray (chunk chain, one copy, chunks cleared) yes yes
the same chunk chain, copy and clear in a raw read loop, no Map view and none of the terminal's plumbing (RawChain; async: ToList only) yes ToList
a List<T> that grows (the baseline of its group) yes yes
that list, then ToArray() yes yes
ImmutableArray.CreateBuilder<T>() of unknown capacity, then ToImmutable() yes yes
a stack scratch of 8 rows in front of the pooled chain (a prototype, in the benchmark file only) yes no

Results are grouped by sync and async and by row count, each group against its own growing-list baseline. The hand-written async variants return ValueTask, as the library's do, so no Task is allocated on one side only. What the table cannot hide, and the report should say: the chain and the scratch rent and clear pooled chunks, the hand-written ones do neither. The library variants answer "what users get"; the raw-chain variants use the same read loop as the growing list, so against it they isolate the collection strategy, and a library variant minus its raw-chain variant is what the terminal path itself costs (the Map view, the abandon bookkeeping).

The scratch variant is synchronous only: a ref struct cannot live across an await. It uses the library's own chunk schedule (16 doubling to 8,192) and clears the used part of every segment on the way back (the copy-out rule), and holds at most 27 segments (about 155,000 rows; more throws), as the rows stop at 100,000.

System.Linq's segmented builder, for the scratch variant

Read in dotnet/runtime, src/libraries/System.Linq/src/System/Linq/SegmentedArrayBuilder.cs, at the tags v10.0.15 (the latest release) and v11.0.0; the file is identical at both. Enumerable.ToArray of a source of unknown length uses it (ToCollection.cs, same tags). No code is copied.

  • Segments from a pool: yes. Each new segment is rented from ArrayPool<T>.Shared, at least 16 elements and at least double the previous one, held in an inline array of 27 slots.
  • Clear on return: by the type. A type that holds references has its segments cleared before they are returned (full segments entirely, the last one up to its count); a type without references is returned without clearing. So it is the old rule of this library's buffers, not the new copy-out rule.
  • Inline scratch for the first items: yes. The caller passes a stack buffer of 8 elements (ScratchBufferSize = 8, an InlineArray(8) local); the first 8 items go there and nothing is rented until the ninth.

The raw-chain variants need the benchmark assembly to see Core's internals: one InternalsVisibleTo line in the Core project (owner-approved), with its comment. Nothing public changes.

The check

--verify-copyout (no BenchmarkDotNet, nothing measured) runs every variant for each row kind at 0, 1, 5, 8, 9, 10, 16, 24, 25, 100, 1,000, 8,200 and 100,000 rows (the new raw-chain variants included), twice each (the second call reuses the pooled arrays of the first), and compares the rows with the growing list, and the exact capacity of the chain's lists. On this branch: "copy-out variants: all equal".

Checks

  • Release and Debug builds, 0 warnings and 0 errors; Core tests and the samples pass; the Visual Studio solution build has no errors; dotnet format style and analyzers at --severity info report nothing on all 14 projects; the docs build is clean (a benchmark-only change, run through the whole gate anyway).
  • Two Haiku reviewers, arbitrated (benchmark fairness, and the scratch prototype with the check). Fixed: the hand-written async variants allocated a Task the library's do not; one baseline across sync and async made the async ratios misleading; the scratch prototype overran its 27 slots past about 155,000 rows and kept state after Dispose; the capacity check skipped the scratch list. Dropped: a claim that the inline array does not convert to a span (it compiles and the check runs).

🤖 Generated with Claude Code

electricessence and others added 2 commits December 1, 2026 19:47
… and a stack scratch

A new BenchmarkDotNet class family (`CopyOutInt`, `CopyOutSmallStruct`, `CopyOutClass`, one per row kind: an `int`, a 16-byte struct, a class with a string) for the question "does the chunk chain with its clear lose to the hand-written ways at small counts?". Over an in-memory `DataTableReader` (no database), at 0, 1, 5, 10, 100, 1,000 and 100,000 rows (`ODX_COPYOUT_ROWS` changes them), every variant through the same selector delegate, synchronous and asynchronous, each result returned so the harness consumes it, with the memory diagnoser:

- the library's `ToList`, `ToArray` and `ToImmutableArray` (chain plus clear);
- a `List<T>` that grows, that list then `ToArray()`, and `ImmutableArray.CreateBuilder<T>()` of unknown capacity then `ToImmutable()`;
- a prototype, in the benchmark file only, of a stack scratch buffer of 8 rows in front of the chain (synchronous only; a ref struct cannot live across an await), modelled on `System.Linq`'s internal segmented builder, so the timed run can say whether it is worth adopting.

`--verify-copyout` checks, without BenchmarkDotNet, that every variant gives the same rows as the growing list for each row kind and counts around the scratch and chunk edges, and that the chain's lists are exactly sized.

The results are grouped by sync and async and by row count, each group against its own growing-list baseline; the hand-written async variants return `ValueTask`, as the library's do, so no `Task` is allocated on one side only.

Not run as timed benchmarks by this change: that needs a quiet machine.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…out of the comparison

The chain variants went through `reader.Map(selector).ToList()`, the library's terminal path (the `Map` view, the abandon bookkeeping, the plumbing), while the growing list, the builder and the scratch used a raw read loop, so the comparison mixed the terminal overhead with the collection strategy. Added `Sync_RawChain_ToList`, `ToArray` and `ToImmutableArray` and `Async_RawChain_ToList`: the same read loop as the growing list, rows into the real chunk chain with the default configuration (copy-out clearing on), then the chain's own `ToList`, `ToArray` or the immutable build. The library variants stay, as they are what a user gets; the difference between a library variant and its raw-chain variant is the terminal's cost.

The benchmark assembly now sees Core's internals (`InternalsVisibleTo`, one line in the Core project, with its comment); nothing public changes. `--verify-copyout` covers the new variants, including the exact capacity of their lists.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit 1417e54 into master Dec 2, 2026
9 checks passed
@electricessence
electricessence deleted the bench/copy-out-collectors branch December 2, 2026 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant