-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Task System for Bevy #318
Comments
This is awesome I'm so glad we have people like you contributing to bevy |
It seems like there is consensus to move this forward. I think we are quite close to having a PR ready :) We had a bit more discussion on discord tonight from a number of participants, mainly about how this would work within a browser:
|
You can see an example here how amethyst integrated web workers into rayon. Rayon allows specifying The main blocker for amethyst was
The implications of async |
@chemicstry I looked into the proposal for One way of completely sidestepping the "no blocking the main thread" problem is to only run bevy in workers when on the web. Those can block no problem. We can render with an offscreen canvas. |
@lachlansneff async is the only way to achieve "blocking" execution in web context. Any wasm execution must immediately return to prevent blocking main thread and this inevitably loses stack, so you can't resume execution. You can emulate this async behavior like I mentioned in 2nd solution, but it would be less performant than async atomics because of worker message overhead. We explored the option of running everything in web workers, but it has its downsides:
The list is probably longer. It would require a lot of glue to make everything run in web workers, but if this could be implemented as a separate glue logic instead of a bunch of cfg macros, then it may very well be a desired solution. Although, I would still say that avoiding locks in main thread and relying on ECS for ensuring safe shared access is a good pattern from performance and code quality perspective. |
@chemicstry I think most people are in agreement that Bevy should be focusing on targeting the web as it stands to exist sometime around when Bevy hits 1.0 (or more generally: targeting some future state of the web.) This means avoiding development of suboptimal solutions in an attempt to make something work today. So with that in mind, and recognizing that you did a lot of research in this area:
No disagreement here regarding solutions that avoid this problem altogether! |
Task System for Bevy
Currently bevy depends on rayon for multithreaded dispatching of systems. @lachlansneff and @aclysma have iterated on a prototype with feedback from @kabergstrom and @cart. This issue is meant to:
Why Replace Rayon?
What Would the Alternative Be?
@lachlansneff and @aclysma implemented a prototype using
multitask.
It’s a small executor based onasync-task
, which is used withinasync-std
. The dependencies are:The API does three things:
We have a prototype of ParallelExecutor using it instead of rayon, allowing us to remove rayon as a dependency from bevy. This repo is a prototype and we intend to add it as a module within bevy directly. (bevy_tasks) This will allow us to do more sophisticated instrumentation and profiling than if we were using an externally-managed thread pool.
Advantages/Disadvantages
Advantages:
Disadvantages:
Tradeoffs:
Short Term Plan
Finish a PR to add bevy_tasks as a module, remove rayon as a dependency, and update ParallelExecutor to work with bevy_tasks. In principle these steps are already done, but we may want to polish a bit first.
We have a feature branch for this underway here: https://github.com/lachlansneff/bevy/tree/bevy-tasks
Long Term Plan
Thread management is clearly a key problem that needs to be solved in a scalable game engine. In my opinion there are three main uses of threads in a modern, ECS-based game engine, from high-level to low-level:
a. Some systems might wait for tasks to complete before returning
b. Other systems might start jobs to be finished later in the frame or in a future frame
We plan to apply this solution to #2 now, and longer term expose the same underlying system to solve #3. (#1 is out of scope for now, but might also be able to use this system)
We discussed #3 in the #rendering channel in discord:
Configuration of Task Buckets
@lachlansneff and @aclysma also discussed the need to assign threads to the proposed buckets (IO, async compute, and compute). We considered several approaches:
Potential Future Improvements to bevy_tasks
Next Steps
The text was updated successfully, but these errors were encountered: