Rust io unbuffered: O_DIRECT

Hi,

I read that Rust IO is unbuffered by default.

When choosing to write directly to disk under Linux, do we still need to specify the O_DIRECT flag when opening the file?

Thank you

1 Like

It's not unbuffered on that level. It just means that Rust's std does not add any additional buffering, but your operating system caching is still in effect.

If you use std::fs::File to open a file for reading or writing, and you keep jumping around in the file to read/write at different locations somewhat close to each other, all those read/writes will cause read/write syscalls to the OS. However, these reads/writes will still be subject to regular caching in the operating system.

If you put the File in a BufReader/BufWriter, jumping around in the file to read and write will allow the Rust code to use a memory buffer rather than issue the read/write syscalls (until a flush).

I.e. you don't need to worry about O_DIRECT, O_SYNC et al for regular fs::File operations.

3 Likes

Thanks,

So, I still need to use O_DIRECT to bypass the OS caching system.

You answered my question!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.