Skip to content

Commit e1b94a9

Browse files
committed
Cache the page size on a static readonly field and add a couple of TODOs.
1 parent 656adf6 commit e1b94a9

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ static async ValueTask<long> CastValueTask(ValueTask<int> task) =>
446446
}
447447

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

473+
// TODO: Use SystemPageSize directly when #57442 is fixed.
474+
private static readonly int s_cachedPageSize = Environment.SystemPageSize;
475+
472476
// From the same source:
473477
// "Each buffer must be at least the size of a system memory page and must be aligned on a system
474478
// 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<T, THandler>(IReadOnly
485489
THandler handler, out MemoryHandle[] handlesToDispose, out IntPtr segmentsPtr, out int totalBytes)
486490
where THandler: struct, IMemoryHandler<T>
487491
{
488-
int pageSize = Environment.SystemPageSize;
492+
int pageSize = s_cachedPageSize;
489493
Debug.Assert(BitOperations.IsPow2(pageSize), "Page size is not a power of two.");
490494
// We take advantage of the fact that the page size is
491495
// a power of two to avoid an expensive modulo operation.

0 commit comments

Comments
 (0)