Refactors VariantValueWriter#329
Conversation
Refactors `VariantValueWriter` to use a new `Buffer<>` type to manage the output buffers instead of using `MemoryStream`.
There was a problem hiding this comment.
Pull request overview
Refactors VariantValueWriter to replace MemoryStream-based output buffering with a new pooled Buffer<T> implementation, aiming to reduce allocations and improve reuse.
Changes:
- Introduce
Buffer<T>+ byte-writing extensions to manage pooled, growable buffers. - Refactor
VariantValueWriterto useBuffer<byte>/Buffer<int>, add per-writer pools, and implementIDisposable. - Update call sites to dispose the writer, and add
CopyValue(VariantReader)for transcoding.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/Apache.Arrow.Scalars/Variant/VariantValueWriter.cs | Replaces MemoryStream with pooled Buffer<>, adds disposal, and adds CopyValue transcoding. |
| src/Apache.Arrow.Scalars/Variant/VariantBuilder.cs | Disposes VariantValueWriter via using to return pooled arrays. |
| src/Apache.Arrow.Scalars/Variant/Buffer.cs | Adds the new pooled, growable Buffer<T> and byte-oriented write helpers. |
| src/Apache.Arrow.Operations/VariantJson/VariantJsonReader.cs | Disposes VariantValueWriter via using to return pooled arrays. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
adamreeve
left a comment
There was a problem hiding this comment.
Looks good to me thanks Curt.
Is the motivation for this just to increase performance by reducing allocations, and do you have any measurements of the effect of this change on allocations?
I saw you made a comment about wanting to refactor VariantValueWriter before adding shredding support, but I'm not sure how this helps with shredding.
| /// within one owner — unlike <see cref="ArrayPool{T}.Shared"/>, which | ||
| /// buckets by size class and may hand back a different array than the one | ||
| /// last returned. | ||
| /// </summary> |
There was a problem hiding this comment.
Might be worth adding a comment that arrays may be added to the pool from ArrayPool<T>.Shared by Acquire and the caller is responsible for returning all arrays in the Stack back to ArrayPool<T>.Shared.
There was a problem hiding this comment.
Thanks! This got me to realize that the arrays being rented in Buffer but returned in VariantValueWriter was unsatisfying if not actively dangerous. I've moved the drainage code to Buffer and added a comment.
There was a problem hiding this comment.
👍 that's much nicer having the array pool interactions all within Buffer
There is shredding-related code which needs to write a |
What's Changed
Refactors
VariantValueWriterto use a newBuffer<>type to manage the output buffers instead of usingMemoryStream.