Commit 27b730e
authored
perf: reuse HPACK encode buffer across frames (#929)
`into_encoding` allocated a fresh `BytesMut` for every HEADERS and
PUSH_PROMISE frame, grew it through several capacity doublings while HPACK
encoded into it, then called `.freeze()` — allocating again for the `Bytes`
refcount header. That is at least two allocations per outbound header block,
on every request and every response.
The block does not need to outlive the call: on the common path it is copied
into the connection write buffer and dropped immediately. So `hpack::Encoder`
now keeps one scratch buffer that `into_encoding` takes and `encode` hands
back once the block has been written.
`EncodingHeaderBlock::hpack` becomes `BytesMut`, which removes the
`.freeze()` as well. The CONTINUATION path keeps its remainder instead of
returning the buffer, and splitting that remainder is now `split_to` on a
uniquely-owned buffer rather than a copy.
Same bytes on the wire; no public API change.
`cargo bench --bench main`, 5 interleaved rounds per arm: multi-thread 100k
requests improves 3.8 % (faster in 24 of 25 pairwise comparisons, exact
permutation test p = 0.016), current-thread 1.5 %, and the write-contention
benchmark is unchanged as expected.1 parent c2d3fb6 commit 27b730e
2 files changed
Lines changed: 45 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| |||
287 | 287 | | |
288 | 288 | | |
289 | 289 | | |
290 | | - | |
| 290 | + | |
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
| |||
508 | 508 | | |
509 | 509 | | |
510 | 510 | | |
511 | | - | |
| 511 | + | |
512 | 512 | | |
513 | 513 | | |
514 | 514 | | |
| |||
551 | 551 | | |
552 | 552 | | |
553 | 553 | | |
554 | | - | |
| 554 | + | |
555 | 555 | | |
556 | 556 | | |
557 | 557 | | |
| |||
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
650 | | - | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
651 | 657 | | |
652 | 658 | | |
653 | 659 | | |
| |||
664 | 670 | | |
665 | 671 | | |
666 | 672 | | |
667 | | - | |
| 673 | + | |
| 674 | + | |
668 | 675 | | |
669 | 676 | | |
670 | 677 | | |
671 | 678 | | |
672 | 679 | | |
673 | 680 | | |
674 | 681 | | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
675 | 687 | | |
676 | 688 | | |
677 | 689 | | |
| |||
978 | 990 | | |
979 | 991 | | |
980 | 992 | | |
981 | | - | |
| 993 | + | |
| 994 | + | |
982 | 995 | | |
983 | 996 | | |
984 | 997 | | |
985 | 998 | | |
986 | 999 | | |
987 | 1000 | | |
988 | 1001 | | |
989 | | - | |
990 | | - | |
991 | | - | |
| 1002 | + | |
992 | 1003 | | |
993 | 1004 | | |
994 | 1005 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
11 | 18 | | |
12 | 19 | | |
13 | 20 | | |
| |||
21 | 28 | | |
22 | 29 | | |
23 | 30 | | |
| 31 | + | |
24 | 32 | | |
25 | 33 | | |
26 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
27 | 50 | | |
28 | 51 | | |
29 | 52 | | |
| |||
0 commit comments