Question: Where is the difference between: "Microsoft.Toolkit.HighPerformance.Buffers.SpanOwner<T>" and "DotNext.MemoryRental<T>"? #159
-
Title. src for "SpanOwner": https://learn.microsoft.com/en-us/dotnet/communitytoolkit/high-performance/spanowner I cant quite grasp in what scenario yours may be better or equal to that type? I liked from your type that it may rent from whatever pool depending on the size needed, although I dont know if the reasoning-algorithm for that beats the purpose of "SpanOwner" from "Microsoft.Toolkit.HighPerformance" Nuget-package. Any advise to this is very very appreciated cause I am in process to determine the proper buffering-strategy and would be cool to get the best one selected ^^ Btw very strong and dedicated work you have put in here, you earned my highest respect! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
const int stackallocThreshold = 20;
var memory = size <=stackallocThreshold
? new MemoryRental<byte>(stackalloc byte[stackallocThreshold], size)
: new MemoryRental<byte>(size); You can find a lot of places in .NEXT codebase that use this pattern. MemoryOwner<T> is a regular value type (not ref) that can be used inside of async context. Thus, it can't be used to make abstraction over stack-allocated memory. Its counterpart from Community Toolkit is MemoryOwner<T>, surprise 😄 (I never tried to copy-paste from Community Toolkit implementation, moreover, I even didn't know about it in the beginning of .NEXT project). However, implementation from .NEXT is different:
|
Beta Was this translation helpful? Give feedback.
-
First, thx alot for the detailed explanation! Would there be 1) any benefit, in actually creating a hybrid buffer, who takes any give (sizeOf(T) * count) and checks of HOW MUCH of that (sizeOf(T) * count) does actually fit inside a reasonable stackalloc (lets say 1-2 KB) and THE REMAINING of that size-expression will be rented from a shared pool? example:
Would this approach yield any benefit in your opinion? Best regards! |
Beta Was this translation helpful? Give feedback.
BufferWriterSlim
is a growable dynamic buffer whileMemoryRental
not. You can add as many elements as you want toBufferWriterSlim
. It transparently reallocates the internal buffer.MemoryRental
can't grow. They have the same difference asList<T>
andT[]
.