From e1b94a9323c554dc7e492b291d451072da1cc4c1 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Sun, 15 Aug 2021 19:07:53 +0300 Subject: [PATCH] Cache the page size on a static readonly field and add a couple of TODOs. --- .../src/System/IO/RandomAccess.Windows.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs index 6cc4794137ce30..90a6c4de074bf0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs @@ -446,6 +446,7 @@ static async ValueTask CastValueTask(ValueTask task) => } // Abstracts away the type signature incompatibility between Memory and ReadOnlyMemory. + // TODO: Use abstract static methods when they become stable. private interface IMemoryHandler { int GetLength(in T memory); @@ -469,6 +470,9 @@ private struct ReadOnlyMemoryHandler : IMemoryHandler> 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." @@ -485,7 +489,7 @@ private static unsafe bool TryPrepareScatterGatherBuffers(IReadOnly THandler handler, out MemoryHandle[] handlesToDispose, out IntPtr segmentsPtr, out int totalBytes) where THandler: struct, IMemoryHandler { - 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.