Skip to content

Commit

Permalink
Refactor process_handle_drop_internal() in bevy_asset (bevyengine#1…
Browse files Browse the repository at this point in the history
…0920)

# Objective

- Reduce nesting in `process_handle_drop_internal()`.
- Related to bevyengine#10896.

## Solution

- Use early returns when possible.
- Reduced from 9 levels of indents to 4.
  • Loading branch information
matiqo15 authored Dec 12, 2023
1 parent 5af2f02 commit a7a5d17
Showing 1 changed file with 52 additions and 36 deletions.
88 changes: 52 additions & 36 deletions crates/bevy_asset/src/server/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,35 @@ impl AssetInfos {
}
}

fn remove_dependants_and_labels(
info: &AssetInfo,
loader_dependants: &mut HashMap<AssetPath<'static>, HashSet<AssetPath<'static>>>,
path: &AssetPath<'static>,
living_labeled_assets: &mut HashMap<AssetPath<'static>, HashSet<String>>,
) {
for loader_dependency in info.loader_dependencies.keys() {
if let Some(dependants) = loader_dependants.get_mut(loader_dependency) {
dependants.remove(path);
}
}

let Some(label) = path.label() else {
return;
};

let mut without_label = path.to_owned();
without_label.remove_label();

let Entry::Occupied(mut entry) = living_labeled_assets.entry(without_label) else {
return;
};

entry.get_mut().remove(label);
if entry.get().is_empty() {
entry.remove();
}
}

fn process_handle_drop_internal(
infos: &mut HashMap<UntypedAssetId, AssetInfo>,
path_to_id: &mut HashMap<AssetPath<'static>, UntypedAssetId>,
Expand All @@ -562,44 +591,32 @@ impl AssetInfos {
watching_for_changes: bool,
id: UntypedAssetId,
) -> bool {
match infos.entry(id) {
Entry::Occupied(mut entry) => {
if entry.get_mut().handle_drops_to_skip > 0 {
entry.get_mut().handle_drops_to_skip -= 1;
false
} else {
let info = entry.remove();
if let Some(path) = info.path {
if watching_for_changes {
for loader_dependency in info.loader_dependencies.keys() {
if let Some(dependants) =
loader_dependants.get_mut(loader_dependency)
{
dependants.remove(&path);
}
}
if let Some(label) = path.label() {
let mut without_label = path.to_owned();
without_label.remove_label();
if let Entry::Occupied(mut entry) =
living_labeled_assets.entry(without_label)
{
entry.get_mut().remove(label);
if entry.get().is_empty() {
entry.remove();
}
};
}
}
path_to_id.remove(&path);
}
true
}
}
let Entry::Occupied(mut entry) = infos.entry(id) else {
// Either the asset was already dropped, it doesn't exist, or it isn't managed by the asset server
// None of these cases should result in a removal from the Assets collection
Entry::Vacant(_) => false,
return false;
};

if entry.get_mut().handle_drops_to_skip > 0 {
entry.get_mut().handle_drops_to_skip -= 1;
return false;
}

let info = entry.remove();
let Some(path) = &info.path else {
return true;
};

if watching_for_changes {
Self::remove_dependants_and_labels(
&info,
loader_dependants,
path,
living_labeled_assets,
);
}
path_to_id.remove(path);
true
}

/// Consumes all current handle drop events. This will update information in [`AssetInfos`], but it
Expand All @@ -625,7 +642,6 @@ impl AssetInfos {
}
}
}

/// Determines how a handle should be initialized
#[derive(Copy, Clone, PartialEq, Eq)]
pub(crate) enum HandleLoadingMode {
Expand Down

0 comments on commit a7a5d17

Please sign in to comment.