BlackPhlox / bevy_midi

Send and receive MIDI data to and from bevy using DAWS or MIDI Controllers
Apache License 2.0
56 stars 10 forks source link

Io Task Pool starvation #24

Closed joverwey closed 12 months ago

joverwey commented 1 year ago

The way bevy_midi uses task pools causes thread starvation on machines with 8 cores or less, like the Apple Mac M1.

The way bevy assigns cores to background tasks pools are defined in task_pool_options.rs. The IoTaskPool gets 25% of the available cores, with a max of 4. If you only have 8 cores, that is only 2 cores for IO.

Each of the midi_input and midi_out background threads occupies a thread in the task pool. These threads never exit until the application is finished. This means no other IO tasks can run as soon as these two threads are active. I'm not sure the task pools are intended to be used in this way. Tasks should eventually complete to allow other tasks to run.

This prevents other assets from being loaded, like the midi mesh keys in the 'piano' example.

It would help to use the AsyncComputeTaskPool instead of the IoTaskPool. It will still break if the downstream app uses the AsyncComputeTaskPool, but most apps, including the demo example will use the IoTaskPool for loading assets.

Have you considered just running it as a system instead of a background thread? Instead of doing recv() on the channel you can do try_recv(). This will not block if there are no messages in the queue. Let me know what you think. I could do a pull request if you are happy with this direction.

BlackPhlox commented 1 year ago

Oh wow, didn't realize had would have an impact but totally makes sense! The history behind using IoTaskPool was basically just a coincidence. I started by using inputs as bevy events but due to the events having a lot of overhead and introducing delay, I just arbitrarily chose IoTaskPool for handling inputs. Personally, the original goal was to minimize the delay from the point of input. But if keep the performance and make it more usable, including try_recv, all the better! 🚀 Thanks for taking your time with this, highly appreciated!😊