feat: use reader.copy_into_slice when deserializing char#187
feat: use reader.copy_into_slice when deserializing char#187kskalski wants to merge 1 commit intoanza-xyz:masterfrom
Conversation
| 0xF0..=0xF4 => 4, | ||
| _ => return Err(invalid_char_lead(b0)), | ||
| }; | ||
| let mut buf = [0u8; 4]; |
There was a problem hiding this comment.
I think we could use [MaybeUninit<u8>; 4] and assume_init_ref with rust >=1.93 (https://doc.rust-lang.org/stable/core/primitive.slice.html#method.assume_init_ref), but I doubt for 4-byte array it would make any difference
|
For small reads like this it's totally fine to use wincode/wincode/src/io/std_io.rs Lines 103 to 106 in 85034c2 The proposed implementation will short-circuit if the buffer already contains enough bytes to fulfill the request.
Our Consider a case where one is deserializing a struct containing all integers and chars. If those were to all use The proposed no-grow implementation will still actually call wincode/wincode/src/io/std_io.rs Line 213 in 85034c2 and this was done specifically to avoid the above case mentioned above. But, this is an implementation detail of the proposal, not something necessarily guaranteed by the API. By using the |
|
The practical problem is that even when operating on buffers, satisfying
I would argue that It's fine to have Arguably the de-fragmentation cost mentioned above is not that bad if the expectation for returned slice can be bounded to |
The issue with a memcpy for small values is that it can prohibit scalarization because it is an opaque "copy some bytes" intrinsic. This is similar in motivation to #64. It gives the compiler more opportunity and more visibility to I do agree with one of your points in #188, that because we employ this pattern so often: let bytes: [u8; N] = *reader.fill_array();
unsafe { reader.consume_unchecked(N) };There is likely room for an additional helper like your proposed I would certainly prefer this over using |
Avoid calling
fill_buf_exactduringchardeserialization such thatReaderimplementation isn't required to always provide borrowed slice of requested (2-4 byte) size.Use a stack defined 4-byte array instead and fill it with requires (up to 4) bytes using
copy_into_slice.This removes the only call to
fill_buf_exactin production code outside of default / forwarding implementations ofReader.