Commit Graph
128 Commits
Author SHA1 Message Date
David Evans b0effbcfd9 Make Swift 5.2 the minimum requirement (#1860)
Make Swift 5.2 the minimum requirement, dropping support for Swift 5.0 and 5.1.

Motivation:

Whenever we have problems, Swift 5.0 and 5.1 seem to be the culprits. Dropping support for these very old versions will require less maintenance and free up our time to work on new features.

Modifications:

Set the tools version in Package.swift to 5.2
Remove CI configurations for 5.0 and 5.1
Update the various readmes to reflect that this change will be rolled out in NIO 2.30.0
Result:

Swift 5.2 is the minimum version of Swift required to use NIO.
2021-06-18 21:02:41 +01:00
Johannes Weiss b23f40b4fa add Swift 5.5 job (#1845) 2021-06-01 18:58:56 +01:00
Cory Benfield 43fa7686db Add alloc counter benchmark for result erasing maps. (#1858)
Motivation:

Peter thinks that result-erasing maps should not allocate, and we have
special code paths in the code to try to make Void -> Void maps not
allocate. Sadly, both code paths currently do allocate.

Per our rules for not trying to make optimizations without data, we
should start measuing these closures so we can make optimizations.

Modifications:

- Added an alloc couter test for result-erasing maps.

Result:

Alloc counter test suitable for any fix of #1697.
2021-05-25 12:56:38 +01:00
tomer doron aa9cfb6fb6 fix 5.4. docker setup (#1851)
motivation: update syntax for release images

changes: replace use of "base_image" with "ubuntu_version" and "swift_version" pair, which the intended way to use release images
2021-05-07 08:30:27 +01:00
Johannes Weiss b27b08a828 docker: use 5.4 release instead of nightly (#1843) 2021-05-04 16:21:44 +01:00
Cory Benfield 8369411d6f Stop measuring alloc counts on 5.0 (#1826)
Motivation:

5.0 is several years old now, and while we still support it there is
beginning to be some conflict with performance optimizations for newer
Swift versions. We should stop counting the allocation counts on this
version now: we no longer care as much about regressions.

Modifications:

- Stop setting alloc counter limits on 5.0
- Remove the script that tries to add them back.

Result:

No longer measure allocs on 5.0
2021-04-28 14:02:13 +01:00
Johannes WeissandCory Benfield 0a860b1c3d CI: print out version info (#1821)
Motivation:

Sometimes it's useful to know the kernel & Swift versions that CI uses.

Modifications:

Print the versions in the soundness check.

Result:

More info.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-04-27 13:34:54 +01:00
Fabian Fett e08f9f903e Reducing allocations in future chains (#1818) 2021-04-26 13:54:26 +01:00
Fabian Fett 72f5a56328 Less allocs during flatMapErrorThrowing (#1816)
Motivation:

We alloc quite a lot with our implementations of flatMapThrowing and flatMapErrorThrowing.
While we don't use Futures a lot in NIO itself a lot of our users, depend quite a bit on them. Let's make their code faster.

Modifications:

Create a Promise and use _whenComplete directly instead of going through another flatMap method

Result:

In my testing I see a reduction of 3 allocs per invocation 🎉
2021-04-23 19:27:38 +01:00
Johannes Weiss 06ce11fb82 generic integer bit packing (#1807)
Motivation:

With the registration ID refactoring (#1801) we'll now have the third
reimplementation of integer bit packing. Three is one too many, let's
make it generic.

Modifications:

Introduce generic integer bit packing and use it for _UInt56/24.

Result:

- Less bit packing code.
- Will simplify #1801
2021-04-22 15:02:47 +01:00
Johannes Weiss a305518d88 implement CircularBuffer.first: to prevent allocs (#1814)
Motivation:

Due to https://bugs.swift.org/browse/SR-14516 , we sometimes get
allocating (!?) `subscript.read` accessors in the CircularBuffer.first
depending on the `Element` type...

Modifications:

Implement `CircularBuffer.first` instead of inheriting it from
Collection.

Result:

Fewer allocs in some cases.
2021-04-22 13:34:14 +01:00
Johannes Weiss ce4328689c disable tests on main (#1790)
Motivation:

The Swift compiler can still not compile NIO's tests on main, so I'm
proposing this sad workaround for
https://bugs.swift.org/browse/SR-14268 .

Modifications:

Don't compile (or run) tests on `main` :(

Result:

May get a green tick.
2021-03-30 16:05:07 +01:00
Johannes Weiss 2d27890eb8 normalise alloc counter slack (#1789)
Motivation:

Usually, we add a more or less random number of slack allocations to
make sure the tests don't spuriously fail. This makes it quite costly to
support new Swift versions.

Modifications:

Add a script which can spit you out the right allocation limits
including slack.

Result:

Easier to support new Swift versions
2021-03-30 14:42:33 +01:00
Johannes WeissandCory Benfield 589a16e70b docker setup for main nightlies (#1752)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-03-23 13:01:14 +00:00
Cory Benfield 6c18d23930 Make ChannelHandler removal cheaper. (#1784)
Motivation:

Any version of ChannelHandler removal that does not have a
ChannelHandlerContext already in hand is currently excessively
expensive. This is because it allocates a promise and a callback for
finding the context, despite already having a promise in hand for users
to complete.

We can remove a pair of allocations here by jumping to the event loop
directly and then running our operations synchronously.

Modifications:

- Rewrite removeHandler(name:promise:) and removeHandler(_:promise:) to
  jump directly to the event loops and then work synchronously.

Result:

Cheaper code
2021-03-18 15:43:21 +00:00
Cory Benfield c3479df8b7 Add allocation counter tests handler add/remove (#1785)
Motivation:

Allocation counter tests are good, and we aren't measuring this today.

Modifications:

- Wrote some add/remove tests that use different remove functions.

Result:

Better insight into performance.
2021-03-18 14:42:51 +00:00
George Barnett e2b39de23b Add synchronous helpers for HTTP1 pipeline setup (#1762)
Motivation:

We recently added a synchronous view of the `ChannelPipline` so that
callers can avoid allocating futures when they know they're on the right
event loop. We also offer convenience APIs to configure the pipeline for
particular use cases, like an HTTP/1 server but we don't have
synchronous versions of these APIs yet. We should have parity
between as synchronous and asyncronous APIs where feasible.

Modifications:

- Add synchronous helpers to configure HTTP1 client and server pipelines

Result:

Callers to synchronously configure HTTP1 client and server pipelines.
2021-03-04 12:04:26 +00:00
George Barnett 178636d999 Use the pre-succeeded void future when possible for async pipeline operations (#1766)
Motivation:

We added synchronous pipeline operations to allow the caller to save
allocations when they know they are already on the correct event loop.
However, we missed a trick! In some cases the caller cannot guarantee
they are on the correct event loop and must use an asynchronous method
instead. If that method returns a void future and is called on the event
loop, then we can perform the operation synchronously and return a
cached void future.

Modifications:

- Add API to `EventLoop` for creating a 'completed' future with a
  `Result` (similar to `EventLoopPromise.completeWith`)
- Add an equivalent for making completed void futures
- Use these when asynchronously adding handlers and the caller is
  already on the right event loop.

Result:

- Fewer allocations on the happiest of happy paths when adding handlers
  asynchronously to a pipeline.
2021-02-25 13:08:00 +00:00
Johannes Weiss 9b230f89a4 TSan is still broken on != Ubuntu 16.04 (#1764) 2021-02-24 08:41:27 +00:00
George Barnett 9451f4ac6f Add synchronous channel options (#1755)
* Add synchronous channel options

Motivation:

The functions for getting and setting channel options are currently
asynchronous. This ensures that options are set and retrieved safely.
However, in some cases the caller knows they are on the correct event
loop but still has to pay the cost of allocating a future to either get
or set an option.

Modifications:

- Add a 'NIOSynchronousChannelOptions' protocol for getting and setting
  options
- Add a customisation point to 'Channel' to return 'NIOSynchronousChannelOptions'.
- Default implementation returns nil so as to not break API.
- Add implementations for 'EmbeddedChannel' and 'BaseSocketChannel'
- Allocation tests for getting and setting autoRead

Results:

Options can be get and set synchronously.
2021-02-22 11:07:30 +00:00
Johannes Weissandtomer doron 8f531c3e02 Swift 5.4 docker setup (#1750)
Co-authored-by: tomer doron <tomer@apple.com>
2021-02-19 09:42:27 +00:00
George Barnett 63d356819c Add synchronous pipeline operations (#1741)
Motivation:

`ChannelPipeline` is explicitly thread-safe, any of the operations may
be called from outside of the channel's event loop. However, there are
often cases where it is known that the caller be on the right event
loop, and an asynchronous API is unnecessary.

In some cases -- such as when a pipeline is configured dynamically and
handlers are added from the 'channelRead' implementation of one handler
-- it forces the caller to write code that they might not actually need:
such as buffering events which may happen before the future completes.
This is unnecessary complexity when the caller knows that they must
already be on an event loop.

Modifications:

- Add a 'SynchronousOperations' view to the 'ChannelPipeline' which is
  available to callers via 'syncOperations'.
- Supported operations include: adding a handler, adding multiple
  handlers, retrieving a context via various predicates and retrieving a
  handler of a given type.
- Some of the operations in 'ChannelPipeline' were refactored to have an
  explicitly synchronous version, asynchronous versions complete their
  promise based on the result of these calls.
- Various minor documentation fixes and addition of 'self' where it was
  not used explicitly.

Result:

Users can perform synchronous operations on the 'ChannelPipeline' if
they know they are on the right event loop.
2021-02-09 09:59:41 +00:00
Johannes Weiss ef9e98a592 align all functions to make micro benchmarks more stable (#1739)
Motivation:

After chatting to the Swift perf team, they thought it may be a good
idea to align all functions in our performance tests which may make the
micro benchmarks more stable.

Modifications:

Align all functions/blocks.

Result:

Hopefully more stable micro benchmarks.
2021-01-29 08:32:08 +00:00
Fabian Fett f745421d49 Final language fix (#1732) 2021-01-22 18:39:10 +00:00
Fabian Fett 76b4637122 Make language more welcoming (#1728) 2021-01-21 12:45:46 +00:00
Johannes Weiss 2b821b2ef6 Specialise succeeded Void futures so we allocate less (#1703)
Motivation:

Succeeded `EventLoopFuture<Void>`s are quite important in SwiftNIO, they
happen all over the place. Unfortunately, we usually allocate each time,
unnecessarily.

Modifications:

Offer `EventLoop`s the option to cache succeeded void futures.

Result:

Fewer allocations.
2021-01-15 17:07:15 +00:00
Johannes Weiss 6b4a8197f9 order perf tests by name in docker files (#1720)
Motivation:

When you make a change that affects many performance tests, it's often
easier to just copy the results from CI. Unfortunately, that makes the
diff hard to read because the order is arbitrary.

Modifications:

Sort the list so you can always easily get to the same order as the
docker file by using `| sort` or `:sort` in vim.

Result:

Easier to update perf tests.
2021-01-14 19:03:59 +00:00
Peter Adams f4c87c800d Remove an allocation from addHandlers (#1710)
* Add allocation test for adding multiple handlers

Motivation:

I believe there is at least 1 avoidable allocation in this area.
Even if there isn't, making sure we don't increase allocations is good.

Modifications:

Add a test of allocations when adding multiple handlers.
Set limits for docker images.

Result:

Allocations when adding multiple handlers are now checked.

* Remove an allocation from addHandlers

Motivation:

Fewer allocations should improve performance.

Modifications:

Split out a sub function from addHandlers.

I originally thought I'd have to change the part of this
function which reads `var handlers = handlers` as there was
a surprising allocation at the beginning of this function.
It seems that breaking out some of the logic is sufficient
to remove an allocation.

Result:

1 fewer allocation.

* Fix up alloc tests.
2020-12-21 13:54:28 +00:00
Cory Benfield 076fda1521 Use CircularBuffer in EmbeddedChannel. (#1700)
Motivation:

When running load through EmbeddedChannel we spend an enormous amount of
time screwing around with removing things from Arrays. Arrays are not a
natural data type for `removeFirst()`, and in fact that method is
linear-time on Array due to the need for Array to be zero-indexed. Let's
stop using (and indeed misusing) Array on EmbeddedChannel.

While we're here, if we add some judicious @inlinable annotations we can
also save additional work generating results that users don't need.

Modifications:

- Replace arrays with circular buffers (including marked versions).
- Avoid CoWs and extra allocations on flush.
- Make some API methods inlinable to make them cheaper.

Result:

- Much cheaper EmbeddedChannel for benchmark purposes.
2020-12-02 16:01:21 +00:00
Peter Adams 91eab6d836 Add multiple channel handlers in a single async call. (#1683)
Motivation:

Curently adding multiple channel handlers makes a call to the
async version of addHandler for each handler resulting in
n+1 futures.  It feels better to use just one future and add
all the handlers synchoronously.

Modifications:

Change sync functions with take a promise to instead return a Result.
Feed this back until reaching addHandlers.

Result:

Multiple handlers can be added more quickly.
2020-10-27 16:14:51 +00:00
tomer doron d9d4918333 remove symbolicate-linux-fatal from Docker (#1670)
motivation: we are not actually using symbolicate-linux-fatal in any meaningful way in CI and it's pinned to the master branch which has been removed

changes: remove symbolicate-linux-fatal fetching from Docker
2020-10-08 13:16:12 +01:00
Cory Benfield e7880565aa We use main as our development branch now. (#1651) 2020-09-24 18:02:44 +01:00
Cory Benfield 06554fd48e Swift 5.3-RELEASE is out, let's use it. (#1631) 2020-09-21 12:09:47 +01:00
Peter Adams 3cfb3f1d6e Don't install Jazzy on Xenial (#1597)
Motivation:

Ruby on Xenial is too old for the cocoapods downloader which jazzy uses.

Modifications:

Change Dockerfile to not install Jazzy on xenial.

Result:

Dockerfile will build again, but bionic or later is required for docs.
2020-07-21 17:52:23 +01:00
Peter Adams 8a865bd15e Make UDP connection allocation test more like best practice code. (#1583)
Motivation:

There is at least a theoretical race to flush before close in prior version.
Having terrible code in the NIO repo is asking for someone to copy it.

Modifications:

flatMap the various parts of the client together which also ensures the
flush is complete before close is called.

Result:

Slightly nicer code, slightly fewer allocations.
2020-06-30 17:11:05 +01:00
Peter Adams a035a1b154 UDP allocation tests should not use so_reuseaddr (#1581)
Motivation:

so_reuseaddr on linux allows multiple binding to the same port simultaneously - this has not been seen to happen in practice but it is best to be cautious.

Modifications:

Remove address reuse from the UDP allocation tests.

Result:

Reduced danger of binding the same port twice.
2020-06-29 16:51:42 +01:00
669a19cb7b Set SO_LINGER in integration tests. (#1549)
Motivation:

We now have sufficiently many integration tests that the number of TCP
sockets in TIME_WAIT begins to cause resource pressure on the system.
This can lead to the integration tests failing due to being unable to
assign a socket.

We can force this to not happen by disabling linger. As we're running on
a lossless fabric (localhost), this is entirely safe.

Modifications:

- Disable TIME_WAIT for integration tests.

Result:

Integration tests will pass.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: Peter Adams <pp_adams@apple.com>
2020-06-24 14:28:40 +01:00
Johannes Weiss 01e0e0cef4 stablise 1000 TCP connections alloc test (#1566)
Motivation:

The 1000 TCP connections allocation counter test did way too many thread
crosses and other things that allocated for unrelated reasons.

Modifications:

Remove those.

Result:

Much more stable
2020-06-23 13:11:43 +01:00
Peter Adams c03dd9c5f1 Make allocation tests produce more stable results (#1565)
Motivation:

Allocation tests are very hard to use productively if they don't produce stable results.

Modifications:

Change TCP and UDP connection tests to monitor on the server side as well as the client side to make sure all data is completely send and received before stopping counting allocations.

Result:

Allocation tests are more stable than before.
2020-06-23 10:25:36 +01:00
Johannes WeissandCory Benfield e57122bd89 ByteBuffer: add convenience initialisers (#1533)
Motivation:

There are multiple sub-optimal ByteBuffer creation patterns that occur
in the wild. Most often they happen when people don't actually have
access to a `Channel` just want to "convert" a `String` into a
`ByteBuffer`. To do this, they are forced to type

    var buffer = ByteBufferAllocator().buffer(capacity: string.utf8.count)
    buffer.writeString(string)

Sometimes, they don't get the capacity calculation right or just put a
`0`.

Similar problems happen if NIO users want to cache a ByteBuffer in their
`ChannelHandler`. You will then find this code:

```swift
if self.buffer == nil {
    self.buffer = receivedBuffer
} else {
    var receivedBuffer = receivedBuffer
    self.buffer!.writeBuffer(&receivedBuffer)
}
```

And lastly, sometimes people want to append one `ByteBuffer` to another
without mutating the appendee. That's also cumbersome because we only
support a mutable version of `writeBuffer`.

Modifications:

- add `ByteBuffer` convenience initialisers
- add convenience `writeBuffer` methods to `Optional<ByteBuffer>`
- add `writeBufferImmutable` which doesn't mutate the appendee.

Result:

More convenience.

Co-authored-by: Cory Benfield <lukasa@apple.com>

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-06-04 21:02:11 +01:00
Peter Adams 1d134ccdcc Fine grained UDP allocation tests (#1534)
Fine grained UDP allocation tests

Motivation:

The existing UDP allocation tests count allocations everywhere.

Split out 3 tests to cover:

Making connections.
Making bootstraps.
Transferring data.
Modifications:

Three new tests.

Result:

Easier to debug changes to allocation profiles in UDP.
2020-05-29 12:49:34 +01:00
Peter Adams 51370e4113 Finer grained allocation tests (#1531)
Motivation:

The existing tcp allocation tests cover everything from bootstrap creation through to sending data in one massive test. The parameters of how many iterations control the focus of the test. It would be better to be more explicit about what we're trying to test.

Modifications:

2 new tests:
   * client bootstrap creation.
   * connection establishment.
 (Data transfer is already covered by the ping_pong test)

Result:

Easier to assess the impact of changes to memory allocation patterns.
2020-05-28 16:17:20 +01:00
Peter AdamsandCory Benfield b174051b81 Supply allocation limits for UDP allocation tests (#1523)
Motivation:

Values were previously missing (5.3) or incorrectly specified (5.1)
meaning that allocations were not checked in for these swift versions.

Modifications:

Correct variable name in 5.1, add missing variables in 5.3 docker files.

Result:

UDP allocations will be checked.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-18 08:34:56 +01:00
9afac5ecda UDP based allocation tests (#1496)
UDP based allocation tests (#1496)

Motivation:

With future work being considered in the UDP space it will be useful to have a baseline measure of allocations.

Modifications:

New allocation tests for UDP -
Ideas inspired by the TCP allocation tests.
Code a modified version of UDPEchoServer and UDPEchoClient examples.

Result:

There are UDP based allocation tests.

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-05-04 11:36:23 +01:00
David Evans 8da802c5eb Add 5.3 CI (#1498) 2020-05-01 19:17:44 +01:00
Johannes WeissandCory Benfield 2ad77f9a9b WIP: universal bootstrap (#1323)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-18 16:20:19 +00:00
Johannes Weiss a8fe2f343b SR-12094 is fixed, no need to skip tests (#1371) 2020-02-01 10:28:06 +00:00
tomer doron 25583d5ad8 improve docker security (#1375)
motivation: more secured ci setup

changes:
* enable :z selinux flag on bind mounts so we can enable selinux on ci
* drop potentially exploitable capabilities from docker-compose
2020-02-01 07:40:06 +00:00
George Barnett b4483e5c49 Avoid curried thunks (workaround SR-12115) (#1374)
Motivation:

Calling `function(otherFunction)` allocates, but
`function({ otherFunction($0) }` does not.

See: SR-12115 and SR-12116.

Modifications:

Avoid passing functions directly; pass them in new closures instead.

Result:

Fewer allocations.
2020-01-31 17:57:30 +00:00
Johannes Weissandtomer doron 7f8e96cf02 fix 5.2 builds (#1366)
motivation: tsan is broken on ubuntu 18.04.use 16.04 so 5.2 build can pass

changes:
* change 5.2 docker setup to use 16.04 instead of 18.04
* add the ability to pipe arguments to the integration tests via the docker setup
* skip tests_02 integration tests suite since it broken on 5.2 (swift-pm issue?)

fake the new limits from #1362

Co-authored-by: tomer doron <tomer@apple.com>
2020-01-29 14:39:28 +00:00