Skip to content

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

Merged
merged 1 commit into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Shared/Buffers.MemoryPool/MemoryPoolBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal MemoryPoolBlock(SlabMemoryPool pool, MemoryPoolSlab slab, int offset, i
Pool = pool;
Slab = slab;

Memory = MemoryMarshal.CreateFromPinnedArray(slab.Array, _offset, _length);
Memory = MemoryMarshal.CreateFromPinnedArray(slab.PinnedArray, _offset, _length);
}

/// <summary>
Expand Down
56 changes: 9 additions & 47 deletions src/Shared/Buffers.MemoryPool/MemoryPoolSlab.cs
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>
Expand All @@ -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>
Copy link
Member

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.

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;

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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)

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}
}
}
4 changes: 3 additions & 1 deletion src/Shared/Buffers.MemoryPool/SlabMemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace System.Buffers
Expand Down Expand Up @@ -110,7 +111,8 @@ private MemoryPoolBlock AllocateSlab()
var slab = MemoryPoolSlab.Create(_slabLength);
_slabs.Push(slab);

var basePtr = slab.NativePointer;
// Get the address for alignment
IntPtr basePtr = Marshal.UnsafeAddrOfPinnedArrayElement(slab.PinnedArray, 0);
// Page align the blocks
var offset = (int)((((ulong)basePtr + (uint)_blockSize - 1) & ~((uint)_blockSize - 1)) - (ulong)basePtr);
// Ensure page aligned
Expand Down