-
Notifications
You must be signed in to change notification settings - Fork 346
Conversation
Added |
|
fe9ab0c
to
c441844
Compare
|
||
if (index > _outstandingReferences.Length) | ||
{ | ||
var refs = new uint[index + 1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this store refs in _outstandingReferences?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_outstandingReferences is just a counted bit mask; there is an issue with Empty
OwnedMemory as it would keep growing, but that has disposal issues anyway.
Idea in this pattern is if _outstandingReferences
array > 1 in length on disposal it is released and set to empty. If it is length == 1 it is kept and cleared; so in reuse if less than 32 reservations were made on it there would be no allocations.
This is interesting. I just worry about two things:
cc: @davidfowl, @jkotas, @vancem |
Is mostly just a bit mask so should be low; unless you go above 31 reservations when it will allocate (and every 32 reservations on the same OwnedMemory prior to disposal) - but hopefully that should be uncommon.
A sort of lifetime-less shared OwnedMemory beyond OwnedArray? Will have an explore... |
It's a bitmask but in an array. Are you not concerned with the allocation? |
You know my weakness ;-) Also is an extra indirection... Could dual field it; change to long + array and only allocate array if you expand passed 64 reservations? Probably wouldn't be worse than just an array, but wouldn't be pleasing to eye... |
Yeah, I was thinking about using an int first (or even not having the _references field) and then switching to an array. Since slots cannot be reused, sooner of later it would allocate (if many reservations are taken from the same memory). BTW, this test fails (with index out of range): OwnedMemory<byte> owned = new OwnedArray<byte>(1024);
var baseMemory = owned.Memory;
for(int i=0; i<100; i++) {
var r = baseMemory.Reserve();
r.Dispose();
} |
Off by one error + missing assingment, adding test |
Closing this @benaadams |
Calling dispose on reservation disposes all associated
Memory
.Reservation dispose can only dispose itself and Memory derived from it; not other Reservations or owner (reference count only decrements once).
Calling dispose on owner disposes itself and all associated
Memory
it does not dispose any Reservations or their Memory (reference count only decrements once).Lifetimes are independent and disposes can't effect each other e.g. you can't copy the Reservation struct and then over dispose the reference.
Full compliance with Dispose pattern