From f6230d37089fbb52612caf338dc6671e027817b0 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Wed, 23 Oct 2024 17:14:35 +0100 Subject: [PATCH] NIOPosix on Darwin: inherit main thread QoS (#2944) ### Motivation: On Darwin, QoS (quality of service) of threads plays an important role, especially on Apple Silicon machines with P-cores and E-cores. If you spawn raw threads (like NIOPosix) and use a mechanism that doesn't support QoS propagation (like reading/writing to networks -- like NIOPosix does), it's recommended to default to the main thread's QoS. Otherwise you'll always be at the default QoS for "legacy" threads which means bad latencies, especially on Apple Silicon machines. In a follow-up PR #2943 we're adding better configurability for thread configuration. ### Modifications: Default to main thread QoS on Darwin. ### Result: Better latencies for applications with higher QoS classes. --- Sources/NIOPosix/ThreadPosix.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/NIOPosix/ThreadPosix.swift b/Sources/NIOPosix/ThreadPosix.swift index aacaba5b47..f876c42c8a 100644 --- a/Sources/NIOPosix/ThreadPosix.swift +++ b/Sources/NIOPosix/ThreadPosix.swift @@ -43,7 +43,12 @@ private func sysPthread_create( args: UnsafeMutableRawPointer? ) -> CInt { #if canImport(Darwin) - return pthread_create(handle, nil, destructor, args) + var attr: pthread_attr_t = .init() + pthread_attr_init(&attr) + pthread_attr_set_qos_class_np(&attr, qos_class_main(), 0) + let thread = pthread_create(handle, &attr, destructor, args) + pthread_attr_destroy(&attr) + return thread #else #if canImport(Musl) var handleLinux: OpaquePointer? = nil