File tree 2 files changed +20
-2
lines changed
2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change 91
91
92
92
#![ doc( html_logo_url = "https://raw.githubusercontent.com/fjall-rs/value-log/main/logo.png" ) ]
93
93
#![ doc( html_favicon_url = "https://raw.githubusercontent.com/fjall-rs/value-log/main/logo.png" ) ]
94
- #![ forbid( unsafe_code) ]
95
94
#![ deny( clippy:: all, missing_docs, clippy:: cargo) ]
96
95
#![ deny( clippy:: unwrap_used) ]
97
96
#![ deny( clippy:: indexing_slicing) ]
98
97
#![ warn( clippy:: pedantic, clippy:: nursery) ]
99
98
#![ warn( clippy:: expect_used) ]
100
99
#![ allow( clippy:: missing_const_for_fn) ]
101
100
#![ warn( clippy:: multiple_crate_versions) ]
101
+ // the bytes feature uses unsafe to improve from_reader performance
102
+ #![ cfg_attr( not( feature = "bytes" ) , forbid( unsafe_code) ) ]
102
103
103
104
mod blob_cache;
104
105
mod coding;
Original file line number Diff line number Diff line change @@ -37,10 +37,27 @@ impl Slice {
37
37
}
38
38
39
39
/// Constructs a [`Slice`] from an I/O reader by pulling in `len` bytes.
40
+ ///
41
+ /// The reader may not read the existing buffer.
40
42
#[ doc( hidden) ]
41
43
pub fn from_reader < R : std:: io:: Read > ( reader : & mut R , len : usize ) -> std:: io:: Result < Self > {
42
- let mut builder = BytesMut :: zeroed ( len) ;
44
+ // Use `with_capacity` & `set_len`` to avoid zeroing the buffer
45
+ let mut builder = BytesMut :: with_capacity ( len) ;
46
+
47
+ // SAFETY: we just allocated `len` bytes, and `read_exact` will fail if
48
+ // it doesn't fill the buffer, subsequently dropping the uninitialized
49
+ // BytesMut object
50
+ unsafe {
51
+ builder. set_len ( len) ;
52
+ }
53
+
54
+ /// SAFETY: Normally, read_exact over an uninitialized buffer is UB,
55
+ /// however we know that in lsm-tree etc. only I/O readers or cursors over Vecs are used
56
+ /// so it's safe
57
+ ///
58
+ /// The safe API is unstable: https://github.com/rust-lang/rust/issues/78485
43
59
reader. read_exact ( & mut builder) ?;
60
+
44
61
Ok ( Self ( builder. freeze ( ) ) )
45
62
}
46
63
}
You can’t perform that action at this time.
0 commit comments