-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: using
support for ArrayPool
#112839
Comments
Tagging subscribers to this area: @dotnet/area-system-buffers |
The typical use should not involve try-finally as it makes it fragile and vulnerable to potential use-after-free bugs (unless it is 100% predictable who/how can throw an exception) The end goal is to hide 95% of usages with something like #52065 |
A struct like this also makes it even more likely to accidentally return the buffer multiple times. And using a class instead is exactly what MemoryPool provides. With all of the deabstraction work being done by @AndyAyersMS, I would also hope that this allocation could end up frequently being implicitly stack allocated, once all the right inlining and the like happens. |
Presumably, it is unlikely JIT can ever do "stackalloc or ArrayPool" under the hood for a normal allocation, only "stackalloc or heap", so an explicit API probably still good idea. |
I wasn't talking about the array allocation. Rather, I was talking about the IDisposable object wrapper it allocates for the returned array. |
Should this cover synchronous cases only? What about the async cases? |
It would hopefully cover the async cases as well in the 99% case once the new async support lands in the runtime. |
Background and motivation
Currently,
ArrayPool<T>.Shared
is used more widely in user code and BCL code, especially async methods. Typical use cases look like:The try-finally pattern currently can't leverage
using
syntax to simplify the code. In contrast,MemoryPool<T>
is providingusing
support throughIMemoryOwner<T>
:However,
MemoryPool<T>
andIMemoryOwner<T>
aren't suitable for many use cases ofArrayPool<T>
because of the higher overhead ofIMemoryOwner<T>
andMemory<T>
. Thus, it can be worthy to addusing
support forArrayPool<T>
for low overhead.API Proposal
API Usage
Alternative Designs
Making
RentLease
virtual. Unlikely because it would create either additional performance penalty on top ofRent
, or duplicated code withRent
if overridden.Risks
When
Lease
is captured in async method, it will capture additional fields comparing to raw array management. The pool (usuallyArrayPool<T>.Shared
and additional arguments for return (clearOnReturn
) need to be captured.The text was updated successfully, but these errors were encountered: