-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Copy on Write AssetPaths #9729
Copy on Write AssetPaths #9729
Conversation
Reasonable enough, but it is a shame we don't have benchmarks or even stress tests for asset processing currently. |
We have a pseudo stress test in the form of running the asset processor on everything in the I ran that about 10 times for both main and this pr before submitting it and the change here was dwarfed by very swingey results (my guess is filesystem i/o). We might need something a bit more targeted to isolate the perf difference. |
However given that |
Yep, agreed with your instincts here. Thanks for testing that; I'll spin out an issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CowArc
is missing Display
and also Ord
and PartialOrd
to be complet. But since the last two are not used in bevy I think it's ok to omit them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense but would be good to see benchmarks, maybe also to see how much memory pressure is related to this.
I think there's a couple crates out there that try to make atomic string types as small as possible (at the cost of indirection). If this turns out to be performance critical, maybe some of their tricks can be implemented generically here, too.
Co-authored-by: Pascal Hertleif <killercup@gmail.com>
Something else interesting we could consider: add a This would do two things:
The theory being that a very high percentage of paths defined in user code are static strs. AssetLoaders basically never use static strs, but every case in the built in Bevy AssetLoader code uses But it would come at the cost of nasty ergonomics / confusing apis for borrowed It would also make CowArc much more opinionated. But this would likely save a lot of work on average and so far the UX hit seems pretty much non-existent. I had to change zero Bevy examples or AssetLoader impls after the change. |
I just pushed the change to illustrate. We can revert if y'all don't like it. |
# Objective - Silence `Failed to send DropEvent for StrongHandle "SendError(..)"` errors when `StrongHandle` were dropped during application shutdown. - Re-export `BoxedFuture` considering that it's used everywhere in bevy_asset - Fixed an issue introduced by #9729. ``` Asset 'final_gather.rgen' encountered an error in dust_render::shader::spirv::SpirvLoader: Failed to deserialize asset meta: SpannedError { code: InvalidValueForType { expected: "string AssetPath", found: "a sequence" }, position: Position { line: 9, col: 24 } } ``` Basically, for processed assets with dependencies, bevy will serialize the metafile as follows: ``` ( meta_format_version: "1.0", processed_info: Some(( hash: (203, 239, 108, 156, 180, 23, 157, 217, 159, 36, 158, 193, 185, 253, 242, 156), full_hash: (77, 58, 30, 200, 21, 180, 221, 133, 151, 83, 247, 47, 193, 70, 228, 97), process_dependencies: [ ( full_hash: (56, 46, 55, 118, 3, 6, 213, 250, 124, 26, 153, 87, 15, 85, 4, 89), path: ("standard.glsl"), # <<---------- See here ), ], )), asset: Load( loader: "dust_render::shader::spirv::SpirvLoader", settings: (), ), ) ``` `AssetPath` gets serialized as `("standard.glsl")` which was then deserialized as a sequence instead of our `AssetPath`. ## Solution - Serialize `AssetPath` directly as a string instead. The above metafile would be serialized as follows: ``` ( meta_format_version: "1.0", processed_info: Some(( hash: (203, 239, 108, 156, 180, 23, 157, 217, 159, 36, 158, 193, 185, 253, 242, 156), full_hash: (77, 58, 30, 200, 21, 180, 221, 133, 151, 83, 247, 47, 193, 70, 228, 97), process_dependencies: [ ( full_hash: (56, 46, 55, 118, 3, 6, 213, 250, 124, 26, 153, 87, 15, 85, 4, 89), path: "standard.glsl", # <<------- No longer a tuple struct ), ], )), asset: Load( loader: "dust_render::shader::spirv::SpirvLoader", settings: (), ), ) ``` ---
# Objective The `AssetServer` and `AssetProcessor` do a lot of `AssetPath` cloning (across many threads). To store the path on the handle, to store paths in dependency lists, to pass an owned path to the offloaded thread, to pass a path to the LoadContext, etc , etc. Cloning multiple string allocations multiple times like this will add up. It is worth optimizing this. Referenced in bevyengine#9714 ## Solution Added a new `CowArc<T>` type to `bevy_util`, which behaves a lot like `Cow<T>`, but the Owned variant is an `Arc<T>`. Use this in place of `Cow<str>` and `Cow<Path>` on `AssetPath`. --- ## Changelog - `AssetPath` now internally uses `CowArc`, making clone operations much cheaper - `AssetPath` now serializes as `AssetPath("some_path.extension#Label")` instead of as `AssetPath { path: "some_path.extension", label: Some("Label) }` ## Migration Guide ```rust // Old AssetPath::new("logo.png", None); // New AssetPath::new("logo.png"); // Old AssetPath::new("scene.gltf", Some("Mesh0"); // New AssetPath::new("scene.gltf").with_label("Mesh0"); ``` `AssetPath` now serializes as `AssetPath("some_path.extension#Label")` instead of as `AssetPath { path: "some_path.extension", label: Some("Label) }` --------- Co-authored-by: Pascal Hertleif <killercup@gmail.com>
# Objective - Silence `Failed to send DropEvent for StrongHandle "SendError(..)"` errors when `StrongHandle` were dropped during application shutdown. - Re-export `BoxedFuture` considering that it's used everywhere in bevy_asset - Fixed an issue introduced by bevyengine#9729. ``` Asset 'final_gather.rgen' encountered an error in dust_render::shader::spirv::SpirvLoader: Failed to deserialize asset meta: SpannedError { code: InvalidValueForType { expected: "string AssetPath", found: "a sequence" }, position: Position { line: 9, col: 24 } } ``` Basically, for processed assets with dependencies, bevy will serialize the metafile as follows: ``` ( meta_format_version: "1.0", processed_info: Some(( hash: (203, 239, 108, 156, 180, 23, 157, 217, 159, 36, 158, 193, 185, 253, 242, 156), full_hash: (77, 58, 30, 200, 21, 180, 221, 133, 151, 83, 247, 47, 193, 70, 228, 97), process_dependencies: [ ( full_hash: (56, 46, 55, 118, 3, 6, 213, 250, 124, 26, 153, 87, 15, 85, 4, 89), path: ("standard.glsl"), # <<---------- See here ), ], )), asset: Load( loader: "dust_render::shader::spirv::SpirvLoader", settings: (), ), ) ``` `AssetPath` gets serialized as `("standard.glsl")` which was then deserialized as a sequence instead of our `AssetPath`. ## Solution - Serialize `AssetPath` directly as a string instead. The above metafile would be serialized as follows: ``` ( meta_format_version: "1.0", processed_info: Some(( hash: (203, 239, 108, 156, 180, 23, 157, 217, 159, 36, 158, 193, 185, 253, 242, 156), full_hash: (77, 58, 30, 200, 21, 180, 221, 133, 151, 83, 247, 47, 193, 70, 228, 97), process_dependencies: [ ( full_hash: (56, 46, 55, 118, 3, 6, 213, 250, 124, 26, 153, 87, 15, 85, 4, 89), path: "standard.glsl", # <<------- No longer a tuple struct ), ], )), asset: Load( loader: "dust_render::shader::spirv::SpirvLoader", settings: (), ), ) ``` ---
Objective
The
AssetServer
andAssetProcessor
do a lot ofAssetPath
cloning (across many threads). To store the path on the handle, to store paths in dependency lists, to pass an owned path to the offloaded thread, to pass a path to the LoadContext, etc , etc. Cloning multiple string allocations multiple times like this will add up. It is worth optimizing this.Referenced in #9714
Solution
Added a new
CowArc<T>
type tobevy_util
, which behaves a lot likeCow<T>
, but the Owned variant is anArc<T>
. Use this in place ofCow<str>
andCow<Path>
onAssetPath
.Changelog
AssetPath
now internally usesCowArc
, making clone operations much cheaperAssetPath
now serializes asAssetPath("some_path.extension#Label")
instead of asAssetPath { path: "some_path.extension", label: Some("Label) }
Migration Guide
AssetPath
now serializes asAssetPath("some_path.extension#Label")
instead of asAssetPath { path: "some_path.extension", label: Some("Label) }