Skip to content

Recovery lazy storage staging - #375

Open
rnro wants to merge 4 commits into
apple:mainfrom
rnro:recovery-lazy-storage-staging
Open

rnro wants to merge 4 commits into
apple:mainfrom
rnro:recovery-lazy-storage-staging

Conversation

@rnro

@rnro rnro commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Allocate Recovery storage lazily and clear AckBitstring storage in place

QUICConnection.init builds a placeholder Recovery that setup immediately replaces, and each Recovery allocated an AckBitstring of 64 words (512 bytes) and room for 10 outstanding packets for every packet number space, whether or not the space ever carried a packet. Recovery.InnerState.reset also replaced both ACK bitstrings with new ones, allocating two 512 byte arrays and freeing the old ones on every reset. Recovery.init runs twice per connection on each side, and a connection resets a packet number space when it discards that space's keys during the handshake and again when it closes.

This is two commits that can be reviewed separately.

Changed:

  • Added AckBitstring.reset(), which zero-fills the existing words, and used it in Recovery.InnerState.reset instead of constructing new bitstrings.
  • Started AckBitstring with no words; nset and xor grow it on demand, and resize(to:) reserves the full capacity so growth is a single allocation.
  • Started outstandingPackets as an empty deque that insertSentPacket sizes for 10 entries on the first insert.
  • Updated testBitstringGrowth to expect an empty bitstring to hold no words.

Result:

  • Constructing a Recovery allocates nothing.
  • Fewer allocs

rnro added 3 commits November 26, 2026 10:51
…space

`Recovery.InnerState.reset` replaced both ACK bitstrings with new `AckBitstring()` values, allocating two 512 byte arrays and freeing the old ones on every reset. A connection resets a packet number space when it discards that space's keys during the handshake, and closing a connection resets every space.

Changed:
* Added `AckBitstring.reset()`, which zero-fills the existing words and returns to the initial word.
* Used `AckBitstring.reset()` in `Recovery.InnerState.reset` instead of constructing new bitstrings.

Result:
* A `QUICHandshake` iteration, which runs a client and a server, makes 20 fewer allocations (1929 to 1909).
* A `QUICConnectionSetup` iteration makes 18 fewer allocations (657 to 639).
* The stream echo and stream churn benchmarks make the same number of allocations as before.
`QUICConnection.init` builds a placeholder `Recovery` that `setup` immediately replaces. Each `Recovery` allocated an `AckBitstring` of 64 words (512 bytes) and room for 10 outstanding packets for every packet number space, whether or not the space ever carried a packet. `Recovery.init` therefore runs twice per connection on each side, and together with the in-place reset it accounts for about 23 allocations of a handshake per side.

Changed:
* Started `AckBitstring` with no words; `nset` and `xor` grow it on demand.
* Reserved the full capacity in `AckBitstring.resize(to:)` so growth is a single allocation.
* Started `outstandingPackets` as an empty deque that `insertSentPacket` sizes for 10 entries on the first insert.
* Updated `testBitstringGrowth` to expect an empty bitstring to hold no words.

Result:
* Constructing a `Recovery` allocates nothing.
* A `QUICHandshake` iteration, which runs a client and a server, makes 26 fewer allocations (1909 to 1883).
* A `QUICConnectionSetup` iteration makes 35 fewer allocations (639 to 604).
* The stream echo and stream churn benchmarks make the same number of allocations as before.
@rnro rnro added the 🔨 semver/patch No public API change. label Nov 26, 2026
@rnro
rnro marked this pull request as ready for review November 26, 2026 16:27
if outstandingPackets.capacity == 0 {
// Geometric growth from an empty deque would reallocate several times before
// reaching a typical flight size; jump straight to it.
outstandingPackets.reserveCapacity(Self.initialOutstandingPacketsCapacity)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably reserve the capacity of 5 for the initial and handshake and then do 10 for the application space.

@@ -1,5 +1,5 @@
{
"mallocCountTotal" : 80988,
"mallocCountTotal" : 80996,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these numbers just variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants