Use packed struct with bitfields to shrink all member modes down to 32-bits#236
Conversation
MatthieuDartiailh
left a comment
There was a problem hiding this comment.
I personally love getting rid off the manual masking.
Out of curiosity what is the advantage to move modes and index at the end of the Member struct ?
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #236 +/- ##
=======================================
Coverage 97.67% 97.67%
=======================================
Files 24 24
Lines 1074 1074
Branches 162 162
=======================================
Hits 1049 1049
Misses 12 12
Partials 13 13 |
|
My understanding is compilers will insert padding bytes depending on the size of fields and alignment. So if you have a struct like Unless it is packed the compiler might/will insert 4 padding bytes after b so all fields are 8 byte aligned. By sorting fields by their size it minimizes any padding. Edit: This is why the sizeof(Member) shrunk to 128 bytes from 144 even though only 12 bytes were removed. |
|
I just realized it works using PACK(struct MemberModes
{
GetAttr::Mode getattr: 4;
PostGetAttr::Mode post_getattr: 3;
SetAttr::Mode setattr: 4;
PostSetAttr::Mode post_setattr: 3;
DefaultValue::Mode default_value: 4;
Validate::Mode validate: 5;
PostValidate::Mode post_validate: 3;
DelAttr::Mode delattr: 3;
GetState::Mode getstate: 3;
});So the get/set methods could just all be dropped. |
|
Neat trick! I'll remember this next time I need bitfields. |
All of the modes can fit into 32 bits. This shrinks the member struct down to 128 bytes (from 144) with an alignment of 8.
The compiler also nicely does all the bit fiddling.