Skip to content

Inline read/write functions for Cursors #33916

@jameysharp

Description

@jameysharp

It would be nice if the Read and Write implementations for std::io::Cursor were inlined. Consider this program:

use std::io::{Cursor,Write};

fn main() {
        let mut arr = [0u8; 4];
        let mut c = Cursor::new(&mut arr as &mut [u8]);
        let _ = c.write(&[42, 7]);
        let _ = c.write(&[127, 255]);
        println!("{:?}", c.into_inner());
}

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:

movl   $0xff7f072a,0x5c(%rsp)

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 of write itself.

That said, the Write implementation for Cursor<Vec<u8>> is more complicated and maybe shouldn't be inlined. But I think this request at least applies to the implementations for Cursor<&'a mut [u8]> and Cursor<Box<[u8]>>, and probably to the Read implementation for Cursor<T> where T: AsRef<[u8]>.

Does this sound sensible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions