-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Use Pinned Object Heap for MemoryPool #21614
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Buffers | ||
{ | ||
/// <summary> | ||
|
@@ -11,70 +9,34 @@ namespace System.Buffers | |
/// </summary> | ||
internal class MemoryPoolSlab : IDisposable | ||
{ | ||
/// <summary> | ||
/// This handle pins the managed array in memory until the slab is disposed. This prevents it from being | ||
/// relocated and enables any subsections of the array to be used as native memory pointers to P/Invoked API calls. | ||
/// </summary> | ||
private GCHandle _gcHandle; | ||
private bool _isDisposed; | ||
|
||
public MemoryPoolSlab(byte[] data) | ||
private MemoryPoolSlab(byte[] pinnedData) | ||
{ | ||
Array = data; | ||
_gcHandle = GCHandle.Alloc(data, GCHandleType.Pinned); | ||
NativePointer = _gcHandle.AddrOfPinnedObject(); | ||
PinnedArray = pinnedData; | ||
} | ||
|
||
/// <summary> | ||
/// True as long as the blocks from this slab are to be considered returnable to the pool. In order to shrink the | ||
/// memory pool size an entire slab must be removed. That is done by (1) setting IsActive to false and removing the | ||
/// slab from the pool's _slabs collection, (2) as each block currently in use is Return()ed to the pool it will | ||
/// be allowed to be garbage collected rather than re-pooled, and (3) when all block tracking objects are garbage | ||
/// collected and the slab is no longer references the slab will be garbage collected and the memory unpinned will | ||
/// be unpinned by the slab's Dispose. | ||
/// collected and the slab is no longer references the slab will be garbage collected | ||
/// </summary> | ||
public bool IsActive => !_isDisposed; | ||
public bool IsActive => PinnedArray != null; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think IDisposable is no longer needed and PinnedArray can be a readonly field. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is needed to switch off the Slab; which then means its blocks are thrown away rather than returned to the pool (i.e. to take them out of circulation) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well sorta... since the pool doesn't really shrink; but if it did... 😉 |
||
public IntPtr NativePointer { get; private set; } | ||
|
||
public byte[] Array { get; private set; } | ||
public byte[] PinnedArray { get; private set; } | ||
|
||
public static MemoryPoolSlab Create(int length) | ||
{ | ||
// allocate and pin requested memory length | ||
var array = new byte[length]; | ||
// allocate requested memory length from the pinned memory heap | ||
var pinnedArray = GC.AllocateUninitializedArray<byte>(length, pinned: true); | ||
|
||
// allocate and return slab tracking object | ||
return new MemoryPoolSlab(array); | ||
} | ||
|
||
protected void Dispose(bool disposing) | ||
{ | ||
if (_isDisposed) | ||
{ | ||
return; | ||
} | ||
|
||
_isDisposed = true; | ||
|
||
Array = null; | ||
NativePointer = IntPtr.Zero; | ||
|
||
if (_gcHandle.IsAllocated) | ||
{ | ||
_gcHandle.Free(); | ||
} | ||
} | ||
|
||
~MemoryPoolSlab() | ||
{ | ||
Dispose(false); | ||
return new MemoryPoolSlab(pinnedArray); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
PinnedArray = null; | ||
} | ||
} | ||
} |
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.
Nice to see that all this code can be removed.