Skip to content

Conversation

@ZackaryRippee
Copy link

Objective

  • Fixes get_path_ref method for AssetServer #10685
  • When working with a large number of asset handles, extracting paths using AssetServer::get_path requires cloning the AssetPath for each handle, which can be inefficient
  • Users need a way to access asset paths by reference to avoid unnecessary cloning when iterating over many handles

Solution

  • Added AssetServer::get_path_ref() method that returns a reference to the asset path without cloning
  • Implemented AssetPathRef struct that holds a read lock guard and dereferences to AssetPath<'static>
  • Added internal AssetInfos::get_path_ref() helper method to retrieve path references from asset indices
  • The returned AssetPathRef implements Deref<Target = AssetPath<'static>>, allowing transparent access to the path while keeping the lock alive

Testing

  • The code compiles successfully with cargo check --package bevy_asset
  • The implementation follows the same pattern as existing AssetServer methods that use read locks
  • Manual testing can be done by:
    1. Loading multiple assets with AssetServer::load()
    2. Calling get_path_ref() on asset IDs/handles
    3. Verifying the returned path matches the loaded asset path
    4. Confirming no unnecessary cloning occurs during iteration

Reviewers can test by:

let asset_server = world.resource::<AssetServer>();
let handle: Handle<Image> = asset_server.load("texture.png");
if let Some(path_ref) = asset_server.get_path_ref(handle.id()) {
    println!("Asset path: {}", path_ref.path().display());
}

@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Nov 23, 2025
@andriyDev
Copy link
Contributor

andriyDev commented Nov 23, 2025

@ZackaryRippee Is this really a problem? AssetPath internally contains 3 CowArc's. So when you clone an AssetPath<'static>, you're really just cloning 3 Arcs (since the AssetServer already stores AssetPath<'static>). Doing double the lookups is much more likely to be a perf problem than the clone IMO.

Slightly related: I would absolutely be in favor of making AssetServer::get_path return AssetPath<'static>, since that is what we return, but it gets coerced into AssetPath<'self> instead, requiring an additional clone just to get it static again. Not a big deal, but seems like an obvious improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Assets Load files from disk to use for things like images, models, and sounds C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_path_ref method for AssetServer

4 participants