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

Conversation

braddunbar
Copy link
Contributor

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

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
Copy link
Contributor Author

Actually, this doesn't allow for extending the allocation in place via realloc as reserve does. I'm gonna think about this a little more…

@braddunbar braddunbar closed this Feb 2, 2024
@braddunbar braddunbar deleted the reserve-copy-only-unread branch March 24, 2024 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant