Skip to content

Commit

Permalink
Cache the page size on a static readonly field and add a couple of TO…
Browse files Browse the repository at this point in the history
…DOs.
  • Loading branch information
teo-tsirpanis committed Aug 15, 2021
1 parent 656adf6 commit e1b94a9
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ static async ValueTask<long> CastValueTask(ValueTask<int> task) =>
}

// Abstracts away the type signature incompatibility between Memory and ReadOnlyMemory.
// TODO: Use abstract static methods when they become stable.
private interface IMemoryHandler<T>
{
int GetLength(in T memory);
Expand All @@ -469,6 +470,9 @@ private struct ReadOnlyMemoryHandler : IMemoryHandler<ReadOnlyMemory<byte>>
private static bool CanUseScatterGatherWindowsAPIs(SafeFileHandle handle)
=> handle.IsAsync && ((handle.GetFileOptions() & SafeFileHandle.NoBuffering) != 0);

// TODO: Use SystemPageSize directly when #57442 is fixed.
private static readonly int s_cachedPageSize = Environment.SystemPageSize;

// From the same source:
// "Each buffer must be at least the size of a system memory page and must be aligned on a system
// memory page size boundary. The system reads/writes one system memory page of data into/from each buffer."
Expand All @@ -485,7 +489,7 @@ private static unsafe bool TryPrepareScatterGatherBuffers<T, THandler>(IReadOnly
THandler handler, out MemoryHandle[] handlesToDispose, out IntPtr segmentsPtr, out int totalBytes)
where THandler: struct, IMemoryHandler<T>
{
int pageSize = Environment.SystemPageSize;
int pageSize = s_cachedPageSize;
Debug.Assert(BitOperations.IsPow2(pageSize), "Page size is not a power of two.");
// We take advantage of the fact that the page size is
// a power of two to avoid an expensive modulo operation.
Expand Down

0 comments on commit e1b94a9

Please sign in to comment.