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

Remove bytes dependency. #77

Merged
merged 2 commits into from
Mar 19, 2020
Merged

Remove bytes dependency. #77

merged 2 commits into from
Mar 19, 2020

Conversation

twittner
Copy link
Contributor

The motivation for this change is the worry, that streams which do not consume their buffers in a timely manner prevent BytesMut from reusing their storage space, instead forcing it to allocate more memory.

This PR removes bytes completely and instead uses Vec<u8>s. The reclamation of storage is left to the allocator. As a side effect, all instances of unsafe are removed, so if merged, #66 can be considered done.

While at it we also remove the BufWriter from Io. Client code often already uses buffers and always adding another buffer layer seems misguided. If desired clients may always provide Connection::new with a BufWriter type.

The motivation for this change is the worry, that streams which do not
consume their buffers in a timely manner prevent `BytesMut` from reusing
their storage space, instead forcing it to allocate more memory.

This PR removes `bytes` completely and instead uses `Vec<u8>`s. The
reclamation of storage is left to the allocator.

While at it we also remove the `BufWriter` from `Io`. Often client code
already uses buffers and always adding another buffer layer seems
misguided. After all clients may always provide `Connection::new` with
a `BufWriter` type.
@twittner twittner added the ready for review This PR is ready for review. label Mar 17, 2020
@romanb romanb self-requested a review March 18, 2020 11:31
pub(crate) fn len(&self) -> Option<usize> {
self.seq.iter().fold(Some(0), |total, x| {
total.and_then(|n| n.checked_add(x.len()))
})
}

pub(crate) fn push(&mut self, x: Bytes) {
/// Add another chunk of bytes to the end.
pub(crate) fn push(&mut self, x: Vec<u8>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this entire module is private to the crate, is there a need for pub(crate) in this module at all, as opposed to just pub? Or is it a stylistic choice to always use pub(crate) in private modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, while those items could be made public, given that the module is private, I like to think it makes it easier to read the code when one can see directly that an item is not part of the public API without having to look outside the current reading context how the whole module is used.

@twittner twittner merged commit 3dc4ca3 into libp2p:develop Mar 19, 2020
@twittner twittner deleted the rm-bytes branch March 19, 2020 10:02
@romanb romanb mentioned this pull request Mar 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review This PR is ready for review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants