Motivation:
Recent nightly builds have introduced small allocation regressions.
Modifications:
Update allocation limits for nightly Swift builds.
Result:
CI passes.
Motivation:
The canonicalForm subscript was written for clarity, but it didn't do a
good job of avoiding performance costs. Unfortunately, this function can
be a bit costly, for two reasons. Firstly, it used a bunch of eager
collection operations, such that it would allocate intermediate arrays.
Secondly, it computed on the String, rather than the UTF8View backing
that string, in order to perform those collection operations. The result
was that it spent a bit too much time messing about with the headers
than it needed to.
Modifications:
- Define a lazy UTF8 view splitting sequence and use it.
- Change to compute on the UTF8View.
Result:
Better performance in the canonicalForm subscript.
Motivation:
To justify performance changes we need to measure the code being
changed. We believe that `HPACKHeaders.subscript(canonicalForm:)` is a
little slow.
Modifications:
- Add allocation and performance tests for fetching header values in
their canonical form
Results:
More benchmarks.
From: https://github.com/apple/swift-nio-extras/pull/141
Motivation:
The combination of Ruby and Jazzy are throwing up issues in CI on older
Ubuntu versions.
Modifications:
- Only install Jazzy on focal
- Fix doc generation script to use more recent source-kitten syntax
Results:
- CI doesn't complain
- Docs get built
Motivation:
Our 5.5 allocation counts were off. Rather than just update that by hand
we should bring over some tooling to do it automatically.
Modifications:
- Bring over and adapt the update-alloc-limits scripts from NIO.
- Run them and commit the updates.
Result:
CI should pass again.
Motivation:
Some of the allocation counting tests used frame-based stream channels
rather than the frame payload based ones. These were deprecated some
time ago.
Modifications:
- Update test handlers to be `HTTP2Frame.FramePayload` based
- Update the 'create_client_stream_channel' test to count streams rather
than summing their stream ID (as stream IDs aren't assigned until the
stream channel delivers pending writes for the first time).
- Update allocation limits
Result:
Allocation counting tests don't use deprecated APIs.
Motivation:
SwiftNIO 2.27.0 added synchronous channel options. We can use these if
available when creating a stream channel to avoid a future allocation.
Modifications:
- 'getAutoReadFromParent' is synchronous when possible and now takes a
closure to handle the result.
- Update allocation counts (and sort them to make updating them easier,
this also includes switching the order we run test_1k_requests
interleaved and noninterleaved)
Result:
Fewer allocations.
Motivation:
NIO 2.26.0 saved us some allocations, some of our limits are now too
high and CI fails as a result.
Modifications:
- Adjust the allocation counter limits.
- Replace a 'class' constraint with 'AnyObject'
Result:
CI passes.
Motivation:
The stream channel configure function uses a chain of futures where the
same can be achieved in fewer futures.
Modifications:
- Merge the future chains where possible in HTTP2StreamChannel.configure
Result:
A 1.3% reduction in instructions in server_only_10k_requests_1_concurrent
The run-all mode of the performance tester inadvertently got removed in
a previous patch. This adds it back.
Along the way we have apparently unlocked some perf gains in a new NIO
release.
Motivation:
The OutboundFlowControlBuffer is responsible for managing DATA and
HEADERS frames for all streams in order to ensure that we obey HTTP/2
flow control rules, even when users don't. This object is therefore on
the data path for the vast majority of frames, and is extremely
performance sensitive.
Unfortunately, the current algorithm has some nasty performance cliffs.
Right now it has the awkward property of making flushes take an amount
of time linear in the number of streams on the connection, not the
number of streams that had had frames sent on them. The result is that
connections with highly parallel numbers of streams see extremely poor
performance when flushing, a problem that is exacerbated if other parts
of the HTTP/2 stack fail to coalesce those flushes.
We can clean this up in a few ways, but the easiest way is to just keep
track of which streams have done I/O since the last flush. While we're
there, we can make a few other optimisations to ensure we only flush
data on streams we are actually processing, to further reduce the risk
of wasting our time processing unnecessary work.
Modifications:
- Add a Set to keep track of where we've done writes since the last
flush.
- Streams whose flow control window goes to zero stop being writable.
- Cheaper and more accurate calculation of zero-length writes, which
avoids the possibility of awkward stalls with zero-length DATA frames
and END_STREAM.
- Stop doing math about the number of flushed bytes, we don't use that
information any more.
Results:
An approximately 10% performance gain on the 100 concurrent streams
server-only benchmark. The relative performance gain shifts with the
number of active streams, but it should be positive in all cases.
Motivation:
A while ago we shimmed out the String unsafeUninitializedCapacity
initializer because while it was on the road to being implemented, it
hadn't actually been implemented. Now it has!
On Apple platforms, the initializer is guarded behind an availability
guard. We need to add that guard here. This means we need to keep hold
of the backport for a good long time, until we raise our minimum Apple
platform support to a new enough version. Happily, Linux on 5.3 should
just see a net win here.
Modifications:
- Updated to enable us to use the proper initializer.
Result:
Fewer allocations when screwing around with HPACK.
Motivation:
The logic for doing bulk stream teardown in the ConnectionStreamsState
was always a bit janky: we'd grab all the stream IDs out of the map,
filter out anything that was too small, insert them one-by-one into a
circular buffer, and then report back. I basically just translated that
directly into StreamMap, but on StreamMap this potentially performs very
poorly, as attempting to find the pivot point becomes a linear scan
through the stream data even when there are _loads_ of streams.
This patch swaps over to add a specialized function for bulk-removal of
streams. The specialized function passes the slice of data to be removed
to the caller so that they can compute on it. It does this by a closure
to try to avoid a CoW where possible.
While we're here I tried to address the loop over the CircularBuffer of
recently reset streams. This flushed out an awkward bug that has existed
in our recently reset streams logic for a very long time, which is that
while we _tried_ to maintain the size of the circular buffer, in
practice we misunderstood the CircularBuffer invariants and so this
never worked. I've added a bunch of unit tests for that logic and fixed
it up.
These two changes together will improve performance in stream bulk
teardown. This is an uncommon case (it only happens on GOAWAY), but it's
still good to make that faster.
Modifications:
- New operation on StreamMap
- New prependWithoutExpanding(contentsOf:) operation
- Unit tests for the circular buffer and stream map logic.
Result:
Better performance on bulk stream teardown.
Motivation:
We don't have any benchmarks or alloc counter tests for doing stream
teardown with GOAWAY frames. While this isn't a terribly hot code path
and isn't terribly performance heavy with allocations, it's good to have
more data here.
Modifications:
- Write benchmarks and alloc counter tests that tear down many streams
with GOAWAY.
Result:
Better testing and alloc counter.
Motivation:
When working with HTTP/2 we frequently have a need to store "per-stream"
state. This is state that is independently reproduced once per stream.
We often need to look up this state by stream ID, and so we are
motivated to store this state as efficiently as possible.
Naively, Dictionary seems like a useful data type for this. However,
Dictionaries are not free. The cost of hashing in order to index into a
Dictionary can be substantial. This is both because the hashing function
itself is branchy and complex, but also because the dictionary access is
very unfriendly to the branch predictor and the memory caching systems.
We can take advantage of the fact that if you split streams into client
and server namespaces, stream IDs are strictly ordered. That is, streams
are created in order of increasing stream ID. While streams are
_retired_ out of order, at no point will a CircularBuffer of stream IDs
ever be unordered.
This unlocks a very powerful data structure for us: the ordered "Array"
(in our case actually CircularBuffer). An ordered Array supports two
powerful lookup tools. Firstly, it supports the mighty binary search.
This means the absolute worst-case searching performance for an ordered
Array is log(n) in the size of the Array. Finding a stream by stream ID
in an Array of one million streams would take around 20 steps: extremely
cheap.
For smaller sizes, however, we unlock the one true searching function:
linear scan. On modern CPUs, performing simple computations (such as
checking if one number equals another) is orders of magnitude faster
than looking data up in memory. More importantly, modern CPUs can often
effectively compute multiple lookups at once thanks to their
out-of-order superscalar pipelines. As a result, on any modern CPU
(including all mobile CPUs) a linear search outperforms almost any other
searching algorithm for surprisingly large Array sizes!
Given that stream IDs are intrinsically sorted, and thus all
per-stream-data is intrinsically sorted by stream ID, we have the
ability to use either of the above strategies. More importantly for us,
we have the ability to be _adaptive_ to those strategies, and swap
between them as needed.
Thus we deploy the StreamMap. This data structure stores per-stream data
in a pair of circular buffers, keyed by stream ID. For smallish numbers
of streams we just search these buffers linearly. For larger numbers, we
flip over to binary search.
Why circular buffers? Because we avoid a compaction problem. While in
general removal of stream IDs is done in arbitrary order, in _general_
the mostly likely streams to end at any given time are the oldest and
the youngest. Circular buffers make removing streams at those ends very
cheap, as compaction can be done easily. This, along with avoiding new
allocations, makes circular buffers a perfect data structure for this
strategy.
Modifications:
- Implemented StreamMap
- Replaced Dictionary
Results:
Meaningful performance gains on all benchmarks. 1k requests with 1
concurrent stream sees about a .1% performance improvement. 1k requests
with 100 concurrent streams sees a more like 15% performance
improvement.
Co-authored-by: George Barnett <gbarnett@apple.com>
Motivation:
Heap allocations that aren't necessary for program correctness are to be
avoided.
Modifications:
- Avoid heap allocating for constructing indirect enum cases.
Result:
Fewer heap allocations.
Resolves#255.
Reduces instructions executed in cachegrind server-only benchmarks by
about 0.15%.
Reduces icache miss rate by 3% in cachegrind due to reduced jumps to
cold malloc code.
Motivation:
Performance benchmarking wants accurate numbers, and a wide range of
possible benchmarks. One way to get somewhat more accurate numbers is to
use a tool like Cachegrind to measure the instruction counts of a
program. Cachegrind allows us to isolate a number of noise-introducing
effects, as "number of executed instructions" is a much more stable
number than how long a program takes to run.
When running under cachegrind we want to change the way our performance
benchmarks work. Instead of running lots of times to try to average out
noise, we only want to run once to save ourselves time, as the benchmark
itself should be drastically more stable.
While we're here, let's add some much more isolated benchmarks than our
current "make lots of streams" benchmark. This is basically the same
logic, but it does less of NIO and so makes the impact of NIOHTTP2
substantially more prominent.
Modifications:
- Added a cachegrind build mode to the benchmarks.
- Added new benchmarks and alloc counters.
Result:
More info
Motivation:
I have suspicions that the allocation counter tests in NIOHTTP2 don't
fail properly, so let's bring the scaffolding up to date.
Modifications:
Update the few local files to the most recent NIO versions.
Result:
Smaller diffs to NIO, maybe better infrastructure?
Motivation:
The 2 to 1 codecs do a number of operations where they "peek" the
pseudo-headers. Each of these operations uses the HPACKHeaders
subscript, meaning that they allocate a temporary array for a field that
can, in a correct request/response, contain only one element. That's
very wasteful!
Modifications:
- Rewrite peekPseudoHeader to iterate instead, avoiding the array.
- Add allocaton counter test.
Result:
4 fewer allocations per HTTP request transformed on the server side,
2 fewer allocations per HTTP response transformed on the client side.
Motivation:
Thread Sanitizer will find bugs which might be missed otherwise.
Modifications:
When running docker image for 1604/5.1 tests and h2spec are both compiled with --sanitize=thread
Result:
Tests are run with thread sanitizer turned on.
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
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.
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
* swift 5.1 docker setup
motivation: support swift 5.1
changes:
* add docker compose file for swift 5.1, will add CI job once merged
* update swiftformat version to 0.40.12
* use more standard dockerfile setup
* 16.04
Motivation:
We regressed 4 allocations in the security update
https://github.com/apple/swift-nio-http2/security/advisories/GHSA-jchv-x857-q8fq
. Four allocations are a great tradeoff for a number of security
vulnerabilities fixed but we need to raise the limits accordingly.
Modifications:
Raise the limits to the new limit so tests can pass again.
Result:
PRs can pass again and master should build again.
Motivation:
It's important to ensure that child channel users can observe the
writability state of a child channel in order to avoid ballooning data
in memory. Up until now we haven't supported writability notifications,
but we really should.
Modifications:
- Added a flow controller to manage writability.
- Managed data flow through promises.
- Lots of tests.
Result:
Child channels will provide a much better signal as to whether they are
writable.
motivation: prepare to run performance tests in ci
changes:
add performance-test docker-compose task
add script that can parse the performance test suite results and compare between two result-sets
add generic shell docker-compose task
motivation: now that swift has official docker images, use them instead of the homebrewed one
changes: refactor docker file to use swift:version instead of ubuntu:version
motivation: test task to include h2spec
changes:
* remove older docker-compose files that are not in use on the master branch
* move the task definition from the 18.04/5.0 docker compose file to the generic one
* create a unit-test task to encapsulate the unit tests and change the test task to include unit tests, integration tests and h2 spec verification
Motivation:
Previously the HTTP2StreamChannel forwarded all frames to the parent
Channel by way of the abstract Channel existential API. While this was
straightforward, it added unnecessary overhead due to the extra pipeline
steps and the extra indirection of the types providing an inlining
barrier.
Given that in #116 we gave the HTTP2StreamChannel a reference to its
parent multiplexer, we may as well take advantage of this and allow
direct calls from the stream channel into the multiplexer.
Modifications:
Avoided calls to parent.write and parent.flush.
Replaced them with a new interface.
Added new allocation test for this path.
Result:
Slightly better performance.
Motivation:
Allocation tests are good :).
Modifications:
Add two allocations tests and NIO's integration test framework.
Result:
Hopefully fewer allocations in the future. And better ways to check what
allocations are happening.
motivation: publish API docs
changes:
* amend docker file to include jazzy
* add doc generation script based on what we have in swift-nio and swift-log