Open sbc100 opened 3 days ago
This change was created using sed to substitutions
Updated to include struct size saves and code size savings.
I could do this in two phases perhaps:
Why do struct sizes change? I'd expect an int and an unpacked bool to both take 32 bits. Is that wrong?
Why do struct sizes change? I'd expect an int and an unpacked bool to both take 32 bits. Is that wrong?
I guess bool
is just a single byte in C/C++?
Indeed this program printf 1 1
on my desktop system:
$ cat test.c
#include <stdbool.h>
#include <stdio.h>
int main() {
printf("%ld %ld\n", sizeof(bool), _Alignof(bool));
return 0;
}
Why do struct sizes change? I'd expect an int and an unpacked bool to both take 32 bits. Is that wrong?
I guess
bool
is just a single byte in C/C++?Indeed this program printf
1 1
on my desktop system:$ cat test.c #include <stdbool.h> #include <stdio.h> int main() { printf("%ld %ld\n", sizeof(bool), _Alignof(bool)); return 0; }
(same for C++ BTW)
By itself it's 1 byte, I guess, but inside a struct the field after it might be aligned:
#include <stdbool.h>
#include <stdio.h>
class C {
bool x;
int y;
};
int main() {
printf("%ld\n", sizeof(C));
return 0;
}
That prints 8
for me, so 4 bytes for each field. I'd expect that to be the common case, I'm surprised we save anything actually...
Anyhow, yeah, if this causes struct layout changes then splitting the PR as much as possible sgtm.
By itself it's 1 byte, I guess, but inside a struct the field after it might be aligned:
#include <stdbool.h> #include <stdio.h> class C { bool x; int y; }; int main() { printf("%ld\n", sizeof(C)); return 0; }
That prints
8
for me, so 4 bytes for each field. I'd expect that to be the common case, I'm surprised we save anything actually...
Right, that is just normal struct layout rules. If you put the bool
last you will see difference. Or if you have several booleans toegther in the struct.
Lowering the alignment of a type can for sure shrink struct sizes. That is the primary use of the alignment of a given type. Higher alignment == worse for struct size.
Rebased now that #22157 has landed. I'm planning on waiting for a release or two before landing this larger change.
There is no need that I can think of to use a custom type/macro here.
This also reduces the size of several structs due to the fact that the C/C++ bool type is smaller than int.
Keep EM_BOOL/EM_TRUE/EM_FALSE around for backwards compat but don't use them internally anymore.