perf(h1): pre-allocate read buffer instead of starting at zero#4030
perf(h1): pre-allocate read buffer instead of starting at zero#4030DioCrafts wants to merge 1 commit intohyperium:masterfrom
Conversation
Change read_buf initialization from BytesMut::with_capacity(0) to BytesMut::with_capacity(INIT_BUFFER_SIZE) (8192 bytes). The read buffer almost always grows to INIT_BUFFER_SIZE on the first read. Pre-allocating avoids the initial zero-capacity allocation and the subsequent reallocation + copy when the first data arrives.
Capacity of zero is not an allocation. |
|
@paolobarbolini You're absolutely right man, with_capacity(0) doesn't allocate, my description was misleading there, sorry about that! The intent is to avoid the deferred allocation path on the first read, since the adaptive strategy will grow it to INIT_BUFFER_SIZE immediately anyway. I'll update the PR description. Thanks for the correction! 🙏 |
|
Does this result in improved performance in some case? Can we measure? |
Hi @seanmonstar. Honestly, for the common case the difference is negligible: with_capacity(0) doesn't allocate, and the first read triggers the 8 KiB allocation anyway. The only gain is skipping the growth check on that first read. I don't have benchmarks showing a measurable improvement. If you'd prefer, I can run h1_parallel or a similar bench to get numbers before moving forward. |
Description:
The H1 read buffer was created with
BytesMut::with_capacity(0). On the first read, the adaptive strategy immediately grows it toINIT_BUFFER_SIZE(8192 bytes). The H1 read buffer was created with BytesMut::with_capacity(0). On the first read, the adaptive strategy grows it to INIT_BUFFER_SIZE (8 KiB). By pre-allocating at that size directly, we skip the deferred-growth code path.This PR initializes the buffer directly at
INIT_BUFFER_SIZE. One allocation instead of two. The adaptive read strategy still controls growth from there.What changed
io.rsline 71:BytesMut::with_capacity(0)→BytesMut::with_capacity(INIT_BUFFER_SIZE)One line, no API changes.