bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.14k stars 3.45k forks source link

Investigate replacing Input internals with bitsets #10883

Open james7132 opened 9 months ago

james7132 commented 9 months ago

What problem does this solve or what need does it fill?

For binary inputs, as Input<T> is made for, bitsets tend to be more compact and have generally have better CPU performance than HashSets and use less memory. Full set operations are vectorizable, and set membership checks are single bit tests.

What solution would you like?

Replace the HashSets with fixedbitset or some other bitset implementation.

This will require making some assumptions about the nature of some input types. For example, gamepad inputs currently support arbitrary controller and button counts, which makes it difficult to map a gamepad/button combination into a bitset index. However, it's exceedingly rare to find a gamepad that has more than 255 buttons, so if we can restrict that, we can use 256 * gamepad index + button index as the bitset index for a gamepad button.

What alternative(s) have you considered?

Leave it as is.

Additional context

I'm primarily filing this bug to jot down an idea that's been popping up here and there. I personally think this is a very low priority performance optimization.

s-puig commented 6 months ago

I have a branch with inputs moved into Gamepad so it was reasonably easy to also make a dirty branch that uses FixedBitSet. Needs proper benchmarking, rather than the completely unscientific method of mashing keys on gamepad_viewer, but at a quick glance bitset seems a bit faster.