Skip to content

Commit

Permalink
Use 4 and 8 GCD sync read queues for OSX and Linux, respectively (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
djones6 authored Feb 22, 2018
1 parent d781655 commit 0837058
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions Sources/KituraNet/IncomingSocketHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,41 @@ public class IncomingSocketHandler {
// (see: https://github.com/IBM-Swift/Kitura/issues/1034) while a proper fix is
// investigated.
var superfluousOptional:String? = String(repeating: "x", count: 2)


// Default tuning for number of GCD serial read queues for DispatchSourceRead.
// On OSX, the optimum number depends on the number of physical threads, with a
// decline in performance if too many are used, so a conservative number (4) is
// chosen by default. Systems with more than 8 hardware threads may benefit from a
// larger number.
#if os(OSX) || os(iOS) || os(tvOS) || os(watchOS)
private static let numberOfSocketReaderQueues = 4
#endif

// Default tuning for GCD serial read queues on Linux (with GCD_ASYNCH)
// As of Swift 4.0, it appears that a larger number of queues provides better
// performance (even if that number exceeds the number of hardware threads),
// though there are diminishing returns after that point.
// The default of 8 queues is sufficient for moderately sized systems. Systems
// with more than 8 hardware threads may benefit from a larger number.
#if os(Linux) && GCD_ASYNCH
private static let numberOfSocketReaderQueues = 8
#endif

#if os(OSX) || os(iOS) || os(tvOS) || os(watchOS) || GCD_ASYNCH
static let socketReaderQueues = [DispatchQueue(label: "Socket Reader A"), DispatchQueue(label: "Socket Reader B")]
private static var _socketReaderQueues:[DispatchQueue] {
var result:[DispatchQueue] = []
for i in 1...numberOfSocketReaderQueues {
result.append(DispatchQueue(label: "Socket Reader \(i)"))
}
return result
}

static let socketReaderQueues:[DispatchQueue] = _socketReaderQueues

// Note: This var is optional to enable it to be constructed in the init function
var readerSource: DispatchSourceRead!
var writerSource: DispatchSourceWrite?

private let numberOfSocketReaderQueues = IncomingSocketHandler.socketReaderQueues.count

private let socketReaderQueue: DispatchQueue
#endif

Expand Down Expand Up @@ -92,7 +117,7 @@ public class IncomingSocketHandler {
processor = using

#if os(OSX) || os(iOS) || os(tvOS) || os(watchOS) || GCD_ASYNCH
socketReaderQueue = IncomingSocketHandler.socketReaderQueues[Int(socket.socketfd) % numberOfSocketReaderQueues]
socketReaderQueue = IncomingSocketHandler.socketReaderQueues[Int(socket.socketfd) % IncomingSocketHandler.numberOfSocketReaderQueues]

readerSource = DispatchSource.makeReadSource(fileDescriptor: socket.socketfd,
queue: socketReaderQueue)
Expand Down

0 comments on commit 0837058

Please sign in to comment.