Skip to content

Don't use callbacks when performing sync IO with async handles #126839

Closed

Description

@adamsitnik

In RandomAccess.Windows.cs, we create a dedicated callback:

private static readonly IOCompletionCallback s_callback = AllocateCallback();

Then provide it in these places:

NativeOverlapped* result = handle.ThreadPoolBinding!.UnsafeAllocateNativeOverlapped(s_callback, resetEvent, null);

And handle the callback in a dedicated class:

// We need to store the reference count (see the comment in ReleaseRefCount) and an EventHandle to signal the completion.
// We could keep these two things separate, but since ManualResetEvent is sealed and we want to avoid any extra allocations, this type has been created.
// It's basically ManualResetEvent with reference count.
private sealed class CallbackResetEvent : EventWaitHandle
{
private readonly ThreadPoolBoundHandle _threadPoolBoundHandle;
private int _freeWhenZero = 2; // one for the callback and another for the method that calls GetOverlappedResult
internal CallbackResetEvent(ThreadPoolBoundHandle threadPoolBoundHandle) : base(initialState: false, EventResetMode.ManualReset)
{
_threadPoolBoundHandle = threadPoolBoundHandle;
}
internal unsafe void ReleaseRefCount(NativeOverlapped* pOverlapped)
{
// Each SafeFileHandle opened for async IO is bound to ThreadPool.
// It requires us to provide a callback even if we want to use EventHandle and use GetOverlappedResult to obtain the result.
// There can be a race condition between the call to GetOverlappedResult and the callback invocation,
// so we need to track the number of references, and when it drops to zero, then free the native overlapped.
if (Interlocked.Decrement(ref _freeWhenZero) == 0)
{
_threadPoolBoundHandle.FreeNativeOverlapped(pOverlapped);
}
}
}

Based on my finding from #126807, we don't need to provide a callback when configuring WaitHandle for overlapped provided by ReadFile/WriteFile. What we need to do is setting the low-order bit of hEvent in the OVERLAPPED structure:

private static unsafe NativeOverlapped* AllocateOverlapped(EventWaitHandle waitHandle)
{
NativeOverlapped* overlapped = (NativeOverlapped*)NativeMemory.AllocZeroed((nuint)sizeof(NativeOverlapped));
overlapped->EventHandle = SetLowOrderBit(waitHandle);
return overlapped;
}

/// <summary>
/// Returns the event handle with the low-order bit set.
/// Per https://learn.microsoft.com/windows/win32/api/ioapiset/nf-ioapiset-getqueuedcompletionstatus,
/// setting the low-order bit of hEvent in the OVERLAPPED structure prevents the I/O completion
/// from being queued to a completion port bound to the same file object. The kernel masks off
/// the bit when signaling, so the event still works normally.
/// </summary>
private static nint SetLowOrderBit(EventWaitHandle waitHandle)
=> waitHandle.SafeWaitHandle.DangerousGetHandle() | 1;

This should allow us to get rid of CallbackResetEvent, slightly reduce allocations and complexity. The perf might also get better.

