Move AssetServerStats from AssetInfos to AssetServer#24045
Conversation
andriyDev
left a comment
There was a problem hiding this comment.
I'm not very excited about this change personally. It takes something simple (counting the number of loads), and makes it a whole atomics thing which is inherently more complex.
The additional mutex locks are a little sad, but they are also for "low traffic" APIs like load_folder and load_untyped_async, so I don't care that much about them.
|
|
||
| pub(crate) fn add_started_load_tasks(&self) { | ||
| use core::sync::atomic::Ordering::Relaxed; | ||
| self.data.stats.started_load_tasks.fetch_add(1, Relaxed); |
There was a problem hiding this comment.
Relaxed ordering makes me nervous. It means there's no guarantee afaik that we actually see this increment, whether in the tests or in an actual app.
|
Visibility is unlikely to be a real problem. In single-threaded mode, there's no need to consider ordering — visibility is guaranteed. In multi-threaded mode, while Additionally, even if we change the add function to In fact, the overhead of atomic operations is negligible compared to I/O itself. The main goal here is diagnostic information — modifying it shouldn't involve complex issues like deadlocks. If |
|
Note, visibility in tests is guaranteed even with First, App itself runs in a single thread — no ordering needed at all. For internal multi-threaded functions like Normally, because of the TaskPool scope, the current frame's (or schedule stage's) diagnostic function can at least see the previous frame's (or stage's) results. (The after/before in Schedule also imply memory ordering.) For systems without explicit ordering in the current frame, no extra guarantees are needed — they're already unordered by design. |
Objective
bevy_asset: move
AssetServerStatsstruct fromAssetInfostoAssetServer.In previous versions, adding
stats.started_load_tasksrequired acquiring a write lock onAssetInfos, which was inefficient and prone to deadlocks (e.g., the deadlock fixed by #23980).Now
started_load_taskshas been changed to an atomic variable and moved into AssetServer, eliminating the deadlock issue entirely.Note that acquiring read-write locks inherently requires more atomic operations, so the new implementation also achieves better performance.
Testing
cargo test bevy_asset, passedShowcase
Click to view showcase