Add optional maximum capacity to buffer writers#287
Open
petrroll wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Frequently you want to buffer but want to be sure it won't each unnecessary too much memory, have some (even if high) upper ceiling. This PR implements in the less impactful way. It's not limit for writing, it's a limit for sizing the buffer.
Not a strict one (as pool can give you bigger) but somewhat guaranteeing you won't OOM. Implemented this way it has almost no perf impact and doesn't complicate the usage much.
If desired it can be tightened up to either strictly limite the buffer / writes (so that it's guaranteed not to get bigger capacity buffer; thus also write more). I don't think it's needed (not for my usecase at least) but could be useful for some.
Summary
Adds an optional
MaxCapacitylimit to the pooling buffer writers so buffer growth can be bounded.MaxCapacityis now available on:PoolingBufferWriter<T>andPoolingArrayBufferWriter<T>(via the sharedBufferWriter<T>base class)BufferWriterSlim<T>When a write requires the internal buffer to grow beyond
MaxCapacity,InsufficientMemoryExceptionis thrown — consistent with the existingArray.MaxLengthceiling and the other max-size guards across the library. The check lives in the shared grow path (IGrowableBuffer<T>.GetBufferSize); when the write still fits, the growth request is clamped toMaxCapacity. By default the maximum capacity is unbounded.Notes
Capacitycan be slightly larger. This is documented in the API remarks.SparseBufferWriter<T>is intentionally not affected — it grows by appending independent chunks and does not use the shared grow path.Tests
Added unit tests covering growth within the limit, growth beyond the limit (throws), exact-boundary clamping, validation of invalid values, and the unbounded default.