Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy only unread bytes in BytesMut::reserve #671

Closed

Commits on Feb 1, 2024

  1. Copy only unread bytes in BytesMut::reserve

    When reserving extra space in the Vec representation, there may not be
    enough space to efficiently reuse the buffer. Currently, the entire
    buffer is copied, including bytes that have already been read. Instead,
    this change copies only the unread bytes to the beginning of the new
    buffer. As a result, fewer bytes are copied and there is more unused
    capacity.
    
    It's important to note that the size of the new buffer will still be
    double the size of the current buffer, including bytes already read.
    That has not changed.
    
    Before this change:
    
    ```
                read       unused capacity
                 │           │
                ─┴─        ──┴──
    old buffer: ░░░████████░░░░░
    
    new buffer: ░░░████████░░░░░░░░░░░░░░░░░░░░░
                ─┬─        ──┬──────────────────
                 │           │
                read       unused capacity
    ```
    
    After this change:
    
    ```
                read       unused capacity
                 │           │
                ─┴─        ──┴──
    old buffer: ░░░████████░░░░░
    
    new buffer: ████████░░░░░░░░░░░░░░░░░░░░░░░░
                        ──┬─────────────────────
                          │
                        unused capacity
    ```
    braddunbar committed Feb 1, 2024
    Configuration menu
    Copy the full SHA
    300eb04 View commit details
    Browse the repository at this point in the history