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

Use bitfield instead of bools in Response and Sense #5556

Merged
merged 4 commits into from
Jan 6, 2025

Conversation

polwel
Copy link
Contributor

@polwel polwel commented Jan 1, 2025

Closes #3862.

Factoring the bool members of Response into a bitfield, the size of Response is now 96 bytes (down from 104).

I gave Sense the same treatment, however this has no effects on Response due to padding. I've decided not to pursue PointerState, as it is quite large (many members that are sized and aligned to multiples of 8 bytes), so I don't expect any noticeable benefit from making handful of bools slightly leaner.

In any case, the changes to Sense are already quite a bit more intrusive than those to Response.
The previous implementation overloaded the names of the attributes click and drag with similarly named methods that construct Sense with the corresponding flag set. Now, that the attributes can no longer be accessed directly, I had to introduce methods with new names (senses_click(), senses_drag() and is_focusable()). I don't think this is the cleanest solution: the old methods are essentially redundant now that the named constants like Sense::CLICK exist. I did however not want to needlessly break backwards compatibility.
I am happy to revert it (or go further 🙂) if there are concerns.

crates/egui/src/response.rs Outdated Show resolved Hide resolved
@polwel polwel changed the title Use bitfield instead of bools in Response (#3862) Use bitfield instead of bools in Response Jan 4, 2025
Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

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

Look great - thank you!

crates/egui/src/context.rs Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
crates/egui/src/widgets/text_edit/builder.rs Outdated Show resolved Hide resolved
crates/egui/src/response.rs Outdated Show resolved Hide resolved
crates/egui/src/response.rs Show resolved Hide resolved
crates/egui/src/response.rs Outdated Show resolved Hide resolved
@emilk emilk added performance Lower CPU/GPU usage (optimize) egui labels Jan 6, 2025
@emilk emilk changed the title Use bitfield instead of bools in Response Use bitfield instead of bools in Response and Sense Jan 6, 2025
Copy link

github-actions bot commented Jan 6, 2025

Preview available at https://egui-pr-preview.github.io/pr/5556-bitflags
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

@polwel
Copy link
Contributor Author

polwel commented Jan 6, 2025

Thanks for the review! I will address your comments shortly.

I see one CI job is failing, but it does not look like something I touched? I can fix it, but should I?

EDIT NVM, I see it's already corrected in master. Will rebase.

@polwel polwel force-pushed the bitflags branch 2 times, most recently from a69fa29 to 507c3e9 Compare January 6, 2025 13:30
&& memory.has_focus(id)
&& (input.key_pressed(Key::Space) || input.key_pressed(Key::Enter))
{
// Space/enter works like a primary click for e.g. selected buttons
res.fake_primary_click = true;
res.flags.set(Flags::FAKE_PRIMARY_CLICKED, true);
Copy link
Owner

Choose a reason for hiding this comment

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

Nit: these could be shorter by using += or |=

Suggested change
res.flags.set(Flags::FAKE_PRIMARY_CLICKED, true);
res.flags += Flags::FAKE_PRIMARY_CLICKED;

@emilk emilk merged commit 3586041 into emilk:master Jan 6, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
egui performance Lower CPU/GPU usage (optimize)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance: replace bools in Response with bit-sets
2 participants