Skip to content

Commit

Permalink
Refactor Bytes to use an internal vtable
Browse files Browse the repository at this point in the history
Bytes is a useful tool for managing multiple slices into the same region
of memory, and the other things it used to have been removed to reduce
complexity. The exact strategy for managing the multiple references is
no longer hard-coded, but instead backing by a customizable vtable.

- Removed ability to mutate the underlying memory from the `Bytes` type.
- Removed the "inline" (SBO) mechanism in `Bytes`. The reduces a large
  amount of complexity, and improves performance when accessing the
  slice of bytes, since a branch is no longer needed to check if the
  data is inline.
- Removed `Bytes` knowledge of `BytesMut` (`BytesMut` may grow that
  knowledge back at a future point.)
  • Loading branch information
seanmonstar committed Oct 11, 2019
1 parent ebe9602 commit c8b5531
Show file tree
Hide file tree
Showing 15 changed files with 2,091 additions and 2,562 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -28,4 +28,5 @@ serde = { version = "1.0", optional = true }
either = { version = "1.5", default-features = false, optional = true }

[dev-dependencies]
loom = "0.2.8"
serde_test = "1.0"
2 changes: 1 addition & 1 deletion ci/azure-cross-compile.yml
@@ -1,5 +1,5 @@
parameters:
cmd: test
cmd: build
rust_version: stable

jobs:
Expand Down
4 changes: 2 additions & 2 deletions ci/azure-tsan.yml
Expand Up @@ -18,9 +18,9 @@ jobs:
# Run address sanitizer
RUSTFLAGS="-Z sanitizer=address" \
cargo test --tests --target x86_64-unknown-linux-gnu
cargo test --target x86_64-unknown-linux-gnu --test test_bytes --test test_buf --test test_buf_mut
# Run thread sanitizer
RUSTFLAGS="-Z sanitizer=thread" \
cargo test --tests --target x86_64-unknown-linux-gnu
cargo test --target x86_64-unknown-linux-gnu --test test_bytes --test test_buf --test test_buf_mut
displayName: TSAN / MSAN
4 changes: 0 additions & 4 deletions ci/tsan
Expand Up @@ -19,10 +19,6 @@ race:test::run_tests_console::*closure
# Probably more fences in std.
race:__call_tls_dtors

# `is_inline_or_static` is explicitly called concurrently without synchronization.
# The safety explanation can be found in a comment.
race:Inner::is_inline_or_static

# This ignores a false positive caused by `thread::park()`/`thread::unpark()`.
# See: https://github.com/rust-lang/rust/pull/54806#issuecomment-436193353
race:pthread_cond_destroy
2 changes: 2 additions & 0 deletions src/buf/chain.rs
Expand Up @@ -187,13 +187,15 @@ impl<T, U> Buf for Chain<T, U>
n
}

/*
fn to_bytes(&mut self) -> crate::Bytes {
let mut bytes: crate::BytesMut = self.a.to_bytes().try_mut()
.unwrap_or_else(|bytes| bytes.into());
bytes.put(&mut self.b);
bytes.freeze()
}
*/
}

impl<T, U> BufMut for Chain<T, U>
Expand Down
19 changes: 0 additions & 19 deletions src/buf/vec_deque.rs
Expand Up @@ -20,22 +20,3 @@ impl Buf for VecDeque<u8> {
self.drain(..cnt);
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn hello_world() {
let mut buffer: VecDeque<u8> = VecDeque::new();
buffer.extend(b"hello world");
assert_eq!(11, buffer.remaining());
assert_eq!(b"hello world", buffer.bytes());
buffer.advance(6);
assert_eq!(b"world", buffer.bytes());
buffer.extend(b" piece");
let mut out = [0; 11];
buffer.copy_to_slice(&mut out);
assert_eq!(b"world piece", &out[..]);
}
}

0 comments on commit c8b5531

Please sign in to comment.