Skip to content
Snippets Groups Projects
  • Noah Zentzis's avatar
    dfce95b8
    Recycle space when reserving from Vec-backed Bytes (#197) · dfce95b8
    Noah Zentzis authored
    * Recycle space when reserving from Vec-backed Bytes
    
    BytesMut::reserve, when called on a BytesMut instance which is backed by
    a non-shared Vec<u8>, would previously just delegate to Vec::reserve
    regardless of the current location in the buffer. If the Bytes is
    actually the trailing component of a larger Vec, then the unused space
    won't be recycled. In applications which continually move the pointer
    forward to consume data as it comes in, this can cause the underlying
    buffer to get extremely large.
    
    This commit checks whether there's extra space at the start of the
    backing Vec in this case, and reuses the unused space if possible
    instead of allocating.
    
    * Avoid excessive copying when reusing Vec space
    
    Only reuse space in a Vec-backed Bytes when doing so would gain back
    more than half of the current capacity. This avoids excessive copy
    operations when a large buffer is almost (but not completely) full.
    dfce95b8
    History
    Recycle space when reserving from Vec-backed Bytes (#197)
    Noah Zentzis authored
    * Recycle space when reserving from Vec-backed Bytes
    
    BytesMut::reserve, when called on a BytesMut instance which is backed by
    a non-shared Vec<u8>, would previously just delegate to Vec::reserve
    regardless of the current location in the buffer. If the Bytes is
    actually the trailing component of a larger Vec, then the unused space
    won't be recycled. In applications which continually move the pointer
    forward to consume data as it comes in, this can cause the underlying
    buffer to get extremely large.
    
    This commit checks whether there's extra space at the start of the
    backing Vec in this case, and reuses the unused space if possible
    instead of allocating.
    
    * Avoid excessive copying when reusing Vec space
    
    Only reuse space in a Vec-backed Bytes when doing so would gain back
    more than half of the current capacity. This avoids excessive copy
    operations when a large buffer is almost (but not completely) full.