Description
I found that preparing queries that match no entities were eating a lot of time in our game. For reference, we run on the order of 200 queries per frame. Here is the timing of some of our fastest systems. There is a big cliff between the systems that don't query the world versus those that do:
I tried refactoring one of these systems to use PreparedQuery
instead and saw a speedup of around 15x when that query matches no entities. Here is the before and after code of the system: https://gist.github.com/LPGhatguy/dcec605c3b6d058332f5ae527af6efde
...so naturally I thought I'd try caching all of my queries automatically! I implemented a quick bodge around World::query
to do that caching: https://gist.github.com/LPGhatguy/2c6180540e45c6fd411456af560f9d64
The speedup on the fastest systems was a lot and I saved around 1.5ms per frame! Here's the updated chart of the fastest systems for comparison:
I imagine that a hecs-native solution could be faster, use less janky unsafe, and then would apply to everyone who uses the library. I've found that this change is a very large speedup and maybe it would be for other games too!