-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Inline read/write functions for Cursors #33916
Comments
The generic ones should in theory already be inlined due to the generics themselves, but feel free to send a PR to inline some of the others! |
Implementing the Write trait for Cursors over slices is so light-weight that under some circumstances multiple writes can be fused into a single instruction. In general I think inlining these functions is a good idea because most of the code can be constant-folded and copy-propagated away. Closes issue rust-lang#33916.
I didn't understand what you meant by "The generic ones should in theory already be inlined", but I guessed you meant the use std::io::{Cursor,Read};
fn main() {
let arr : [u8; 4] = [0xff, 0xee, 0xdd, 0xcc];
let mut c = Cursor::new(&arr as &[u8]);
let mut arr2 = [0u8; 4];
let _ = c.read(&mut arr2[0..2]);
let _ = c.read(&mut arr2[2..4]);
assert!(arr == arr2);
} The body of push %rax
movl $0xccddeeff,0x4(%rsp)
movl $0x0,(%rsp)
movl $0xccddeeff,(%rsp)
cmpl $0xccddeeff,0x4(%rsp)
jne 5243 <_ZN4read4main17h1a4d109b8431532bE+0x23>
pop %rax
retq (where 5243 is the address of the unwind call). I'm a little surprised LLVM didn't constant-fold the comparison and reduce the whole thing to a single So anyway I've submitted a pull request that only inlines |
Inline simple Cursor write calls Implementing the Write trait for Cursors over slices is so light-weight that under some circumstances multiple writes can be fused into a single instruction. In general I think inlining these functions is a good idea because most of the code can be constant-folded and copy-propagated away. Closes issue rust-lang#33916. r? @alexcrichton
The PR was merged, but bors didn't close this issue. |
It would be nice if the
Read
andWrite
implementations forstd::io::Cursor
were inlined. Consider this program:If the
write
calls are inlined, then all the position computations are constant-folded and the writes compile to exactly what I'd hope for:I tested using
rustc -C opt-level=2 -C lto
, using rustc 1.10.0-nightly (e0fd34b 2016-05-09). I think that LTO is a pretty close approximation here to the effect that#[inline]
would have, but I guess that assumption needs testing. (By the way: oddly, at opt-level=3 I got worse code.)Without LTO, I get about 84 instructions for this sequence, counting the ones in
main
as well as the implementation ofwrite
itself.That said, the
Write
implementation forCursor<Vec<u8>>
is more complicated and maybe shouldn't be inlined. But I think this request at least applies to the implementations forCursor<&'a mut [u8]>
andCursor<Box<[u8]>>
, and probably to theRead
implementation forCursor<T> where T: AsRef<[u8]>
.Does this sound sensible?
The text was updated successfully, but these errors were encountered: