diff --git a/IntegrationTests/tests_04_performance/test_01_allocation_counts_for_http1.sh b/IntegrationTests/tests_04_performance/test_01_allocation_counts_for_http1.sh index 3bced4733..5c45efea0 100644 --- a/IntegrationTests/tests_04_performance/test_01_allocation_counts_for_http1.sh +++ b/IntegrationTests/tests_04_performance/test_01_allocation_counts_for_http1.sh @@ -58,7 +58,7 @@ cd .. "$swift_bin" run -c release | tee "$tmp/output" ) -for test in 1000_reqs_1_conn 1_reqs_1000_conn ping_pong_1000_reqs_1_conn bytebuffer_lots_of_rw future_lots_of_callbacks; do +for test in 1000_reqs_1_conn 1_reqs_1000_conn ping_pong_1000_reqs_1_conn bytebuffer_lots_of_rw future_lots_of_callbacks scheduling_10000_executions; do cat "$tmp/output" # helps debugging total_allocations=$(grep "^$test.total_allocations:" "$tmp/output" | cut -d: -f2 | sed 's/ //g') not_freed_allocations=$(grep "^$test.remaining_allocations:" "$tmp/output" | cut -d: -f2 | sed 's/ //g') diff --git a/IntegrationTests/tests_04_performance/test_01_resources/template/Sources/SwiftBootstrap/SwiftMain.swift b/IntegrationTests/tests_04_performance/test_01_resources/template/Sources/SwiftBootstrap/SwiftMain.swift index 186c43b84..ca80c4321 100644 --- a/IntegrationTests/tests_04_performance/test_01_resources/template/Sources/SwiftBootstrap/SwiftMain.swift +++ b/IntegrationTests/tests_04_performance/test_01_resources/template/Sources/SwiftBootstrap/SwiftMain.swift @@ -18,7 +18,7 @@ import NIO import NIOHTTP1 import Foundation import AtomicCounter -import Dispatch // needed for Swift 4.0 on Linux only +import Dispatch private final class SimpleHTTPServer: ChannelInboundHandler { typealias InboundIn = HTTPServerRequestPart @@ -436,5 +436,21 @@ public func swiftMain() -> Int { return 1000 } + measureAndPrint(desc: "scheduling_10000_executions") { + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) + let loop = group.next() + let dg = DispatchGroup() + + try! loop.submit { + for _ in 0..<10_000 { + dg.enter() + loop.execute { dg.leave() } + } + }.wait() + dg.wait() + try! group.syncShutdownGracefully() + return 10_000 + } + return 0 } diff --git a/Sources/NIO/Heap.swift b/Sources/NIO/Heap.swift index 759a704be..8d958ba9d 100644 --- a/Sources/NIO/Heap.swift +++ b/Sources/NIO/Heap.swift @@ -51,76 +51,64 @@ internal struct Heap { self.type = type } - // MARK: verbatim from CLRS's (Introduction to Algorithms) Heapsort chapter with the following changes - // - added a `compare` parameter to make it either a min or a max heap - // - renamed `largest` to `root` - // - removed `max` from the method names like `maxHeapify`, `maxHeapInsert` etc - // - made the arrays 0-based - // named `PARENT` in CLRS - private static func parentIndex(_ i: Int) -> Int { + private func parentIndex(_ i: Int) -> Int { return (i-1) / 2 } // named `LEFT` in CLRS - private static func leftIndex(_ i: Int) -> Int { + private func leftIndex(_ i: Int) -> Int { return 2*i + 1 } // named `RIGHT` in CLRS - private static func rightIndex(_ i: Int) -> Int { + private func rightIndex(_ i: Int) -> Int { return 2*i + 2 } // named `MAX-HEAPIFY` in CLRS - private static func heapify(storage: inout ContiguousArray, compare: (T, T) -> Bool, _ i: Int) { - let l = Heap.leftIndex(i) - let r = Heap.rightIndex(i) + private mutating func heapify(_ index: Int) { + let left = self.leftIndex(index) + let right = self.rightIndex(index) var root: Int - if l <= (storage.count - 1) && compare(storage[l], storage[i]) { - root = l + if left <= (self.storage.count - 1) && self.comparator(storage[left], storage[index]) { + root = left } else { - root = i + root = index } - if r <= (storage.count - 1) && compare(storage[r], storage[root]) { - root = r + if right <= (self.storage.count - 1) && self.comparator(storage[right], storage[root]) { + root = right } - if root != i { - storage.swapAt(i, root) - heapify(storage: &storage, compare: compare, root) - } - } - - // named `MAX-HEAP-INSERT` in CRLS - private static func heapInsert(storage: inout ContiguousArray, compare: (T, T) -> Bool, key: T) { - var i = storage.count - storage.append(key) - while i > 0 && compare(storage[i], storage[parentIndex(i)]) { - storage.swapAt(i, parentIndex(i)) - i = parentIndex(i) + if root != index { + self.storage.swapAt(index, root) + self.heapify(root) } } // named `HEAP-INCREASE-KEY` in CRLS - private static func heapRootify(storage: inout ContiguousArray, compare: (T, T) -> Bool, index: Int, key: T) { + private mutating func heapRootify(index: Int, key: T) { var index = index - if compare(storage[index], key) { + if self.comparator(storage[index], key) { fatalError("New key must be closer to the root than current key") } - storage[index] = key - while index > 0 && compare(storage[index], storage[parentIndex(index)]) { - storage.swapAt(index, parentIndex(index)) - index = parentIndex(index) + self.storage[index] = key + while index > 0 && self.comparator(self.storage[index], self.storage[self.parentIndex(index)]) { + self.storage.swapAt(index, self.parentIndex(index)) + index = self.parentIndex(index) } } - // MARK: Swift interface using the low-level methods above public mutating func append(_ value: T) { - Heap.heapInsert(storage: &self.storage, compare: self.comparator, key: value) + var i = self.storage.count + self.storage.append(value) + while i > 0 && self.comparator(self.storage[i], self.storage[self.parentIndex(i)]) { + self.storage.swapAt(i, self.parentIndex(i)) + i = self.parentIndex(i) + } } @discardableResult @@ -144,23 +132,24 @@ internal struct Heap { return nil } let element = self.storage[index] + let comparator = self.comparator if self.storage.count == 1 || self.storage[index] == self.storage[self.storage.count - 1] { self.storage.removeLast() - } else if !self.comparator(self.storage[index], self.storage[self.storage.count - 1]) { - Heap.heapRootify(storage: &self.storage, compare: self.comparator, index: index, key: self.storage[self.storage.count - 1]) + } else if !comparator(self.storage[index], self.storage[self.storage.count - 1]) { + self.heapRootify(index: index, key: self.storage[self.storage.count - 1]) self.storage.removeLast() } else { self.storage[index] = self.storage[self.storage.count - 1] self.storage.removeLast() - Heap.heapify(storage: &self.storage, compare: self.comparator, index) + self.heapify(index) } return element } internal func checkHeapProperty() -> Bool { func checkHeapProperty(index: Int) -> Bool { - let li = Heap.leftIndex(index) - let ri = Heap.rightIndex(index) + let li = self.leftIndex(index) + let ri = self.rightIndex(index) if index >= self.storage.count { return true } else { @@ -204,8 +193,8 @@ extension Heap: CustomDebugStringConvertible { var all = "\n" let spacing = String(repeating: " ", count: maxLen) func subtreeWidths(rootIndex: Int) -> (Int, Int) { - let lcIdx = Heap.leftIndex(rootIndex) - let rcIdx = Heap.rightIndex(rootIndex) + let lcIdx = self.leftIndex(rootIndex) + let rcIdx = self.rightIndex(rootIndex) var leftSpace = 0 var rightSpace = 0 if lcIdx < self.storage.count { diff --git a/docker/docker-compose.1804.50.yaml b/docker/docker-compose.1804.50.yaml index e5b931228..5cca7355f 100644 --- a/docker/docker-compose.1804.50.yaml +++ b/docker/docker-compose.1804.50.yaml @@ -18,20 +18,22 @@ services: image: swift-nio:18.04-5.0 environment: - MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=31200 - - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=1177050 # was: 685050 + - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=1155050 # was: 685050 - MAX_ALLOCS_ALLOWED_ping_pong_1000_reqs_1_conn=4600 - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2100 - MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=99100 + - MAX_ALLOCS_ALLOWED_scheduling_10000_executions=20150 test: image: swift-nio:18.04-5.0 command: /bin/bash -cl "swift test -Xswiftc -warnings-as-errors && ./scripts/integration_tests.sh" environment: - MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=31200 - - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=1177050 # was: 685050 + - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=1155050 # was: 685050 - MAX_ALLOCS_ALLOWED_ping_pong_1000_reqs_1_conn=4600 - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2100 - MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=99100 + - MAX_ALLOCS_ALLOWED_scheduling_10000_executions=20150 echo: image: swift-nio:18.04-5.0