185 Commits
Author SHA1 Message Date
Aryan Shah 2f58d8ef84 Revert configuration type for HTTP/1 server pipeline setup (#3775)
### Motivation:

We introduced a new configuration type in #3746 to make further
extensions to the server pipeline (#3745 for example) easier to
introduce.

The plan was to merge #3745 soon after #3746 was merged, because #3745
added a breaking change to the new config type.

However, #3745 needs some more consideration. We should therefore revert
the change in #3746 to unblock new releases from being made until we
come up with a better solution.
 
### Modifications:

A clean revert of PR #3746.

### Result:

Unblocks new releases.
2026-11-02 13:00:48 +00:00
Aryan Shah 108daca35d Add configuration type for HTTP/1 server pipeline setup (#3746)
### Motivation:

`NIOHTTP1.HTTPPipelineSetup` contains helper methods which add HTTP/1
client/server handlers to the provided channel pipeline.

Over time, we have had to duplicate these methods when introducing a new
configuration option in order to not break API.

As a result, there is now lots of duplication in
`NIOHTTP1.HTTPPipelineSetup` and introducing more configuration options
is very cumbersome. We should instead add a configuration type to solve
this problem.

### Modifications:

- Added new configuration types named
`NIOHTTPServerPipelineConfiguration`, and
`NIOHTTPServerPipelineSynchronousConfiguration`.
- Added a new pipeline setup helper method on `ChannelPipeline` and
`ChannelPipeline.SynchronousOperations` that requires the configuration
type as an argument.
- Deprecated all existing helper methods on `ChannelPipeline` and
`ChannelPipeline.SynchronousOperations`.

### Result:

Better API.
2026-10-19 13:23:19 +01:00
Gus Cairo 3d1ee9d45a Add and enable 6.4 workflows (#3750)
### Motivation

* Swift 6.4 has been released, so we should add it to our CI coverage.

### Modifications

* Update `apple/swift-nio/scripts/generate_matrix.sh`
* Update `apple/swift-nio/scripts/check-matrix-job.sh`
* Update reusable adopters of `swift_test_matrix.yml`
* Update end-user adopter workflows of `swift_test_matrix.yml`
* Add Swift 6.4 inputs to `macos_tests.yml` and `macos_benchmarks.yml`
* Add a 6.4 release job to `wasm_swift_sdk.yml`
* Enable the Windows 6.4 unit test job in `main.yml` and
`pull_request.yml`
* Copy benchmark thresholds from 6.3

### Result

* NIO runs Swift 6.4 CI, and downstream repositories can opt-in.

---

- Successful run (minus broken nightlies):
https://github.com/apple/swift-nio/actions/runs/36403423635?pr=3750
2026-09-30 10:05:07 +01:00
Fabian FettandCory Benfield f5b34b27cb Allow non-copyable ByteToMessageDecoders and typed throws (#3731)
### Motivation

Decoders are state machines and state machines are a natural fit for
non-copyable types. Today `ByteToMessageDecoder` and
`NIOSingleStepByteToMessageDecoder` require their conformers to be
copyable.

While we are here we also want `NIOSingleStepByteToMessageDecoder` to be
able to declare a concrete error type, so that callers can handle decode
errors without going through an existential.

### Changes
  
- Make `ByteToMessageDecoder` and `NIOSingleStepByteToMessageDecoder`
`~Copyable`.
- Add an `associatedtype DecodeError: Error = any Error` to
`NIOSingleStepByteToMessageDecoder` and spell decode and decodeLast as
`throws(DecodeError)`. Decoders that don't declare it keep behaving
exactly as before.
- Allow `~Copyable` `ByteToMessageDecoder` in `ByteToMessageHandler`.
- Since `~Copyable` types cannot be type erased into an existential
before macOS 15, we have to work around the existing `CanDequeueWrites`
dance: instead of casting `self` to `CanDequeueWrites` in
`handlerAdded`, ByteToMessageHandler gains an optional
`deliverQueuedWrite` closure. It is formed in the constrained
`ChannelOutboundHandler.write`, the only place where `Decoder`'s
`WriteObservingByteToMessageDecoder` conformance is statically known,
the first time a write arrives re-entrantly.
This has to be a closure rather than a protocol requirement on
`ByteToMessageDecoder`. `Decoders` may conform to
`WriteObservingByteToMessageDecoder` conditionally while conforming to
`ByteToMessageDecoder` unconditionally — `HTTPDecoder` does exactly this
— and all generic instantiations share one `ByteToMessageDecoder`
witness table, so such a requirement could only ever be witnessed by a
non-observing default and the write would be silently dropped.
- Split the delivery out of `tryDecodeWrites` into an `@inline(never)
dequeueWrites()`. `decodeLoop` calls `tryDecodeWrites` on every
iteration, so the empty-queue check has to stay cheap; keeping the
delivery inline tripled that function's size (50 → 141 instructions) and
showed up directly on the normal decode path.
- `ByteToMessageDecoderVerifier` allows `~Copyable`
`ByteToMessageDecoder`s.
- Added tests that use the newly lifted `~Copyable` restriction, plus
regression coverage for a decoder whose write-observing conformance is
conditional (the HTTPDecoder shape) and for constructing the handler in
a generic context.

### Result

Decoders may be non-copyable and may declare a concrete error type.
  
The normal decode path does not change performance. The re-entrant write
path costs one extra allocation per handler, but is meaningfully cheaper
in instructions. I'm happy to provide the benchmarks used to generate
those numbers in a separate pr – leaving them out here, as we would end
up with as many benchmarks as we have unit tests sooner or later.
  
Instructions, macOS arm64, per 1000 iterations, min of 5 interleaved
rounds:
  
```
  ┌──────────────────────────────┬────────────┬────────────┬───────────────────────────────────┐
  │                              │    main    │  this PR   │                                   │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ decode, no write observation │ 9,404,415  │ 9,360,896  │ −0.46% (noise ~0.8%, i.e. parity) │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ decode + re-entrant write    │ 13,656,063 │ 12,541,951 │ −8.16%                            │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ handler add / remove         │ 85,983,231 │ 83,361,791 │ −3.05% (noise ~2.2%)              │
  └──────────────────────────────┴────────────┴────────────┴───────────────────────────────────┘
```

Replacing the CanDequeueWrites existential with the closure is what
improves the reentrant write path: delivering a queued write is now a
direct closure call rather than a witness-table dispatch, and
handlerAdded no longer performs a runtime conditional-conformance
lookup.
The saving grows with the number of writes queued per decode — around
−16% with 8 queued writes, though that benchmark is noisier.

Allocations (nio-alloc-counter-tests, Swift 6.2):
  
```
  ┌────────────────────────────┬─────────┬─────────┬────────┐
  │                            │  main   │ this PR │        │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ 1000_reqs_1_conn           │ 27,341  │ 27,342  │ +1     │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ 1_reqs_1000_conn           │ 368,000 │ 369,000 │ +1,000 │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ ping_pong_1000_reqs_1_conn │ 296     │ 296     │ —      │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ decode_1000_ws_frames      │ 2,000   │ 2,000   │ —      │
  └────────────────────────────┴─────────┴─────────┴────────┘
```

Forming the closure captures Decoder.OutboundIn's metadata, so it
allocates once per handler that actually queues a re-entrant write —
1_reqs_1000_conn builds 1000 such handlers, hence +1000. main's
existential cast did not allocate, so this is a new cost rather than
staying flat. The 1_reqs_1000_conn threshold is raised by 1000 for
6.1/6.2/6.3; the nightly toolchains are unchanged (10,000 on both — they
specialize the closure away entirely).

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-09-17 13:44:37 +01:00
Fabian Fett 6a10931af5 Fix Integration Tests thresholds (#3732)
### Motivation:

Some CI is red that doesn't need to be red.

### Modifications:

- Update the nightly next thresholds for the integration tests – only
improvements.

### Result:

- Green CI for nightly next integration tests
2026-09-03 19:33:58 +02:00
Fabian Fett 28b1e07dd8 [Performance] Mark QUICBinaryEncodingStrategy.bytesNeededForInteger as @inlinable (#3718) 2026-09-03 10:40:24 +00:00
Mahdi Bahrami 63018571c8 [perf] Do stack allocs instead of heap allocs for small fixed-size allocations (#3652)
For small fixed-size allocations, we can use stack instead of heap.

### Motivation:

Allocations are costly. Stack allocations are much cheaper than heap
allocations.

### Modifications:

Use `withUnsafeTemporaryAllocation(of:capacity:_:)` instead of
allocating an `Array`.
`withUnsafeTemporaryAllocation` allocates on the stack for up to 1000
bytes.

### Result:

Better performance.
2026-07-24 12:13:48 +01:00
Johannes Weiss b1f2dd18fc rewrite NIOCrashTester in Swift Testing (#3658)
### Motivation:

The old NIOCrashTester doesn't work on Windows and would be hard to
port. Also, Swift Testing has wonderful 'exit test' ability, let's use
that!

### Modifications:

- Rewrite all NIOCrashTester tests in Swift Testing

### Result:

- Less code to maintain
- (hopefully) works on Windows
- fixes #3657
2026-07-13 15:12:55 +01:00
Fabian Fett 37e191d35b Strip Transfer-Encoding when Content-Length is present in HTTPEncoder (#3583)
Strip Transfer-Encoding when Content-Length is present in HTTPEncoder

### Motivation

When `correctlyFrameTransportHeaders()` detected a `Content-Length`
header, it returned `.contentLength` without removing any existing
`Transfer-Encoding` header.

### Changes

- Remove `Transfer-Encoding` header, if a `Content-Length` header is
present

### Result

NIO sends either a `Content-Length` header or a `Transfer-Encoding`
header, but not both.
2026-05-06 10:14:09 +00:00
Rick Newton-RogersandGeorge Barnett f99e1b6a30 Drop Swift 6.0 (#3566)
Motivation:

Swift 6.0 is no longer supported, we should bump the tools version and
remove it from our CI.

Modifications:

* Bump the Swift tools version to Swift 6.1
* Remove Swift 6.0 jobs where appropriate in main.yml, pull_request.yml
* Set language mode 5 in Benchmarks package manifest

Result:

Code reflects our support window.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-04-09 09:59:19 +00:00
Rick Newton-Rogers d80f3bac98 Add and enable Swift 6.3 workflows (#3565)
### Motivation

* Swift 6.3 has been released, we should add it to our CI coverage.

### Modifications

* Update `apple/swift-nio/scripts/generate_matrix.sh`
* Update `apple/swift-nio/scripts/check-matrix-job.sh`
* Update reusable adopters of `swift_test_matrix.yml`
* Update end-user adopter workflows of `swift_test_matrix.yml`
* Remove stale `NIGHTLY_6_1` fallback shims from `generate_matrix.sh`
* Migrate `linux_nightly_6_1_arguments_override` to
`linux_nightly_next_arguments_override` in `main.yml` and
`pull_request.yml`
* Copy benchmark thresholds from 6.2

### Result

* NIO tests against Swift 6.3 in CI and downstream repositories can
opt-in.
 

* A successful run using the new Swift 6.3 jobs:
https://github.com/apple/swift-nio/actions/runs/23841350537/job/69497505715?pr=3565
2026-04-01 13:06:15 +02:00
Fabian Fett 08abc3a02c Fix flaky testCopyFileReplacingExistingFileSucceeds (#3554)
### Motivation
`testCopyFileReplacingExistingFileSucceeds` (and
`testCopySymlinkReplacingExistingSymlinkSucceeds`) verify that no .tmp-
prefixed files are left behind after a `copyItem(replaceExisting: true)`
call on Linux. However, both tests create their files in the shared
system temp directory (e.g. /tmp) and then scan that entire directory
for .tmp- files. When other tests run in parallel, their in-flight
`copyItem(replaceExisting: true)` operations leave transient .tmp- files
in the same directory, causing spurious failures.

### Changes

- Both tests now create a dedicated subdirectory for their files, so the
temp file scan only sees files belonging to that specific test.
- Cleanup is handled via `addTeardownBlock` to ensure the subdirectory
is removed even if the test fails early.

### Results

The temp file assertions are no longer affected by concurrent test
activity or other processes writing to the shared temp directory,
eliminating the flakiness.
2026-03-18 15:25:06 +00:00
Fabian Fett ada5f7690b Improve NIOAsyncChannel performance with UnsafeContinuations (#3521)
Let's improve NIOAsyncChannels performance, by using UnsafeContinuations
instead of CheckedContinuations.

### Motivation:

We want to improve the performance for our async APIs.

### Modifications:

- Introduce a new typealias `NIOUnsafeContinuation` that behaves like a
UnsafeContinuation in release mode and like a CheckedContinuation in
debug mode.
- Use the `NIOUnsafeContinuation` inside the `NIOAsyncWriter`
- Use the `NIOUnsafeContinuation` inside the
`NIOThrowingAsyncSequenceProducer`

### Result:

Fewer allocations
2026-03-04 11:09:34 +00:00
Rick Newton-Rogers fb69e42d77 Add parallel execution mode for integration tests (#3522)
Add parallel execution mode for integration tests

### Motivation:

Integration tests regularly take 21m in CI; they're the long pole.

### Modifications:

* Allocation count tests are now run in parallel if
`NIO_ALLOC_COUNTER_TESTS_PARALLEL` is `true`, tests in a given file
are still in serial.
* Test output is catted to different files and recovered and printed in
the order in which the tests were started.

### Result:

Integration test execution (and therefore a PR's CI checks) goes from
21m to 10m
2026-02-27 11:47:06 +00:00
Fabian Fett 6a6f7d7c33 Centralize UnsafeTransfer in NIOCore (#3492)
We have a number of copies of `UnsafeTransfer` and two copies of
`UnsafeMutableTransferBox` in our code base. Before introducing more of
those, lets centralize to just one using a `package` access modifier.
2026-02-03 10:27:30 +00:00
Si Beaumont 37ffc4b8d5 Add API to enable throwing in EmbeddedChannel.getOption and .setOption if channel is closed (#3495)
### Motivation:

Channels based on `BaseSocketChannel` throw in both `getOption` and
`setOption` if the channel has been closed, since the `setsockopt` will
fail. However, the current behavior of `EmbeddedChannel` is options
remain writable and readable on closed channels.

There are situations where we'd like to be able to model the runtime
behaviour of the real channel in tests, e.g. to test this fix:
https://github.com/apple/swift-nio-extras/pull/304.

### Modifications:

- Add API to enable throwing in `EmbeddedChannel.getOption` and
`.setOption` if channel is closed.
- Add a test for this new behavior.

### Result:

- New API to enable throwing in `EmbeddedChannel.getOption` and
`.setOption` if channel is closed.
- No observable change for existing users of `EmbeddedChannel`.
2026-01-27 12:06:55 +00:00
aryan-25 b0895c094d Update integration test allocation thresholds for nightly-main (#3465)
### Motivation:

From
https://github.com/apple/swift-nio/actions/runs/20197120190/job/57982705638
onwards, the Integration Test benchmark runs have been failing for
`Linux (nightly-main)` due to allocations for seven tests falling below
the defined thresholds.

### Modifications:

Updated thresholds for the tests where the number of allocations were
out-of-threshold (seven in total).

### Result:

The Integration Test run for `Linux (nightly-main)` no longer fails.
2025-12-17 17:14:42 +00:00
Rick Newton-Rogers 56724a2b6d NIOAsyncTestingChannel local/remote addrs on EmbeddedChannelCore (#3442)
### Motivation:

`NIOAsyncTestingChannel` stored its `localAddress` and `remoteAddress`
in a locked storage on itself for thread safety, however in doing so
left us open to bugs because a handler grabbing the addresses of the
context had no visibility of the values.

### Modifications:

Reach into `EmbeddedChannelCore` for the addresses instead of storing
them on the `NIOAsyncTestinghannel`. I also considered a delegate
approach where the `EmbeddedChannelCore` could offload the
responsibility for storing the values back to the
`NIOAsyncTestingChannel` but it was complicated and of questionable
value.

### Result:

* The correct address values are seen no matter how they are obtained.
* We probably take a performance hit locking the values in this way but
this is testing code so probably not the end of the world.
2025-11-11 15:08:15 +00:00
Fabian Fett 6c114e3c62 Update CI scripts for 6.2 (#3374)
Replaces: #3373
2025-09-19 13:55:48 +01:00
George Barnett 9b2642c21b Use path based deps in the alloc counter framework (#3355)
The alloc counter framework uses `url` based deps for local
dependencies. They should use `path` instead.
2025-08-22 11:14:33 +01:00
George BarnettandCory Benfield 664032bdd6 Wrap closures stored in the CallbackList (#3303)
Motivation:

The compiler is better at optimising closures wrapped in nominal types
than raw closures when used as generic parameters. CallbackList is used
a lot and stores an optional closure as well as on optional array of
closures. Wrapping these internally should save some allocations.

Modifications:

- Wrap the element stored in the `CallbackList`.

Result:

Fewer allocs

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-07-14 16:48:26 +00:00
Cory Benfield 70439feacd Widen the allocation limit on nightlies (#3280)
Nightly CI fails occasionally because the allocs on low-overhead lists
occasionally break 20. Give ourselves some breathing room here.
2025-07-01 11:45:13 -07:00
Cory Benfield c9e2ac115f Adjust for SendableMetatype (#3266)
Motivation

With the introduction of isolated conformances, it has become necessary
to start managing the use of metatypes for some of our protocols. In
general, we don't want to force the relevant protocols to only be
conformed in non-isolated forms. Instead, we just want to make the
specific APIs non-usable.

Modifications

- Add shims for SendableMetatype that only use it when it is available.
- Require SendableMetatype where needed, gated by @preconcurrency.

Result

We continue to be safe.
2025-06-13 09:12:25 +01:00
Rick Newton-Rogers 57658e12ff Drop Swift 5.9 (#3228)
Motivation:

Swift 5.9 is no longer supported, we should bump the tools version and
remove it from our CI.

Modifications:

* Bump the Swift tools version to Swift 5.10
* Remove Swift 5.9 jobs where appropriate in main.yml, pull_request.yml

Result:

Code reflects our support window.
2025-05-07 12:24:44 +00:00
Rick Newton-Rogers b8c446368d Add and enable Swift 6.1 workflows (#3196)
Add and enable Swift 6.1 workflows

### Motivation:

Swift 6.1 has been released, we should add it to our CI coverage.

### Modifications:

* Update `appe/swift-nio/scripts/generate_matrix.sh`
* Update reusable adopters of `swift_test_matrix.yml`
* Update end-user adopter workflows of `swift_test_matrix.yml`
  * Copy over test flags from Swift 6.0 jobs

### Result:

NIO tests against Swift 6.1 in CI and downstream repositories can
opt-in.

(Successful CI run with the workflows modified to use the changes on
this branch
https://github.com/apple/swift-nio/actions/runs/14400598096?pr=3196)
2025-04-11 13:45:49 +01:00
Gus Cairo 7d3204d232 Improve alloc tests failure messages (#3173)
This PR improves the messaging when tests fail so it's easier to
troubleshoot failures.
2025-04-03 15:23:41 +00:00
Rick Newton-Rogers 26d40ebbda Delete Thresholds/nightly-6.1 symlinks (#3133)
Following on from https://github.com/apple/swift-nio/pull/3126 delete
`Benchmarks/Thresholds/nightly-6.1` and
`IntegrationTests/tests_04_performance/Thresholds/nightly-6.1.json`
which is no longer needed now that the shared benchmarks workflow has
been updated.
2025-03-07 12:09:53 +00:00
Rick Newton-Rogers 517dccac42 rename nightly 6 1 to nightly next (#3126)
Use nightly_next as swift version

see https://github.com/apple/swift-nio/pull/3122

Motivation:
To not have to rename threshold directories when the nightly branch
changes.

Modifications:
* Use nightly_next as swift version in the matrix generation script
which
is picked up by the benchmark script.
* Move nightly-next thresholds and add legacy symlink

Result:

Benchmark thresholds will attempt to find directories named
nightly_next, not nightly_6_1.
2025-03-04 09:37:47 +00:00
Clinton NkwochaandGeorge Barnett b14012bb1a Improve flatScheduleTask (#3079)
Motivation:

As pointed out in #3071, the `flatScheduleTask` implementations can be
improved.

Modifications:

- Refactor the `flatScheduleTask` implementations to skip `flatMap`
calls, which avoids creating an extra promise.
- As there is now a lower number of allocations, reduce the necessary
thresholds for the allocation tests.

Result:

Reduction in the number of allocations in the package.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2025-01-22 16:26:51 +00:00
Rick Newton-Rogers 9336e1f259 more 6.0 nightly to 6.1 nightly CI changes (#3078)
### Motivation:

Some changes were missed in #3076, passing through parameters for the
new 6.1 version.

### Modifications:

Pass through more parameters, clean up misleading comments.

### Result:

6.1 nightly runs will be more like 6.0 nightly runs were
2025-01-22 13:38:02 +00:00
Cory BenfieldandGeorge Barnett e2bfe0875b Adopt optimized isolated path for SelectableEL (#3075)
Motivation:

Users of SelectableEventLoop deserve to have their code that uses the
isolated event loop view perform at least as well as the code that
doesn't.

Modifications:

- Adopt the extra protocol witnesses.
- Refactor out common code.

Result:

Better performance

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2025-01-21 16:36:41 +00:00
bf41ad1595 Improve HTTPHeaders description performance (#3063)
### Motivation:

As outlined in this
[issue](https://github.com/apple/swift-nio/issues/2930) by @weissi, the
current performance of calling `description` on `HTTPHeaders` is
undermined by the dynamism of Array.

### Modifications:

The proposed solution replaces the implementation of `description` to
iterate over the items and print them out manually. This provides a
faster solution since we bypass the cost of calling into `description`
of an Array.


### Result:

A more performant implementation of `HTTPHeaders` description.

---------

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-01-20 16:39:21 +00:00
Cory BenfieldandGeorge Barnett 77c1b79c6c Move Isolated EL operations to EL protocol (#3070)
Motivation

Right now the isolated view on the EventLoop has to implement its
interface in terms of UnsafeTransfer, because there are no non-Sendable
operations it can use.

That hurts us because we have to allocate an extra closure for all these
operations, making them slower than they need to be. This is unavoidable
in some contexts, but most EventLoops would be able to offer a fast-path
that avoids this extra allocation.

To achieve that, we need to move this logic into a customisation point
on the EventLoop protocol. Our fallback default implementation can be
the same as we do today.

Modifications

- Add customisation points for EventLoop for each of its operations that
has a protocol witness already.
- Add default implementations of these, using the implementation from
EventLoop.Isolated.

Results

No behavioural changes, but the code has moved.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2025-01-20 14:06:21 +00:00
Cory Benfield 15a38aef06 Add allocation counter tests for isolated EL operations (#3068)
Motivation:

As with the ELF operations before them, the isolated EL operations
currently incur overhead above-and-beyond the overhead of their
non-isolated counterparts. That's not what we want to see. However,
before we "fix" them, we need to add regression testing to confirm our
fix actually worked.

Modifications:

- Add isolated variations of all current EL scheduling tests.
- Where isolated methods didn't have alloc counter tests, add new ones
for the non-isolated versions too.

Result:

More tests.
2025-01-17 10:31:53 +00:00
Cory Benfield 6ac01d1734 Clean up sendability in the bootstraps (#3051)
### Motivation:

The bootstraps are the next most obvious target for cleaning up
Sendability issues. These issues are mostly just missing annotations,
but there are a few places where we had actual latent threading bugs
that were missed. A lot more of the control flow is made more explicit
in this patch, and in general it should get a lot easier to be confident
that the code is correct.

### Modifications:

- Add necessary `@Sendable` annotations
- Clean up some incorrect `self` captures of bootstraps, which are not
`Sendable`, and which could lead to real threading bugs
- Make a few methods `static` to avoid needing to capture `self` at all.
- Make a few threading assumptions clear by using the isolated views or
the sync operations
- Add some missing `Sendable` constraints on interior generic functions.

### Result:

Better safety, better correctness in the bootstraps.
2025-01-14 20:49:33 +00:00
Cory Benfield e19202cb01 Avoid abstraction overheads on isolated ELF (#3055)
Motivation:

Unfortunately, closure composition is really expensive: closures that
capture closures always heap allocate. To make ELF.Isolated perform
well, then, we need to inline the method bodies directly.

Modifications:

- Add some isolated functions into ELF for enqueueing callbacks.
- Inline the implementation of the ELF methods into the isolated view.

Result:

Allocation counts match between isolated/nonisolated.
2025-01-13 20:31:22 +00:00
Cory Benfield 588523e7d6 Test allocations of isolated ELF views (#3054)
Motivation:

We've added isolated views onto EventLoopFutures. These are great, but
as initially implemented they add a bunch of overhead to the nonisolated
versions. That's not optimal, but before we change the code we should
make sure we have a benchmark baseline.

Modifications:

- Extend the test_future_lots_of_callbacks alloc test to add all the
callbacks that the isolated views have.
- Duplicate that test to one that adds isolated wrappers so we can
compare.

Result:

Two regression tests whose allocation counts should be equal, but
aren't.
2025-01-13 17:44:49 +00:00
Cory Benfield 2d72ada999 Get NIOEmbedded clean under strict concurrency (#3030)
Motivation:

NIOEmbedded is used all over NIO-land for testing various pieces of the
infrastructure, and so requires a substantial audit for strict
concurrency.

Modifications:

- Mark a few things Sendable.
- Fix the tests, which actually did have some nasty bugs

Result:

Sendable-clean NIOEmbedded
2024-12-17 09:49:06 +00:00
Cory Benfield 8126cba712 Avoid converting array holding existentials (#3006)
Motivation:

When converting an Array that holds existentials, it is necessary for
Swift to allocate a new Array and copy the elements into it, so that it
can fix up their existential boxes. We forced this to happen with taking
a `[ChannelHandler]` to `[ChannelHandler & Sendable]`, without rewriting
the other functions.

The result of that was that the other functions needed to convert the
channel handler types, causing extra allocations in this path.

Modifications:

Propagate the constraint down to the point where we iterate the Array.

Result:

Allocations reduced.
2024-11-28 11:30:54 +00:00
Peter Adams e1d0d21391 Correct allocation test script (#2956)
Motivation:

Unfortunately the current script has a bug meaning it can never fail.

Modifications:

Change bracketing for output search.

Result:

Allocation tests will flag when failing
2024-10-31 15:15:24 +00:00
Ayush Garg 7840b6b067 ChannelOption: Allow types to be accessed with leading dot syntax (#2816)
Motivation:

Since Swift 5.5 and [SE-0299](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0299-extend-generic-static-member-lookup.md) it is possible to add static members to protocols which are discoverable through the shorthand dot syntax.
This change reduces type repetition and improves call-site legibility.

Modifications:

Added extensions for ChannelOption with static members where Self is bound to a concrete type.

Result:

ChannelOption types can be used with the leading dot syntax. For eg:

```
//before
.channelOption(ChannelOptions.explicitCongestionNotification, value: true)
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)

//after
.channelOption(.explicitCongestionNotification, value: true)
.channelOption(.socketOption(.so_reuseaddr), value: 1)
```
2024-07-30 15:19:59 +00:00
Franz Busch f17f7e5909 [CI] Add shellcheck and fix up warnings (#2809)
# Motivation

We have quite a lot of shell scripts in our repo and want to make sure that they all pass `shellcheck`.

# Modification

This PR adds a GH action workflow to the soundness script for `shellcheck` and fixes up all errors and warnings.

# Result

No more shell/bash discussions
2024-07-25 14:55:44 +00:00
Si Beaumont 55d4c49334 Better align shutdown semantics of testing event loops (#2800)
### Motivation:

The two testing event loops—`EmbeddedEventLoop` and `NIOAsyncTestingEventLoop`—have different semantics for outstanding work during shutdown, which are both different from the production `SelectableEventLoop`, specifically with newly scheduled tasks that result from running existing scheduled work at the time of shutdown.

There are three axes to consider:

1. Scheduled tasks that are at or past their deadline.
2. Scheduled tasks with a deadline in the future.
3. Newly scheduled work that result from either running or cancelling (1) and (2).

| | (1) | (2) | (3) |
|-|-|-|-|
| `SelectableEventLoop` | Run | Cancel | Quiesce over 1000 ticks, repeatedly run resulting (1) and cancel resulting (2) |
| `EmbeddedEventLoop` | Run | Cancel | Cancel resulting (1) and resulting (2) |
| `NIOAsyncTestingEventLoop` | Run | Run | Run resulting (1) and resulting (2) |

Note that `NIOAsyncTestingEventLoop` may never terminate because of this.

### Modifications:

This PR aligns `EmbeddedEventLoop` and `NIOTestingEventLoop` and makes them more similar to `SelectableEventLoop` and less surprising semantics.

### Result:

| | (1) | (2) | (3) |
|-|-|-|-|
| `SelectableEventLoop` | Run | Cancel | Quiesce over 1000 ticks, repeatedly run resulting (1) and cancel resulting (2) |
| `EmbeddedEventLoop` | Run | Cancel | Cancel resulting (1) and resulting (2) |
| `NIOAsyncTestingEventLoop` | Run | Cancel | Cancel resulting (1) and resulting (2) |

### Future work:

Given both the `EmbeddedEventLoop` and `NIOAsyncTestingEventLoop` are used in tests of NIO applications that run with `SelectableEventLoop` in production, it might be worth extending both to also support quiescing to give a more representative shutdown behaviour in tests.
2024-07-25 14:38:36 +00:00
Franz Busch c9756e1083 Adopt swift-format (#2794)
* Apply formatting

* Apply no block comments rule

* Apply OmitExplicitReturns

* Apple OnlyOneTrailingClosureArgument

* Apply NoAssignmentInExpressions

* Fix up DontRepeatTypeInStaticProperties lint errors

* Apply `OrderedImports`

* Apply `ReplaceForEachWithForLoop`

* format file

* Enable the formatting pipeline

* Adopt `AmbiguousTrailingClosureOverload`

* Fix license header

* Fix format check

* Fix `EndOfLineComment`

* Fix CI

* Adapt CI script to check if changes when running formatting

* Separate lint and format into to steps

* Fix format

* Adopt `UseEarlyExits`

* Revert "Adopt `UseEarlyExits`"

This reverts commit d1ac5bbe12.
2024-07-19 11:48:17 +02:00
George Barnett 980bd3e630 Disable warnings as errors on Swift 6 and main (#2793)
Motivation:

The Swift 6 compiler emits a number of warnings about sendability which
we haven't dealt with yet. The CI failing on warnings may mask other
compilation issues or test failures.

Modifications:

- Disable warnings as errors on Swift 6 and main

Result:

Swift 6 and main CI should pass
2024-07-18 12:10:50 +00:00
Johannes Weiss 7948ed2104 ChannelHandler: provide static (un)wrap(In|Out)bound(In|Out) (#2791) 2024-07-18 11:55:48 +01:00
Franz Busch 8a69bd9d88 [GHA] Cxx interoperability compatibility and integration tests check (#2790)
# Motivation

Another reusable check is to make sure that all library products of a package are successfully building when consumed from a module that has Cxx interoperability enabled. Another check that's missing is running the integration tests.

# Modification

This PR adds two new checks to the reusable workflow. One to check for Cxx interoperability compatibility and another one to run the integration tests. I also fixed a misalgined name for the nightly benchmarks.

# Result

This should be one of the last reusable workflow checks.
2024-07-16 01:22:04 -07:00
Franz Busch 5cc05490c4 [GHA] Unacceptable language check (#2766)
* [GHA] Unacceptable language check

# Motivation

Next up replacing part of our soundness script that checked for unacceptable language.

# Modification

This PR adds a new GH action that checks for unacceptable language.

# Result

One more script replaced

* PR review

* Change homepage link to repo
2024-07-09 07:56:26 +01:00
Gregor Milos (Grzegorz Miłoś) fc63f0cf4e Fix memory allocations counters on macOS. (#2673)
Motivation:

Allocation counters are broken on macOS as described [here](https://github.com/apple/swift-nio/issues/2672). This PR fixes it.

Modifications:

`AtomicCounter` library is made `.dynamic`. This deduplicates the two (static) copies that used to exist. One embedded in the main binary, and the other in `HookedFunctions` dylib. This deplication fixes the issue, because there is just one copy of the counters in the process address space.

Result:

All the tests counting allocations and related statistics work on macOS.
2024-03-06 14:46:50 +00:00
Gustavo Cairo 0c648c91c6 Fix broken tests (#2621)
* Update swift-tools-version to 5.7 in integration tests

* Fix flaky test

* Add missing AtomicCounter dependency in generated Package.swift

* Update swift-tools-version in integration test helper Swift projects

* Make test clearer

* Don't run MTELG integration test on Swift 5.11+

It hasn't been properly tested
2024-01-08 10:02:09 +00:00