Skip to content

Commit

Permalink
Revert "Adopt UseEarlyExits"
Browse files Browse the repository at this point in the history
This reverts commit d1ac5bb.
  • Loading branch information
FranzBusch committed Jul 19, 2024
1 parent d1ac5bb commit 1aa8ba5
Show file tree
Hide file tree
Showing 90 changed files with 1,654 additions and 1,399 deletions.
2 changes: 1 addition & 1 deletion .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"UseEarlyExits" : true,
"UseEarlyExits" : false,
"UseExplicitNilCheckInConditions" : false,
"UseLetInEveryBoundCaseVariable" : false,
"UseShorthandTypeNames" : true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ private final class PongDecoder: ByteToMessageDecoder {
typealias InboundOut = UInt8

public func decode(context: ChannelHandlerContext, buffer: inout ByteBuffer) -> DecodingState {
guard let ping = buffer.readInteger(as: UInt8.self) else {
if let ping = buffer.readInteger(as: UInt8.self) {
context.fireChannelRead(Self.wrapInboundOut(ping))
return .continue
} else {
return .needMoreData
}
context.fireChannelRead(Self.wrapInboundOut(ping))
return .continue
}

public func decodeLast(
Expand Down
7 changes: 4 additions & 3 deletions Sources/NIOConcurrencyHelpers/atomics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,12 @@ public final class AtomicBox<T: AnyObject> {
return true
} else {
let currentPtrBits = self.storage.load()
guard currentPtrBits == 0 || currentPtrBits == expectedPtrBits else {
if currentPtrBits == 0 || currentPtrBits == expectedPtrBits {
sys_sched_yield()
continue
} else {
return false
}
sys_sched_yield()
continue
}
}
}
Expand Down
237 changes: 123 additions & 114 deletions Sources/NIOCore/AsyncSequences/NIOAsyncWriter.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,27 @@ extension NIOThrowingAsyncSequenceProducer {
):
self._state = .modifying

guard let element = buffer.popFirst() else {
if let element = buffer.popFirst() {
// We have an element to fulfil the demand right away.

let shouldProduceMore = backPressureStrategy.didConsume(bufferDepth: buffer.count)

self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: nil,
hasOutstandingDemand: shouldProduceMore,
iteratorInitialized: iteratorInitialized
)

if shouldProduceMore && !hasOutstandingDemand {
// We didn't have any demand but now we do, so we need to inform the delegate.
return .returnElementAndCallProduceMore(element)
} else {
// We don't have any new demand, so we can just return the element.
return .returnElement(element)
}
} else {
// There is nothing in the buffer to fulfil the demand so we need to suspend.
// We are not interacting with the back-pressure strategy here because
// we are doing this inside `next(:)`
Expand All @@ -1211,42 +1231,25 @@ extension NIOThrowingAsyncSequenceProducer {

return .suspendTask
}
// We have an element to fulfil the demand right away.

let shouldProduceMore = backPressureStrategy.didConsume(bufferDepth: buffer.count)

self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: nil,
hasOutstandingDemand: shouldProduceMore,
iteratorInitialized: iteratorInitialized
)

guard shouldProduceMore && !hasOutstandingDemand else {
// We don't have any new demand, so we can just return the element.
return .returnElement(element)
}
// We didn't have any demand but now we do, so we need to inform the delegate.
return .returnElementAndCallProduceMore(element)

case .sourceFinished(var buffer, let iteratorInitialized, let failure):
self._state = .modifying

// Check if we have an element left in the buffer and return it
guard let element = buffer.popFirst() else {
if let element = buffer.popFirst() {
self._state = .sourceFinished(
buffer: buffer,
iteratorInitialized: iteratorInitialized,
failure: failure
)

return .returnElement(element)
} else {
// We are returning the queued failure now and can transition to finished
self._state = .finished(iteratorInitialized: iteratorInitialized)

return .returnFailureAndCallDidTerminate(failure)
}
self._state = .sourceFinished(
buffer: buffer,
iteratorInitialized: iteratorInitialized,
failure: failure
)

return .returnElement(element)

case .cancelled(let iteratorInitialized):
self._state = .finished(iteratorInitialized: iteratorInitialized)
Expand Down Expand Up @@ -1296,10 +1299,11 @@ extension NIOThrowingAsyncSequenceProducer {
iteratorInitialized: iteratorInitialized
)

guard shouldProduceMore && !hasOutstandingDemand else {
if shouldProduceMore && !hasOutstandingDemand {
return .callProduceMore
} else {
return .none
}
return .callProduceMore

case .streaming(_, _, .some(_), _, _), .sourceFinished, .finished, .cancelled:
preconditionFailure("This should have already been handled by `next()`")
Expand Down
48 changes: 23 additions & 25 deletions Sources/NIOCore/ByteBuffer-aux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,13 @@ extension ByteBuffer {
@inlinable
mutating func _setStringSlowpath(_ string: String, at index: Int) -> Int {
// slow path, let's try to force the string to be native
guard
let written = (string + "").utf8.withContiguousStorageIfAvailable({ utf8Bytes in
self.setBytes(utf8Bytes, at: index)
})
else {
if let written = (string + "").utf8.withContiguousStorageIfAvailable({ utf8Bytes in
self.setBytes(utf8Bytes, at: index)
}) {
return written
} else {
return self.setBytes(string.utf8, at: index)
}
return written
}

/// Write `string` into this `ByteBuffer` at `index` using UTF-8 encoding. Does not move the writer index.
Expand All @@ -141,15 +140,14 @@ extension ByteBuffer {
// Do not implement setString via setSubstring. Before Swift version 5.3,
// Substring.UTF8View did not implement withContiguousStorageIfAvailable
// and therefore had no fast access to the backing storage.
guard
let written = string.utf8.withContiguousStorageIfAvailable({ utf8Bytes in
self.setBytes(utf8Bytes, at: index)
})
else {
if let written = string.utf8.withContiguousStorageIfAvailable({ utf8Bytes in
self.setBytes(utf8Bytes, at: index)
}) {
// fast path, directly available
return written
} else {
return self._setStringSlowpath(string, at: index)
}
// fast path, directly available
return written
}

/// Write `string` null terminated into this `ByteBuffer` at `index` using UTF-8 encoding. Does not move the writer index.
Expand Down Expand Up @@ -264,16 +262,15 @@ extension ByteBuffer {
public mutating func setSubstring(_ substring: Substring, at index: Int) -> Int {
// Substring.UTF8View implements withContiguousStorageIfAvailable starting with
// Swift version 5.3.
guard
let written = substring.utf8.withContiguousStorageIfAvailable({ utf8Bytes in
self.setBytes(utf8Bytes, at: index)
})
else {
if let written = substring.utf8.withContiguousStorageIfAvailable({ utf8Bytes in
self.setBytes(utf8Bytes, at: index)
}) {
// fast path, directly available
return written
} else {
// slow path, convert to string
return self.setString(String(substring), at: index)
}
// fast path, directly available
return written
}

// MARK: DispatchData APIs
Expand Down Expand Up @@ -847,12 +844,13 @@ extension Optional where Wrapped == ByteBuffer {
@discardableResult
@inlinable
public mutating func setOrWriteBuffer(_ buffer: inout ByteBuffer) -> Int {
guard self == nil else {
if self == nil {
let readableBytes = buffer.readableBytes
self = buffer
buffer.moveReaderIndex(to: buffer.writerIndex)
return readableBytes
} else {
return self!.writeBuffer(&buffer)
}
let readableBytes = buffer.readableBytes
self = buffer
buffer.moveReaderIndex(to: buffer.writerIndex)
return readableBytes
}
}
18 changes: 9 additions & 9 deletions Sources/NIOCore/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,14 @@ public struct ByteBuffer {
@inlinable
mutating func _setBytes<Bytes: Sequence>(_ bytes: Bytes, at index: _Index) -> _Capacity
where Bytes.Element == UInt8 {
guard
let written = bytes.withContiguousStorageIfAvailable({ bytes in
self._setBytes(UnsafeRawBufferPointer(bytes), at: index)
})
else {
if let written = bytes.withContiguousStorageIfAvailable({ bytes in
self._setBytes(UnsafeRawBufferPointer(bytes), at: index)
}) {
// fast path, we've got access to the contiguous bytes
return written
} else {
return self._setSlowPath(bytes: bytes, at: index)
}
// fast path, we've got access to the contiguous bytes
return written
}

// MARK: Public Core API
Expand Down Expand Up @@ -1191,10 +1190,11 @@ extension ByteBuffer {
/// - returns: The return value of `body`.
@inlinable
public mutating func modifyIfUniquelyOwned<T>(_ body: (inout ByteBuffer) throws -> T) rethrows -> T? {
guard isKnownUniquelyReferenced(&self._storage) else {
if isKnownUniquelyReferenced(&self._storage) {
return try body(&self)
} else {
return nil
}
return try body(&self)
}
}

Expand Down
10 changes: 6 additions & 4 deletions Sources/NIOCore/ByteBuffer-hexdump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,18 @@ extension ByteBuffer {
public func hexDump(format: HexDumpFormat) -> String {
switch format.value {
case .plain(let maxBytes):
guard let maxBytes = maxBytes else {
if let maxBytes = maxBytes {
return self.hexDumpPlain(maxBytes: maxBytes)
} else {
return self.hexDumpPlain()
}
return self.hexDumpPlain(maxBytes: maxBytes)

case .detailed(let maxBytes):
guard let maxBytes = maxBytes else {
if let maxBytes = maxBytes {
return self.hexDumpDetailed(maxBytes: maxBytes)
} else {
return self.hexdumpDetailed()
}
return self.hexDumpDetailed(maxBytes: maxBytes)
}
}
}
7 changes: 4 additions & 3 deletions Sources/NIOCore/ChannelOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,12 @@ extension ChannelOptions {
var hasSet = false
self._storage = self._storage.map { currentKeyAndValue in
let (currentKey, _) = currentKeyAndValue
guard let currentKey = currentKey as? Option, currentKey == newKey else {
if let currentKey = currentKey as? Option, currentKey == newKey {
hasSet = true
return (currentKey, (newValue, applier))
} else {
return currentKeyAndValue
}
hasSet = true
return (currentKey, (newValue, applier))
}
if !hasSet {
self._storage.append((newKey, (newValue, applier)))
Expand Down
5 changes: 3 additions & 2 deletions Sources/NIOCore/ChannelPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,11 @@ public final class ChannelPipeline: ChannelInvoker {
internal func _contextSync(_ body: (ChannelHandlerContext) -> Bool) -> Result<ChannelHandlerContext, Error> {
self.eventLoop.assertInEventLoop()

guard let context = self.contextForPredicate0(body) else {
if let context = self.contextForPredicate0(body) {
return .success(context)
} else {
return .failure(ChannelPipelineError.notFound)
}
return .success(context)
}

/// Returns a `ChannelHandlerContext` which matches.
Expand Down
15 changes: 9 additions & 6 deletions Sources/NIOCore/CircularBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,11 @@ extension CircularBuffer {
/// Returns the number of element in the ring.
@inlinable
public var count: Int {
guard self.tailBackingIndex >= self.headBackingIndex else {
if self.tailBackingIndex >= self.headBackingIndex {
return self.tailBackingIndex &- self.headBackingIndex
} else {
return self._buffer.count &- (self.headBackingIndex &- self.tailBackingIndex)
}
return self.tailBackingIndex &- self.headBackingIndex
}

/// The total number of elements that the ring can contain without allocating new storage.
Expand Down Expand Up @@ -537,10 +538,11 @@ extension CircularBuffer: RangeReplaceableCollection {
/// - Complexity: O(1)
@inlinable
public mutating func popFirst() -> Element? {
guard count > 0 else {
if count > 0 {
return self.removeFirst()
} else {
return nil
}
return self.removeFirst()
}

/// Removes and returns the last element of the `CircularBuffer`.
Expand All @@ -555,10 +557,11 @@ extension CircularBuffer: RangeReplaceableCollection {
/// - Complexity: O(1)
@inlinable
public mutating func popLast() -> Element? {
guard count > 0 else {
if count > 0 {
return self.removeLast()
} else {
return nil
}
return self.removeLast()
}

/// Removes the specified number of elements from the end of the
Expand Down
14 changes: 8 additions & 6 deletions Sources/NIOCore/Codec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,12 @@ extension B2MDBuffer {
var buffer = self.buffers.removeFirst()
buffer.writeBuffers(self.buffers)
self.buffers.removeAll(keepingCapacity: self.buffers.capacity < 16) // don't grow too much
guard buffer.readableBytes > 0 || allowEmptyBuffer else {
if buffer.readableBytes > 0 || allowEmptyBuffer {
self.state = .processingInProgress
return .available(buffer)
} else {
return .nothingAvailable
}
self.state = .processingInProgress
return .available(buffer)
case .ready:
assert(self.buffers.isEmpty)
if allowEmptyBuffer {
Expand Down Expand Up @@ -617,11 +618,12 @@ extension ByteToMessageHandler {
self.tryDecodeWrites()
continue
case .didProcess(.needMoreData):
guard self.queuedWrites.count > 0 else {
if self.queuedWrites.count > 0 {
self.tryDecodeWrites()
continue // we might have received more, so let's spin once more
} else {
return .didProcess(.needMoreData)
}
self.tryDecodeWrites()
continue // we might have received more, so let's spin once more
case .cannotProcessReentrantly:
return .cannotProcessReentrantly
}
Expand Down
Loading

0 comments on commit 1aa8ba5

Please sign in to comment.