Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace EM_BOOL with normal C/C++ bool type. NFC #22155

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Jun 27, 2024

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.

@sbc100 sbc100 requested review from dschuff and kripken June 27, 2024 17:42
@sbc100
Copy link
Collaborator Author

sbc100 commented Jun 27, 2024

This change was created using sed to substitutions

@sbc100
Copy link
Collaborator Author

sbc100 commented Jun 27, 2024

Updated to include struct size saves and code size savings.

sbc100 added a commit to sbc100/emscripten-glfw that referenced this pull request Jun 27, 2024
I'm trying to change the type of EM_BOOL and this is currently
blocking that:
  emscripten-core/emscripten#22155
@sbc100
Copy link
Collaborator Author

sbc100 commented Jun 27, 2024

I could do this in two phases perhaps:

  • Narrow the type of EM_BOOL
  • Remove the use of EM_BOOL

@kripken
Copy link
Member

kripken commented Jun 27, 2024

Why do struct sizes change? I'd expect an int and an unpacked bool to both take 32 bits. Is that wrong?

@sbc100
Copy link
Collaborator Author

sbc100 commented Jun 27, 2024

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;
}

@sbc100
Copy link
Collaborator Author

sbc100 commented Jun 27, 2024

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)

@kripken
Copy link
Member

kripken commented Jun 27, 2024

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...

@kripken
Copy link
Member

kripken commented Jun 27, 2024

Anyhow, yeah, if this causes struct layout changes then splitting the PR as much as possible sgtm.

@sbc100
Copy link
Collaborator Author

sbc100 commented Jun 27, 2024

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.

sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 27, 2024
This reduces the size of several structs and results in code size
savings in some cases.

This change is split of from a larger change I have planned to remove
the use of EM_BOOL completely: emscripten-core#22155.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 27, 2024
This reduces the size of several structs and results in code size
savings in some cases.

This change is split of from a larger change I have planned to remove
the use of EM_BOOL completely: emscripten-core#22155.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 27, 2024
This reduces the size of several structs and results in code size
savings in some cases.

This change is split of from a larger change I have planned to remove
the use of EM_BOOL completely: emscripten-core#22155.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 27, 2024
This reduces the size of several structs and results in code size
savings in some cases.

This change is split of from a larger change I have planned to remove
the use of EM_BOOL completely: emscripten-core#22155.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 27, 2024
This reduces the size of several structs and results in code size
savings in some cases.

This change is split of from a larger change I have planned to remove
the use of EM_BOOL completely: emscripten-core#22155.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 28, 2024
This reduces the size of several structs and results in code size
savings in some cases.

This change is split of from a larger change I have planned to remove
the use of EM_BOOL completely: emscripten-core#22155.
sbc100 added a commit that referenced this pull request Jun 28, 2024
This reduces the size of several structs and can result in code size
savings in some cases. The reason
the code size savings don't show up in trivial examples is (I believe)
because this change also increases
the use of HEAP8 (where previously some examples only depended on
HEAP32).

This change is split of from a larger change I have planned to remove
the use of EM_BOOL completely: #22155.
@sbc100
Copy link
Collaborator Author

sbc100 commented Jun 29, 2024

Rebased now that #22157 has landed. I'm planning on waiting for a release or two before landing this larger change.

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm in a separate release as you said.

Also, do we have a test that EM_BOOL, EM_TRUE, EM_FALSE still work as legacy?

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.
@sbc100
Copy link
Collaborator Author

sbc100 commented Jul 1, 2024

Added a test.

@kripken
Copy link
Member

kripken commented Jul 1, 2024

Thanks - which is it though? (this is a pretty big diff and I don't see a separate commit for it, this would have been a nice case to not squash actually I think?)

@sbc100
Copy link
Collaborator Author

sbc100 commented Jul 1, 2024

Added test as a separate commit

@kripken
Copy link
Member

kripken commented Jul 1, 2024

Thanks, lgtm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants