This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Change OwnedMemory to MemoryManager and add an IMemoryOwner. #17340
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a297b76
Change OwnedMemory to MemoryManager and add an IMemoryOwner.
ahsonkhan b717bfa
Fix comments.
ahsonkhan 5044940
Reset start and length if TryGetMemoryManager returns false.
ahsonkhan c1ed9b1
Reset start and length if TryGetMemoryManager returns false [actually].
ahsonkhan 1988ca1
Re-order MemoryHandle ctor parameters and rename TOwner to TManager.
ahsonkhan 984b805
Fix formatting, remove unused code, and fix impl of Pin()
ahsonkhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace System.Buffers | ||
| { | ||
| /// <summary> | ||
| /// Owner of Memory<typeparamref name="T"/> that is responsible for disposing the underlying memory appropriately. | ||
| /// </summary> | ||
| public interface IMemoryOwner<T> : IDisposable | ||
| { | ||
| /// <summary> | ||
| /// Returns a Memory<typeparamref name="T"/>. | ||
| /// </summary> | ||
| Memory<T> Memory { get; } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace System.Buffers | ||
| { | ||
| /// <summary> | ||
| /// Provides a mechanism for pinning and unpinning objects to prevent the GC from moving them. | ||
| /// </summary> | ||
| public interface IPinnable | ||
| { | ||
| /// <summary> | ||
| /// Call this method to indicate that the IPinnable object can not be moved by the garbage collector. | ||
| /// The address of the pinned object can be taken. | ||
| /// <param name="elementIndex">The offset to the element within the memory at which the returned <see cref="MemoryHandle"/> points to.</param> | ||
| /// </summary> | ||
| MemoryHandle Pin(int elementIndex); | ||
|
|
||
| /// <summary> | ||
| /// Call this method to indicate that the IPinnable object no longer needs to be pinned. | ||
| /// The garbage collector is free to move the object now. | ||
| /// </summary> | ||
| void Unpin(); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Runtime; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace System.Buffers | ||
| { | ||
| /// <summary> | ||
| /// Manager of Memory<typeparamref name="T"/> that provides the implementation. | ||
| /// </summary> | ||
| public abstract class MemoryManager<T> : IMemoryOwner<T>, IPinnable | ||
| { | ||
| /// <summary> | ||
| /// The number of items in the Memory<typeparamref name="T"/>. | ||
| /// </summary> | ||
| public virtual int Length => GetSpan().Length; | ||
|
|
||
| /// <summary> | ||
| /// Returns a Memory<typeparamref name="T"/>. | ||
| /// </summary> | ||
| public virtual Memory<T> Memory => new Memory<T>(this, 0, Length); | ||
|
|
||
| /// <summary> | ||
| /// Returns a span wrapping the underlying memory. | ||
| /// </summary> | ||
| public abstract Span<T> GetSpan(); | ||
|
|
||
| /// <summary> | ||
| /// Returns a handle to the memory that has been pinned and hence its address can be taken. | ||
| /// <param name="elementIndex">The offset to the element within the memory at which the returned <see cref="MemoryHandle"/> points to. (default = 0)</param> | ||
| /// </summary> | ||
| public abstract MemoryHandle Pin(int elementIndex = 0); | ||
|
|
||
| /// <summary> | ||
| /// Lets the garbage collector know that the object is free to be moved now. | ||
| /// </summary> | ||
| public abstract void Unpin(); | ||
|
|
||
| /// <summary> | ||
| /// Returns an array segment. | ||
| /// <remarks>Returns the default array segment if not overriden.</remarks> | ||
| /// </summary> | ||
| protected internal virtual bool TryGetArray(out ArraySegment<T> segment) | ||
| { | ||
| segment = default; | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Implements IDisposable. | ||
| /// </summary> | ||
| void IDisposable.Dispose() | ||
| { | ||
| Dispose(disposing: true); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Clean up of any leftover managed and unmanaged resources. | ||
| /// </summary> | ||
| protected abstract void Dispose(bool disposing); | ||
|
|
||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.
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.
With this change; any reason for this to be virtual?
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.
Probably to avoid the
GetSpan()call?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.
Could have done
Unless its valid that derived classes of
MemoryManagercan change in length on the go?Uh oh!
There was an error while loading. Please reload this page.
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.
@benaadams There are scenarios where this can change; e.g., if the factory pools and reuses these instances. We can provide a default implementation of this logic via reading the value from
GetSpan(), but as a perf optimization implementations may want to provide the value in a more direct manner.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.
Why would it change in case of a pool? We usually don't return exact size requested.
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.
@benaadams @GrabYourPitchforks just made me aware of the fact that this type will rarely be used directly because the MemoryPool returns
IMemoryOwner<T>, so really none of this matters. Though as an implementer ofMemoryManager<T>, I think it's confusing to expose length. So lets try to come up with something concrete.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.
K so
Memorywon't inline due to the interface.However as the implementer you will have to get a
Lengthsomewhere and possibly anOffset, so can stick it on the type and have everything else inline and have a good performance implementation with no additional virtuals or the need to implement the two methods?Uh oh!
There was an error while loading. Please reload this page.
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.
Or kill
Lengthand force the implementationPassing off the validation of offset and length to derived class
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.
@GrabYourPitchforks making it abstract?
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.
Thanks for the feedback. In the interest of time, I am going to merge the change as is and we can consider changing this separately.