mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-25 04:47:50 +00:00
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>