Skip to content

Collecting terminals clear their value rows when no pool is offered - #286

Merged
electricessence merged 1 commit into
masterfrom
feature/copy-out-clearing
Dec 2, 2026
Merged

electricessence merged 1 commit into
masterfrom
feature/copy-out-clearing

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

The collecting terminals hide their pooling, so with no pool offered they now always clear the rows they used.

ToList, ToArray, ToImmutableArray, ToHashSet and the dictionaries (asynchronous and synchronous, on a view or on a DbDataReader) copy their rows out and never show their pooled arrays. A query that does not call UsePool now clears the used rows of those arrays before they go back, plain value rows included, and the ClearPooledArrays switch cannot turn that off. UsePool(pool, clearOnReturn) is followed as before (false keeps the rows). Rows that hold references were always cleared. The buffers and the streams keep their rule: references, then the query, then the switch.

How

  • PoolClearing.ResolveCopyOut<T>(bool?) is Resolve<T>(clear, all: true): what UsePool said, otherwise clear, plus the references floor.
  • ChunkConfig gets CopyOutClearOnReturn and SealCopyOut. The chunk chain (every collector on a view and on a reader) and the array sink (the synchronous ToArray) release through it; Seal and the buffer and stream paths are untouched.
  • A chunk of 128 KB or more of rows is still cleared in the background recycle bin, so a large result adds no inline clear to the read path; a smaller one is cleared where it is released.

Cost

Allocation is unchanged: the bytes per call of ToArrayAsync and ToImmutableArrayAsync over an in-memory reader (the probe from the previous change) are the same before and after, at 0, 1, 1,000 and 100,000 rows (304, 336, 28,304 and 400,328 bytes for ToImmutableArrayAsync; 280, 312, 28,280 and 400,304 for ToArrayAsync). I did not run a timing benchmark. The clear is one Span.Clear of the rows that were written, about as much memory traffic as the single copy-out; for int rows the chunks are at most 32 KB, so it is cleared where released (about 8 to 20 ns a KB by the measurements recorded in ChunkConfig), and a result of 100,000 ints clears about 400 KB. The timing of chain plus clear against a List or builder is for the next timed run.

Tests

  • The rule: ResolveCopyOut for null, true and false, for value rows and for rows with references; the buffer and stream rule unchanged.
  • The chunk chain and the array sink, with no pool, UsePool(true) and UsePool(false), cleared in place and by the bin.
  • A row type that holds references is cleared through the chain's copy-out whatever UsePool said.
  • Through a view, asynchronous and synchronous terminals (ToListAsync, ToArrayAsync, ToImmutableArrayAsync, ToHashSetAsync, both ToDictionaryAsync, and the synchronous ToArray, ToImmutableArray, ToDictionary): no pool offered and UsePool(true) leave no row behind; UsePool(false) leaves them. The synchronous ToList grows a list of its own and rents nothing, so it is not in the list. The no-pool cases fail on the old code (checked by putting the old release back at each of the two sites).

Checks

  • Core tests: 15634 pass in Release with the generator on; 15615 pass (16 skipped) in Debug with it off.
  • 231 sample checks 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.
  • Reviewed by three Haiku reviewers (coverage of the release paths, docs consistency, test adequacy), arbitrated. Real findings fixed: three stale statements in the docs, the deck notes and the ChunkConfig comments; a missing no-pool test at the view level; no test of the synchronous terminals; a reference-rows test that never reached the copy-out release. Dismissed: a UsePool(pool) call without the bool (it does not exist, both arguments are required).

Docs: pooled memory remarks, the async buffers article, the pooling and clearing article, the deck notes, UsePool, and the release notes ("Changes since rc.16").

🤖 Generated with Claude Code

`ToList`, `ToArray`, `ToImmutableArray`, `ToHashSet` and the dictionaries, asynchronous and synchronous, on a view or on a `DbDataReader`, copy their rows out and never show their pooled arrays. They hide their pooling, so a query that does not call `UsePool` now always clears the used rows of those arrays before they go back, plain value rows included, and the app-wide switch cannot turn that off. `UsePool(pool, clearOnReturn)` is followed as before. Rows that hold references were always cleared. The buffers and the streams keep their rule (references, then the query, then the switch).

The chunk chain and the array sink release through a new `ChunkConfig.SealCopyOut`, which reads `CopyOutClearOnReturn` (`PoolClearing.ResolveCopyOut`: what `UsePool` said, otherwise clear). A chunk of 128 KB or more is still cleared in the recycle bin, so the read path gains no inline clear for large results.

The bytes per call of `ToArrayAsync` and `ToImmutableArrayAsync` on a reader (0, 1, 1,000 and 100,000 rows) are the same before and after.

Tests: the rule, the chain and the array sink with no pool, `UsePool(true)` and `UsePool(false)`, in place and in the bin, a row type with references through the chain's copy-out, and the collecting terminals of a view, asynchronous and synchronous, with no pool offered, `UsePool(true)` and `UsePool(false)`. The no-pool cases fail on the old code.

Docs: pooled memory remarks, the async buffers article, the pooling and clearing article, the deck notes, `UsePool`, `ChunkConfig`, and the release notes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit dfb051f into master Dec 2, 2026
9 checks passed
@electricessence
electricessence deleted the feature/copy-out-clearing branch December 2, 2026 03:05
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