Activity

  1. added this to the 11.0.0 milestone on Apr 13, 2026
  2. Copilot commented on Apr 13, 2026

    Copilot
    Contributor

    The agent encountered an error and was unable to start working on this issue: The service is temporarily unavailable. Please try again later. (Request id: 2761:C7BFB:196C634:12D7A01:69DD1849)

  3. dotnet-policy-service commented on Apr 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-io
    See info in area-owners.md if you want to be subscribed.

  4. Copilot commented on Apr 13, 2026

    Copilot
    Contributor

    The agent encountered an error and was unable to start working on this issue: The service is temporarily unavailable. Please try again later. (Request id: 276A:376576:1B56E98:14379C7:69DD1A6D)

  5. Copilot commented on Apr 13, 2026

    Copilot
    Contributor

    The agent encountered an error and was unable to start working on this issue: The service is temporarily unavailable. Please try again later. (Request id: 2764:F4966:1F0385A:16D89FD:69DD1EB9)

  6. adamsitnik commented on Jun 12, 2026

    @adamsitnik
    MemberAuthor

    managed_cached is what we got in #126845

    As you can see, sync IO over async file handles is up to two times faster now ;)

    BenchmarkDotNet v0.16.0-nightly.20260302.459, Windows 11 (10.0.26200.8390/25H2/2025Update/HudsonValley2)
    AMD Ryzen Threadripper PRO 3945WX 12-Cores 3.99GHz, 1 CPU, 24 logical and 12 physical cores
    Memory: 63.86 GB Total, 32.62 GB Available
    .NET SDK 11.0.100-preview.5.26255.101
      [Host]     : .NET 11.0.0 (11.0.0-preview.5.26255.101, 11.0.26.25601), X64 RyuJIT x86-64-v3
    Method Toolchain fileSize userBufferSize options Mean Ratio Allocated
    Read \main\corerun.exe 1024 1024 None 145.7 us 1.00 4360 B
    Read \main\corerun.exe 1024 1024 Asynchronous 173.9 us 1.00 4576 B
    Read \managed_cached\corerun.exe 1024 1024 Asynchronous 156.9 us 0.90 4464 B
    Read \native_cached\corerun.exe 1024 1024 Asynchronous 154.6 us 0.89 4440 B
    Write \main\corerun.exe 1024 1024 None 609.2 us 1.00 4360 B
    Write \main\corerun.exe 1024 1024 Asynchronous 639.3 us 1.00 4577 B
    Write \managed_cached\corerun.exe 1024 1024 Asynchronous 627.9 us 0.98 4465 B
    Write \native_cached\corerun.exe 1024 1024 Asynchronous 623.3 us 0.98 4441 B
    Read \main\corerun.exe 1048576 512 None 779.2 us 1.00 4361 B
    Read \main\corerun.exe 1048576 512 Asynchronous 2,756.3 us 1.00 49460 B
    Read \managed_cached\corerun.exe 1048576 512 Asynchronous 1,262.3 us 0.46 4466 B
    Read \native_cached\corerun.exe 1048576 512 Asynchronous 1,113.6 us 0.40 4442 B
    Write \main\corerun.exe 1048576 512 None 7,438.8 us 1.00 4365 B
    Write \main\corerun.exe 1048576 512 Asynchronous 7,788.8 us 1.00 49470 B
    Write \managed_cached\corerun.exe 1048576 512 Asynchronous 7,507.1 us 0.96 4473 B
    Write \native_cached\corerun.exe 1048576 512 Asynchronous 7,387.7 us 0.95 4449 B
    Read \main\corerun.exe 1048576 4096 None 709.8 us 1.00 241 B
    Read \main\corerun.exe 1048576 4096 Asynchronous 2,929.2 us 1.00 45340 B
    Read \managed_cached\corerun.exe 1048576 4096 Asynchronous 1,235.6 us 0.42 346 B
    Read \native_cached\corerun.exe 1048576 4096 Asynchronous 1,049.8 us 0.36 322 B
    Write \main\corerun.exe 1048576 4096 None 7,197.4 us 1.00 245 B
    Write \main\corerun.exe 1048576 4096 Asynchronous 7,838.3 us 1.00 45350 B
    Write \managed_cached\corerun.exe 1048576 4096 Asynchronous 7,351.9 us 0.94 353 B
    Write \native_cached\corerun.exe 1048576 4096 Asynchronous 7,477.0 us 0.96 329 B
    Read \main\corerun.exe 104857600 4096 None 82,969.7 us 1.00 352 B
    Read \main\corerun.exe 104857600 4096 Asynchronous 329,198.7 us 1.00 4506328 B
    Read \managed_cached\corerun.exe 104857600 4096 Asynchronous 154,884.8 us 0.47 568 B
    Read \native_cached\corerun.exe 104857600 4096 Asynchronous 118,937.8 us 0.36 544 B
    Write \main\corerun.exe 104857600 4096 None 172,746.9 us 1.00 464 B
    Write \main\corerun.exe 104857600 4096 Asynchronous 458,918.6 us 1.00 4506328 B
    Write \managed_cached\corerun.exe 104857600 4096 Asynchronous 208,779.5 us 0.46 792 B
    Write \native_cached\corerun.exe 104857600 4096 Asynchronous 170,257.2 us 0.37 768 B
  7. locked and limited conversation to collaborators on Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.IOtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions