]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Adjust bitfields in struct Condition
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 1 Jul 2025 11:39:00 +0000 (13:39 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 8 Jul 2025 08:22:59 +0000 (10:22 +0200)
As is usually the case, the bitfields don't create the expected space savings,
because the field that follows needs to be aligned. But we don't want to fully
drop the bitfields here, because then ConditionType and ConditionResult are
each 4 bytes, and the whole struct grows from 32 to 40 bytes (on amd64). We
potentially have lots of little Conditions and that'd waste some memory.

Make each of the four fields one byte. This still allows the compiler to
generate simpler code without changing the struct size:

E.g. in condition_test:
                 c->result = CONDITION_ERROR;
-   78fab:      48 8b 45 e8             mov    -0x18(%rbp),%rax
-   78faf:      0f b6 50 01             movzbl 0x1(%rax),%edx
-   78fb3:      83 e2 03                and    $0x3,%edx
-   78fb6:      83 ca 0c                or     $0xc,%edx
-   78fb9:      88 50 01                mov    %dl,0x1(%rax)
+   78f8b:      48 8b 45 e8             mov    -0x18(%rbp),%rax
+   78f8f:      c6 40 03 03             movb   $0x3,0x3(%rax)

src/shared/condition.h

index c25b9643b90b679fbaca0a013c9a37188398a01c..90d896c85ce2a7fec9828f11037ad405866d440a 100644 (file)
@@ -58,12 +58,13 @@ typedef enum ConditionResult {
 } ConditionResult;
 
 typedef struct Condition {
+        /* Use bitfields for ConditionType and ConditionResult to keep the whole struct in 32 bytes. */
         ConditionType type:8;
 
-        bool trigger:1;
-        bool negate:1;
+        bool trigger;
+        bool negate;
 
-        ConditionResult result:6;
+        ConditionResult result:8;
 
         char *parameter;