binrw icon indicating copy to clipboard operation
binrw copied to clipboard

For a fixed-size struct, is it possible to read or write in a single operation?

Open lethargy123 opened this issue 4 months ago • 1 comments

I wrote a demo using binrw, and upon observing the read and write process, I found that for fixed-size structs, binrw always reads or writes the struct members one by one. This seems quite odd. In the case of a large number of structs, frequent reads or writes could impact performance.

Here is the demo code:

rust #[binrw] pub struct A { a: u32, b: [u8; 10], } This block of code should be able to be read or written in one go, but when I use a NoSeek-wrapped reader or writer to read or write, I observed that it first reads or writes the a in A, and then reads or writes the b in A.

lethargy123 avatar Sep 10 '25 03:09 lethargy123

Hi, thanks for your report! I thought this was written already, but I guess it was just in my head (or maybe in some discussion thread somewhere). This is an unimplemented optimisation, along with one to coalesce objects composed of primitive types into a single read. The current optimisations available through fake specialisation are limited, see https://docs.rs/binrw/latest/binrw/docs/performance/index.html#use-specific-types-for-faster-block-io. The most severe performance impacts are mitigated by using buffered streams.

csnover avatar Sep 10 '25 03:09 csnover