Skip to content

Commit 886cd45

Browse files
committed
Add fast-and-unsound feature
In some cases, speed could be preferable over soundness if we control the implementation of `Read` that is used for the underlying stream. By default the feature is _NOT_ activated, keeping the current behavior as-is. However, when the feature is activated, it gives better performance when the user ensures correctness by verifying the behavior of the underlying `Read` implementation.
1 parent 56d758b commit 886cd45

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ native-tls-vendored = ["native-tls", "native-tls-crate/vendored"]
2626
rustls-tls-native-roots = ["__rustls-tls", "rustls-native-certs"]
2727
rustls-tls-webpki-roots = ["__rustls-tls", "webpki-roots"]
2828
__rustls-tls = ["rustls", "rustls-pki-types"]
29+
fast-and-unsound = []
2930

3031
[dependencies]
3132
data-encoding = { version = "2", optional = true }

src/protocol/frame/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,13 @@ impl FrameCodec {
225225
fn read_in(&mut self, stream: &mut impl Read) -> io::Result<usize> {
226226
let len = self.in_buffer.len();
227227
debug_assert!(self.in_buffer.capacity() > len);
228+
#[cfg(not(feature = "fast-and-unsound"))]
228229
self.in_buffer.resize(self.in_buffer.capacity(), 0);
230+
#[cfg(feature = "fast-and-unsound")]
231+
// SAFETY: this is known to be unsound,
232+
// a strange implementation of `Read` could read uninitialized memory.
233+
// However, if speed is preferable over soundness `fast-and-unsound` can be activated.
234+
unsafe { self.in_buffer.set_len(self.in_buffer.capacity()); }
229235
let size = stream.read(&mut self.in_buffer[len..]);
230236
self.in_buffer.truncate(len + size.as_ref().copied().unwrap_or(0));
231237
size

0 commit comments

Comments
 (0)