rust-bitfield
rust-bitfield copied to clipboard
Add design notes
Could you please add an overview of the design to the documentation? I can't get an idea for how efficiently this crate is implemented without reading the source, and macro source is hard to read.
Thanks for the suggestion, I will try to add something.
If you want to see the resulting code to evaluate it, you can use cargo expand
on a small project with just your bitfield definition.You can also use cargo asm
to check the resulting generated assembly.
In practice, because the bit offsets are constant and LLVM is very good, the generated assembly is often efficient, sometimes only a shr
and and
instructions to read a field for examples. Even in some case where the code generated by the macros seems bad.
But there is few things to watch for:
- If you use the array form (like
bitfield! { struct ArrayBitfield([u8]); ...
) and use the bitfield with something else than a fixed size array, there will likely be a bound check and the generated assembly is not always that great. With a fixed size array, LLVM seems to do a really great job. - if you use an array field (like
foo5, set_foo5: 0, 0, 32;
), the resulting code may not be as efficient because theBitRange
methods are called with non constant parameters. If you use a array field and use it with constants index, making the field#[inline]
may help.