What is the most efficient way to represent small values in a struct?

前端 未结 15 845
星月不相逢
星月不相逢 2021-02-01 02:15

Often I find myself having to represent a structure that consists of very small values. For example, Foo has 4 values, a, b, c, d that, range from

15条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 02:54

    For dense packing that doesn't incur a large overhead of reading, I'd recommend a struct with bitfields. In your example where you have four values ranging from 0 to 3, you'd define the struct as follows:

    struct Foo {
        unsigned char a:2;
        unsigned char b:2;
        unsigned char c:2;
        unsigned char d:2;
    }
    

    This has a size of 1 byte, and the fields can be accessed simply, i.e. foo.a, foo.b, etc.

    By making your struct more densely packed, that should help with cache efficiency.

    Edit:

    To summarize the comments:

    There's still bit fiddling happening with a bitfield, however it's done by the compiler and will most likely be more efficient than what you would write by hand (not to mention it makes your source code more concise and less prone to introducing bugs). And given the large amount of structs you'll be dealing with, the reduction of cache misses gained by using a packed struct such as this will likely make up for the overhead of bit manipulation the struct imposes.

提交回复
热议问题