Skip to content

Linux sockets perf: Replace _handleToContextMap dictionary with GCHandle  #43992

Closed

Description

@geoffkizer

SocketAsyncEngine uses _handleToContextMap to find the SocketAsyncContext associated with each epoll notification.

We should be able to replace this with a GCHandle and eliminate the dictionary lookup entirely. We would pass the GCHandle as the data param to epoll_ctl (i.e. TryChangeSocketEventRegistration) instead of the socket handle as we do now. When we receive an epoll notification, we'd simply dereference the GCHandle, which is much cheaper than the dictionary lookup.

I think the tricky part here is figuring out how to release the GCHandle when we are done with the socket and don't expect any more epoll notifications. I think we can wait for an EPOLLHUP notification and dispose the GCHandle then -- I believe this will guarantee we won't get any more epoll notifications for this socket. However, I think from previous experiments here that closing the socket fd will cause epoll notifications to stop, without generating an EPOLLHUP. I think we can deal with this by deferring the actual close until we receive EPOLLHUP, but we need to experiment here.

Activity

  1. added this to the 6.0.0 milestone on Oct 28, 2020
  2. tmds commented on Oct 29, 2020

    @tmds
    Member

    @benaadams I think you suggested/tried this once.

  3. benaadams commented on Oct 29, 2020

    @benaadams
    Member

    Yes, had an issue with events coming through after the handle was freed, which are currently thrown away with the map lookup (as is no longer there) so that would need to be tightened up.

  4. added
    enhancementProduct code improvement that does NOT require public API changes/additions
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Oct 29, 2020
  5. modified the milestones: 6.0.0, Future on May 13, 2021
  6. MihaZupan commented on Nov 12, 2025

    @MihaZupan
    Member

    This was effectively done in .NET 10 - #109052.
    Instead of the ConcurrentDictionary lookup, we now do a lookup from an array instead.
    The epoll state is an arbitrary index into a global array instead of the socket handle.

  7. modified the milestones: Future, 10.0.0 on Nov 12, 2025
  8. locked and limited conversation to collaborators on Dec 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions