From 2f358c9889ff0e129dac5ba6cd8de24c7a1fde97 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 12:01:21 +0100 Subject: [PATCH 01/19] set immutable key for HashMaps --- .../src/io/embedded/embedded_watcher.rs | 6 ++--- crates/bevy_asset/src/io/embedded/mod.rs | 6 ++--- crates/bevy_asset/src/io/gated.rs | 13 ++++----- crates/bevy_asset/src/io/memory.rs | 27 ++++++++++++++----- crates/bevy_asset/src/lib.rs | 12 +++------ crates/bevy_asset/src/processor/mod.rs | 4 +-- crates/bevy_asset/src/server/loaders.rs | 6 ++--- crates/bevy_ecs/src/bundle.rs | 4 +-- crates/bevy_ecs/src/storage/table.rs | 4 +-- crates/bevy_gltf/src/lib.rs | 14 +++++----- crates/bevy_gltf/src/loader.rs | 12 ++++----- crates/bevy_gltf/src/vertex_attributes.rs | 4 +-- .../src/render_resource/pipeline_cache.rs | 4 +-- tools/build-templated-pages/src/examples.rs | 7 ++--- 14 files changed, 65 insertions(+), 58 deletions(-) diff --git a/crates/bevy_asset/src/io/embedded/embedded_watcher.rs b/crates/bevy_asset/src/io/embedded/embedded_watcher.rs index 6e92caa5d3bb3..1e7f3057bbfd2 100644 --- a/crates/bevy_asset/src/io/embedded/embedded_watcher.rs +++ b/crates/bevy_asset/src/io/embedded/embedded_watcher.rs @@ -25,7 +25,7 @@ pub struct EmbeddedWatcher { impl EmbeddedWatcher { pub fn new( dir: Dir, - root_paths: Arc>>, + root_paths: Arc, PathBuf>>>, sender: crossbeam_channel::Sender, debounce_wait_time: Duration, ) -> Self { @@ -49,7 +49,7 @@ impl AssetWatcher for EmbeddedWatcher {} /// the initial static bytes from the file embedded in the binary. pub(crate) struct EmbeddedEventHandler { sender: crossbeam_channel::Sender, - root_paths: Arc>>, + root_paths: Arc, PathBuf>>>, root: PathBuf, dir: Dir, last_event: Option, @@ -61,7 +61,7 @@ impl FilesystemEventHandler for EmbeddedEventHandler { fn get_path(&self, absolute_path: &Path) -> Option<(PathBuf, bool)> { let (local_path, is_meta) = get_asset_path(&self.root, absolute_path); - let final_path = self.root_paths.read().get(&local_path)?.clone(); + let final_path = self.root_paths.read().get(local_path.as_path())?.clone(); if is_meta { warn!("Meta file asset hot-reloading is not supported yet: {final_path:?}"); } diff --git a/crates/bevy_asset/src/io/embedded/mod.rs b/crates/bevy_asset/src/io/embedded/mod.rs index bf22602436470..9af5e0ec55d18 100644 --- a/crates/bevy_asset/src/io/embedded/mod.rs +++ b/crates/bevy_asset/src/io/embedded/mod.rs @@ -22,7 +22,7 @@ pub const EMBEDDED: &str = "embedded"; pub struct EmbeddedAssetRegistry { dir: Dir, #[cfg(feature = "embedded_watcher")] - root_paths: std::sync::Arc>>, + root_paths: std::sync::Arc, PathBuf>>>, } impl EmbeddedAssetRegistry { @@ -35,7 +35,7 @@ impl EmbeddedAssetRegistry { #[cfg(feature = "embedded_watcher")] self.root_paths .write() - .insert(full_path.to_owned(), asset_path.to_owned()); + .insert(full_path.into_boxed_path(), asset_path.to_owned()); self.dir.insert_asset(asset_path, value); } @@ -48,7 +48,7 @@ impl EmbeddedAssetRegistry { #[cfg(feature = "embedded_watcher")] self.root_paths .write() - .insert(full_path.to_owned(), asset_path.to_owned()); + .insert(Box::from(full_path), asset_path.to_owned()); self.dir.insert_meta(asset_path, value); } diff --git a/crates/bevy_asset/src/io/gated.rs b/crates/bevy_asset/src/io/gated.rs index 8a049263dab70..4db9d755d3487 100644 --- a/crates/bevy_asset/src/io/gated.rs +++ b/crates/bevy_asset/src/io/gated.rs @@ -2,10 +2,7 @@ use crate::io::{AssetReader, AssetReaderError, PathStream, Reader}; use bevy_utils::{BoxedFuture, HashMap}; use crossbeam_channel::{Receiver, Sender}; use parking_lot::RwLock; -use std::{ - path::{Path, PathBuf}, - sync::Arc, -}; +use std::{path::Path, sync::Arc}; /// A "gated" reader that will prevent asset reads from returning until /// a given path has been "opened" using [`GateOpener`]. @@ -13,7 +10,7 @@ use std::{ /// This is built primarily for unit tests. pub struct GatedReader { reader: R, - gates: Arc, Receiver<()>)>>>, + gates: Arc, (Sender<()>, Receiver<()>)>>>, } impl Clone for GatedReader { @@ -27,7 +24,7 @@ impl Clone for GatedReader { /// Opens path "gates" for a [`GatedReader`]. pub struct GateOpener { - gates: Arc, Receiver<()>)>>>, + gates: Arc, (Sender<()>, Receiver<()>)>>>, } impl GateOpener { @@ -36,7 +33,7 @@ impl GateOpener { pub fn open>(&self, path: P) { let mut gates = self.gates.write(); let gates = gates - .entry(path.as_ref().to_path_buf()) + .entry(Box::from(path.as_ref())) .or_insert_with(crossbeam_channel::unbounded); gates.0.send(()).unwrap(); } @@ -65,7 +62,7 @@ impl AssetReader for GatedReader { let receiver = { let mut gates = self.gates.write(); let gates = gates - .entry(path.to_path_buf()) + .entry(Box::from(path)) .or_insert_with(crossbeam_channel::unbounded); gates.1.clone() }; diff --git a/crates/bevy_asset/src/io/memory.rs b/crates/bevy_asset/src/io/memory.rs index 94f31928a75f4..52a1a6ffd6264 100644 --- a/crates/bevy_asset/src/io/memory.rs +++ b/crates/bevy_asset/src/io/memory.rs @@ -12,9 +12,9 @@ use std::{ #[derive(Default, Debug)] struct DirInternal { - assets: HashMap, - metadata: HashMap, - dirs: HashMap, + assets: HashMap, Data>, + metadata: HashMap, Data>, + dirs: HashMap, Dir>, path: PathBuf, } @@ -46,7 +46,11 @@ impl Dir { dir = self.get_or_insert_dir(parent); } dir.0.write().assets.insert( - path.file_name().unwrap().to_string_lossy().to_string(), + path.file_name() + .unwrap() + .to_string_lossy() + .into_owned() + .into_boxed_str(), Data { value: value.into(), path: path.to_owned(), @@ -60,7 +64,11 @@ impl Dir { dir = self.get_or_insert_dir(parent); } dir.0.write().metadata.insert( - path.file_name().unwrap().to_string_lossy().to_string(), + path.file_name() + .unwrap() + .to_string_lossy() + .into_owned() + .into_boxed_str(), Data { value: value.into(), path: path.to_owned(), @@ -73,7 +81,7 @@ impl Dir { let mut full_path = PathBuf::new(); for c in path.components() { full_path.push(c); - let name = c.as_os_str().to_string_lossy().to_string(); + let name = c.as_os_str().to_string_lossy().to_string().into_boxed_str(); dir = { let dirs = &mut dir.0.write().dirs; dirs.entry(name) @@ -147,7 +155,12 @@ impl Stream for DirStream { let dir = this.dir.0.read(); let dir_index = this.dir_index; - if let Some(dir_path) = dir.dirs.keys().nth(dir_index).map(|d| dir.path.join(d)) { + if let Some(dir_path) = dir + .dirs + .keys() + .nth(dir_index) + .map(|d| dir.path.join(d.as_ref())) + { this.dir_index += 1; Poll::Ready(Some(dir_path)) } else { diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 41d6ef834f0fc..5160e24cdc7eb 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -460,10 +460,7 @@ mod tests { use bevy_utils::{BoxedFuture, Duration, HashMap}; use futures_lite::AsyncReadExt; use serde::{Deserialize, Serialize}; - use std::{ - path::{Path, PathBuf}, - sync::Arc, - }; + use std::{path::Path, sync::Arc}; use thiserror::Error; #[derive(Asset, TypePath, Debug)] @@ -554,7 +551,7 @@ mod tests { /// A dummy [`CoolText`] asset reader that only succeeds after `failure_count` times it's read from for each asset. #[derive(Default, Clone)] pub struct UnstableMemoryAssetReader { - pub attempt_counters: Arc>>, + pub attempt_counters: Arc, usize>>>, pub load_delay: Duration, memory_reader: MemoryAssetReader, failure_count: usize, @@ -598,13 +595,12 @@ mod tests { Result>, bevy_asset::io::AssetReaderError>, > { let attempt_number = { - let key = PathBuf::from(path); let mut attempt_counters = self.attempt_counters.lock().unwrap(); - if let Some(existing) = attempt_counters.get_mut(&key) { + if let Some(existing) = attempt_counters.get_mut(path) { *existing += 1; *existing } else { - attempt_counters.insert(key, 1); + attempt_counters.insert(Box::from(path), 1); 1 } }; diff --git a/crates/bevy_asset/src/processor/mod.rs b/crates/bevy_asset/src/processor/mod.rs index b5ef275103ac3..025f91fe214df 100644 --- a/crates/bevy_asset/src/processor/mod.rs +++ b/crates/bevy_asset/src/processor/mod.rs @@ -56,7 +56,7 @@ pub struct AssetProcessorData { log: async_lock::RwLock>, processors: RwLock>>, /// Default processors for file extensions - default_processors: RwLock>, + default_processors: RwLock, &'static str>>, state: async_lock::RwLock, sources: AssetSources, initialized_sender: async_broadcast::Sender<()>, @@ -482,7 +482,7 @@ impl AssetProcessor { /// Set the default processor for the given `extension`. Make sure `P` is registered with [`AssetProcessor::register_processor`]. pub fn set_default_processor(&self, extension: &str) { let mut default_processors = self.data.default_processors.write(); - default_processors.insert(extension.to_string(), std::any::type_name::

()); + default_processors.insert(Box::from(extension), std::any::type_name::

()); } /// Returns the default processor for the given `extension`, if it exists. diff --git a/crates/bevy_asset/src/server/loaders.rs b/crates/bevy_asset/src/server/loaders.rs index 671064b31aa81..e6bb5f86d2cd8 100644 --- a/crates/bevy_asset/src/server/loaders.rs +++ b/crates/bevy_asset/src/server/loaders.rs @@ -13,7 +13,7 @@ use thiserror::Error; pub(crate) struct AssetLoaders { loaders: Vec, type_id_to_loaders: TypeIdMap>, - extension_to_loaders: HashMap>, + extension_to_loaders: HashMap, Vec>, type_name_to_loader: HashMap<&'static str, usize>, preregistered_loaders: HashMap<&'static str, usize>, } @@ -44,7 +44,7 @@ impl AssetLoaders { for extension in loader.extensions() { let list = self .extension_to_loaders - .entry(extension.to_string()) + .entry(Box::from(*extension)) .or_default(); if !list.is_empty() { @@ -105,7 +105,7 @@ impl AssetLoaders { for extension in extensions { let list = self .extension_to_loaders - .entry(extension.to_string()) + .entry(Box::from(*extension)) .or_default(); if !list.is_empty() { diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index f0266f6a676cf..7f192547c4474 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -806,7 +806,7 @@ pub struct Bundles { /// Cache static [`BundleId`] bundle_ids: TypeIdMap, /// Cache dynamic [`BundleId`] with multiple components - dynamic_bundle_ids: HashMap, (BundleId, Vec)>, + dynamic_bundle_ids: HashMap, (BundleId, Vec)>, /// Cache optimized dynamic [`BundleId`] with single component dynamic_component_bundle_ids: HashMap, } @@ -871,7 +871,7 @@ impl Bundles { .from_key(component_ids) .or_insert_with(|| { ( - Vec::from(component_ids), + Box::from(component_ids), initialize_dynamic_bundle(bundle_infos, components, Vec::from(component_ids)), ) }); diff --git a/crates/bevy_ecs/src/storage/table.rs b/crates/bevy_ecs/src/storage/table.rs index 5b05e43c29d2b..ca7aa5dcbd7ec 100644 --- a/crates/bevy_ecs/src/storage/table.rs +++ b/crates/bevy_ecs/src/storage/table.rs @@ -796,7 +796,7 @@ impl Table { /// Can be accessed via [`Storages`](crate::storage::Storages) pub struct Tables { tables: Vec, - table_ids: HashMap, TableId>, + table_ids: HashMap, TableId>, } impl Default for Tables { @@ -873,7 +873,7 @@ impl Tables { } tables.push(table.build()); ( - component_ids.to_vec(), + Box::from(component_ids), TableId::from_usize(tables.len() - 1), ) }); diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 5e8d8e3f1cb46..36b9ab512a046 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -26,7 +26,7 @@ use bevy_scene::Scene; /// Adds support for glTF file loading to the app. #[derive(Default)] pub struct GltfPlugin { - custom_vertex_attributes: HashMap, + custom_vertex_attributes: HashMap, MeshVertexAttribute>, } impl GltfPlugin { @@ -41,7 +41,7 @@ impl GltfPlugin { attribute: MeshVertexAttribute, ) -> Self { self.custom_vertex_attributes - .insert(name.to_string(), attribute); + .insert(Box::from(name), attribute); self } } @@ -75,19 +75,19 @@ pub struct Gltf { /// All scenes loaded from the glTF file. pub scenes: Vec>, /// Named scenes loaded from the glTF file. - pub named_scenes: HashMap>, + pub named_scenes: HashMap, Handle>, /// All meshes loaded from the glTF file. pub meshes: Vec>, /// Named meshes loaded from the glTF file. - pub named_meshes: HashMap>, + pub named_meshes: HashMap, Handle>, /// All materials loaded from the glTF file. pub materials: Vec>, /// Named materials loaded from the glTF file. - pub named_materials: HashMap>, + pub named_materials: HashMap, Handle>, /// All nodes loaded from the glTF file. pub nodes: Vec>, /// Named nodes loaded from the glTF file. - pub named_nodes: HashMap>, + pub named_nodes: HashMap, Handle>, /// Default scene to be displayed. pub default_scene: Option>, /// All animations loaded from the glTF file. @@ -95,7 +95,7 @@ pub struct Gltf { pub animations: Vec>, /// Named animations loaded from the glTF file. #[cfg(feature = "bevy_animation")] - pub named_animations: HashMap>, + pub named_animations: HashMap, Handle>, /// The gltf root of the gltf asset, see . Only has a value when `GltfLoaderSettings::include_source` is true. pub source: Option, } diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index b3306ef3d0b1d..442dec2550ef3 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -110,7 +110,7 @@ pub struct GltfLoader { /// Keys must be the attribute names as found in the glTF data, which must start with an underscore. /// See [this section of the glTF specification](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview) /// for additional details on custom attributes. - pub custom_vertex_attributes: HashMap, + pub custom_vertex_attributes: HashMap, MeshVertexAttribute>, } /// Specifies optional settings for processing gltfs at load time. By default, all recognized contents of @@ -293,7 +293,7 @@ async fn load_gltf<'a, 'b, 'c>( let handle = load_context .add_labeled_asset(format!("Animation{}", animation.index()), animation_clip); if let Some(name) = animation.name() { - named_animations.insert(name.to_string(), handle.clone()); + named_animations.insert(Box::from(name), handle.clone()); } animations.push(handle); } @@ -383,7 +383,7 @@ async fn load_gltf<'a, 'b, 'c>( for material in gltf.materials() { let handle = load_material(&material, load_context, false); if let Some(name) = material.name() { - named_materials.insert(name.to_string(), handle.clone()); + named_materials.insert(Box::from(name), handle.clone()); } materials.push(handle); } @@ -526,7 +526,7 @@ async fn load_gltf<'a, 'b, 'c>( }, ); if let Some(name) = gltf_mesh.name() { - named_meshes.insert(name.to_string(), handle.clone()); + named_meshes.insert(Box::from(name), handle.clone()); } meshes.push(handle); } @@ -563,7 +563,7 @@ async fn load_gltf<'a, 'b, 'c>( .filter_map(|(name, index)| { nodes .get(index) - .map(|handle| (name.to_string(), handle.clone())) + .map(|handle| (Box::from(name), handle.clone())) }) .collect(); @@ -661,7 +661,7 @@ async fn load_gltf<'a, 'b, 'c>( let scene_handle = load_context.add_loaded_labeled_asset(scene_label(&scene), loaded_scene); if let Some(name) = scene.name() { - named_scenes.insert(name.to_string(), scene_handle.clone()); + named_scenes.insert(Box::from(name), scene_handle.clone()); } scenes.push(scene_handle); } diff --git a/crates/bevy_gltf/src/vertex_attributes.rs b/crates/bevy_gltf/src/vertex_attributes.rs index 49f99241875bf..347b4e8ab5370 100644 --- a/crates/bevy_gltf/src/vertex_attributes.rs +++ b/crates/bevy_gltf/src/vertex_attributes.rs @@ -255,7 +255,7 @@ pub(crate) fn convert_attribute( semantic: gltf::Semantic, accessor: gltf::Accessor, buffer_data: &Vec>, - custom_vertex_attributes: &HashMap, + custom_vertex_attributes: &HashMap, MeshVertexAttribute>, ) -> Result<(MeshVertexAttribute, Values), ConvertAttributeError> { if let Some((attribute, conversion)) = match &semantic { gltf::Semantic::Positions => Some((Mesh::ATTRIBUTE_POSITION, ConversionMode::Any)), @@ -271,7 +271,7 @@ pub(crate) fn convert_attribute( Some((Mesh::ATTRIBUTE_JOINT_WEIGHT, ConversionMode::JointWeight)) } gltf::Semantic::Extras(name) => custom_vertex_attributes - .get(name) + .get(name.as_str()) .map(|attr| (attr.clone(), ConversionMode::Any)), _ => None, } { diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index ab921024129a5..f3812b4f1d14f 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -125,7 +125,7 @@ impl CachedPipelineState { #[derive(Default)] struct ShaderData { pipelines: HashSet, - processed_shaders: HashMap, ErasedShaderModule>, + processed_shaders: HashMap, ErasedShaderModule>, resolved_imports: HashMap>, dependents: HashSet>, } @@ -277,7 +277,7 @@ impl ShaderCache { data.pipelines.insert(pipeline); // PERF: this shader_defs clone isn't great. use raw_entry_mut when it stabilizes - let module = match data.processed_shaders.entry(shader_defs.to_vec()) { + let module = match data.processed_shaders.entry(Box::from(shader_defs)) { Entry::Occupied(entry) => entry.into_mut(), Entry::Vacant(entry) => { let mut shader_defs = shader_defs.to_vec(); diff --git a/tools/build-templated-pages/src/examples.rs b/tools/build-templated-pages/src/examples.rs index b1cdc3ad32dd3..c50f3e03ac2d9 100644 --- a/tools/build-templated-pages/src/examples.rs +++ b/tools/build-templated-pages/src/examples.rs @@ -80,7 +80,7 @@ fn parse_examples(panic_on_missing: bool) -> Vec { .collect() } -fn parse_categories() -> HashMap { +fn parse_categories() -> HashMap, String> { let manifest_file = std::fs::read_to_string("Cargo.toml").unwrap(); let manifest = manifest_file.parse::().unwrap(); manifest @@ -95,7 +95,7 @@ fn parse_categories() -> HashMap { .iter() .map(|v| { ( - v.get("name").unwrap().as_str().unwrap().to_string(), + Box::from(v.get("name").unwrap().as_str().unwrap()), v.get("description").unwrap().as_str().unwrap().to_string(), ) }) @@ -107,7 +107,7 @@ pub(crate) fn check(what_to_run: Command) { if what_to_run.contains(Command::UPDATE) { let categories = parse_categories(); - let examples_by_category: HashMap = examples + let examples_by_category: HashMap, Category> = examples .into_iter() .fold(HashMap::>::new(), |mut v, ex| { v.entry(ex.category.clone()).or_default().push(ex); @@ -116,6 +116,7 @@ pub(crate) fn check(what_to_run: Command) { .into_iter() .map(|(key, mut examples)| { examples.sort(); + let key = key.into_boxed_str(); let description = categories.get(&key).cloned(); ( key, From 71c7a78af086f2c99acbbcb01dce4b97796239fb Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 12:09:32 +0100 Subject: [PATCH 02/19] change HashSet for immutable values --- crates/bevy_app/src/app.rs | 4 ++-- crates/bevy_asset/src/server/info.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 54a324a85f0b2..357f17852097d 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -79,7 +79,7 @@ pub struct App { pub main_schedule_label: InternedScheduleLabel, sub_apps: HashMap, plugin_registry: Vec>, - plugin_name_added: HashSet, + plugin_name_added: HashSet>, /// A private counter to prevent incorrect calls to `App::run()` from `Plugin::build()` building_plugin_depth: usize, plugins_state: PluginsState, @@ -643,7 +643,7 @@ impl App { plugin: Box, ) -> Result<&mut Self, AppError> { debug!("added plugin: {}", plugin.name()); - if plugin.is_unique() && !self.plugin_name_added.insert(plugin.name().to_string()) { + if plugin.is_unique() && !self.plugin_name_added.insert(Box::from(plugin.name())) { Err(AppError::DuplicatePlugin { plugin_name: plugin.name().to_string(), })?; diff --git a/crates/bevy_asset/src/server/info.rs b/crates/bevy_asset/src/server/info.rs index 22c03efbd0760..aeb2f3ba49bf8 100644 --- a/crates/bevy_asset/src/server/info.rs +++ b/crates/bevy_asset/src/server/info.rs @@ -71,7 +71,7 @@ pub(crate) struct AssetInfos { pub(crate) loader_dependants: HashMap, HashSet>>, /// Tracks living labeled assets for a given source asset. /// This should only be set when watching for changes to avoid unnecessary work. - pub(crate) living_labeled_assets: HashMap, HashSet>, + pub(crate) living_labeled_assets: HashMap, HashSet>>, pub(crate) handle_providers: TypeIdMap, pub(crate) dependency_loaded_event_sender: TypeIdMap, pub(crate) dependency_failed_event_sender: @@ -113,7 +113,7 @@ impl AssetInfos { fn create_handle_internal( infos: &mut HashMap, handle_providers: &TypeIdMap, - living_labeled_assets: &mut HashMap, HashSet>, + living_labeled_assets: &mut HashMap, HashSet>>, watching_for_changes: bool, type_id: TypeId, path: Option>, @@ -129,7 +129,7 @@ impl AssetInfos { let mut without_label = path.to_owned(); if let Some(label) = without_label.take_label() { let labels = living_labeled_assets.entry(without_label).or_default(); - labels.insert(label.to_string()); + labels.insert(Box::from(label.as_ref())); } } } @@ -613,7 +613,7 @@ impl AssetInfos { info: &AssetInfo, loader_dependants: &mut HashMap, HashSet>>, path: &AssetPath<'static>, - living_labeled_assets: &mut HashMap, HashSet>, + living_labeled_assets: &mut HashMap, HashSet>>, ) { for loader_dependency in info.loader_dependencies.keys() { if let Some(dependants) = loader_dependants.get_mut(loader_dependency) { @@ -642,7 +642,7 @@ impl AssetInfos { infos: &mut HashMap, path_to_id: &mut HashMap, TypeIdMap>, loader_dependants: &mut HashMap, HashSet>>, - living_labeled_assets: &mut HashMap, HashSet>, + living_labeled_assets: &mut HashMap, HashSet>>, watching_for_changes: bool, id: UntypedAssetId, ) -> bool { From 5dd67f183f576d72197db60f87de0c0418323ba4 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 15:08:26 +0100 Subject: [PATCH 03/19] use `into` over `Box::from` --- crates/bevy_app/src/app.rs | 2 +- crates/bevy_asset/src/io/embedded/mod.rs | 4 ++-- crates/bevy_asset/src/io/gated.rs | 4 ++-- crates/bevy_asset/src/lib.rs | 2 +- crates/bevy_asset/src/processor/mod.rs | 2 +- crates/bevy_asset/src/server/info.rs | 2 +- crates/bevy_asset/src/server/loaders.rs | 4 ++-- crates/bevy_ecs/src/bundle.rs | 2 +- crates/bevy_ecs/src/storage/table.rs | 5 +---- crates/bevy_gltf/src/lib.rs | 2 +- crates/bevy_gltf/src/loader.rs | 14 +++++--------- .../src/render_resource/pipeline_cache.rs | 2 +- tools/build-templated-pages/src/examples.rs | 2 +- 13 files changed, 20 insertions(+), 27 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 357f17852097d..f32017aa0b852 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -643,7 +643,7 @@ impl App { plugin: Box, ) -> Result<&mut Self, AppError> { debug!("added plugin: {}", plugin.name()); - if plugin.is_unique() && !self.plugin_name_added.insert(Box::from(plugin.name())) { + if plugin.is_unique() && !self.plugin_name_added.insert(plugin.name().into()) { Err(AppError::DuplicatePlugin { plugin_name: plugin.name().to_string(), })?; diff --git a/crates/bevy_asset/src/io/embedded/mod.rs b/crates/bevy_asset/src/io/embedded/mod.rs index 9af5e0ec55d18..b953e1e669f08 100644 --- a/crates/bevy_asset/src/io/embedded/mod.rs +++ b/crates/bevy_asset/src/io/embedded/mod.rs @@ -35,7 +35,7 @@ impl EmbeddedAssetRegistry { #[cfg(feature = "embedded_watcher")] self.root_paths .write() - .insert(full_path.into_boxed_path(), asset_path.to_owned()); + .insert(full_path.into(), asset_path.to_owned()); self.dir.insert_asset(asset_path, value); } @@ -48,7 +48,7 @@ impl EmbeddedAssetRegistry { #[cfg(feature = "embedded_watcher")] self.root_paths .write() - .insert(Box::from(full_path), asset_path.to_owned()); + .insert(full_path.into(), asset_path.to_owned()); self.dir.insert_meta(asset_path, value); } diff --git a/crates/bevy_asset/src/io/gated.rs b/crates/bevy_asset/src/io/gated.rs index 4db9d755d3487..1250dabf75f4b 100644 --- a/crates/bevy_asset/src/io/gated.rs +++ b/crates/bevy_asset/src/io/gated.rs @@ -33,7 +33,7 @@ impl GateOpener { pub fn open>(&self, path: P) { let mut gates = self.gates.write(); let gates = gates - .entry(Box::from(path.as_ref())) + .entry(path.as_ref().into()) .or_insert_with(crossbeam_channel::unbounded); gates.0.send(()).unwrap(); } @@ -62,7 +62,7 @@ impl AssetReader for GatedReader { let receiver = { let mut gates = self.gates.write(); let gates = gates - .entry(Box::from(path)) + .entry(path.into()) .or_insert_with(crossbeam_channel::unbounded); gates.1.clone() }; diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 5160e24cdc7eb..4f560f7cb6cbe 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -600,7 +600,7 @@ mod tests { *existing += 1; *existing } else { - attempt_counters.insert(Box::from(path), 1); + attempt_counters.insert(path.into(), 1); 1 } }; diff --git a/crates/bevy_asset/src/processor/mod.rs b/crates/bevy_asset/src/processor/mod.rs index 025f91fe214df..625a484330078 100644 --- a/crates/bevy_asset/src/processor/mod.rs +++ b/crates/bevy_asset/src/processor/mod.rs @@ -482,7 +482,7 @@ impl AssetProcessor { /// Set the default processor for the given `extension`. Make sure `P` is registered with [`AssetProcessor::register_processor`]. pub fn set_default_processor(&self, extension: &str) { let mut default_processors = self.data.default_processors.write(); - default_processors.insert(Box::from(extension), std::any::type_name::

()); + default_processors.insert(extension.into(), std::any::type_name::

()); } /// Returns the default processor for the given `extension`, if it exists. diff --git a/crates/bevy_asset/src/server/info.rs b/crates/bevy_asset/src/server/info.rs index aeb2f3ba49bf8..080780bcd9045 100644 --- a/crates/bevy_asset/src/server/info.rs +++ b/crates/bevy_asset/src/server/info.rs @@ -129,7 +129,7 @@ impl AssetInfos { let mut without_label = path.to_owned(); if let Some(label) = without_label.take_label() { let labels = living_labeled_assets.entry(without_label).or_default(); - labels.insert(Box::from(label.as_ref())); + labels.insert(label.as_ref().into()); } } } diff --git a/crates/bevy_asset/src/server/loaders.rs b/crates/bevy_asset/src/server/loaders.rs index e6bb5f86d2cd8..65f21d6b9b52f 100644 --- a/crates/bevy_asset/src/server/loaders.rs +++ b/crates/bevy_asset/src/server/loaders.rs @@ -44,7 +44,7 @@ impl AssetLoaders { for extension in loader.extensions() { let list = self .extension_to_loaders - .entry(Box::from(*extension)) + .entry((*extension).into()) .or_default(); if !list.is_empty() { @@ -105,7 +105,7 @@ impl AssetLoaders { for extension in extensions { let list = self .extension_to_loaders - .entry(Box::from(*extension)) + .entry((*extension).into()) .or_default(); if !list.is_empty() { diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index 7f192547c4474..81f025d1024a5 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -871,7 +871,7 @@ impl Bundles { .from_key(component_ids) .or_insert_with(|| { ( - Box::from(component_ids), + component_ids.into(), initialize_dynamic_bundle(bundle_infos, components, Vec::from(component_ids)), ) }); diff --git a/crates/bevy_ecs/src/storage/table.rs b/crates/bevy_ecs/src/storage/table.rs index ca7aa5dcbd7ec..8d6b03d1da189 100644 --- a/crates/bevy_ecs/src/storage/table.rs +++ b/crates/bevy_ecs/src/storage/table.rs @@ -872,10 +872,7 @@ impl Tables { table = table.add_column(components.get_info_unchecked(*component_id)); } tables.push(table.build()); - ( - Box::from(component_ids), - TableId::from_usize(tables.len() - 1), - ) + (component_ids.into(), TableId::from_usize(tables.len() - 1)) }); *value diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 36b9ab512a046..6dabb789c8858 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -41,7 +41,7 @@ impl GltfPlugin { attribute: MeshVertexAttribute, ) -> Self { self.custom_vertex_attributes - .insert(Box::from(name), attribute); + .insert(name.into(), attribute); self } } diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 442dec2550ef3..cd623b23f36df 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -293,7 +293,7 @@ async fn load_gltf<'a, 'b, 'c>( let handle = load_context .add_labeled_asset(format!("Animation{}", animation.index()), animation_clip); if let Some(name) = animation.name() { - named_animations.insert(Box::from(name), handle.clone()); + named_animations.insert(name.into(), handle.clone()); } animations.push(handle); } @@ -383,7 +383,7 @@ async fn load_gltf<'a, 'b, 'c>( for material in gltf.materials() { let handle = load_material(&material, load_context, false); if let Some(name) = material.name() { - named_materials.insert(Box::from(name), handle.clone()); + named_materials.insert(name.into(), handle.clone()); } materials.push(handle); } @@ -526,7 +526,7 @@ async fn load_gltf<'a, 'b, 'c>( }, ); if let Some(name) = gltf_mesh.name() { - named_meshes.insert(Box::from(name), handle.clone()); + named_meshes.insert(name.into(), handle.clone()); } meshes.push(handle); } @@ -560,11 +560,7 @@ async fn load_gltf<'a, 'b, 'c>( .collect::>>(); let named_nodes = named_nodes_intermediate .into_iter() - .filter_map(|(name, index)| { - nodes - .get(index) - .map(|handle| (Box::from(name), handle.clone())) - }) + .filter_map(|(name, index)| nodes.get(index).map(|handle| (name.into(), handle.clone()))) .collect(); let skinned_mesh_inverse_bindposes: Vec<_> = gltf @@ -661,7 +657,7 @@ async fn load_gltf<'a, 'b, 'c>( let scene_handle = load_context.add_loaded_labeled_asset(scene_label(&scene), loaded_scene); if let Some(name) = scene.name() { - named_scenes.insert(Box::from(name), scene_handle.clone()); + named_scenes.insert(name.into(), scene_handle.clone()); } scenes.push(scene_handle); } diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index f3812b4f1d14f..0bd1c1d09ef6b 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -277,7 +277,7 @@ impl ShaderCache { data.pipelines.insert(pipeline); // PERF: this shader_defs clone isn't great. use raw_entry_mut when it stabilizes - let module = match data.processed_shaders.entry(Box::from(shader_defs)) { + let module = match data.processed_shaders.entry(shader_defs.into()) { Entry::Occupied(entry) => entry.into_mut(), Entry::Vacant(entry) => { let mut shader_defs = shader_defs.to_vec(); diff --git a/tools/build-templated-pages/src/examples.rs b/tools/build-templated-pages/src/examples.rs index c50f3e03ac2d9..63370fbee2dcc 100644 --- a/tools/build-templated-pages/src/examples.rs +++ b/tools/build-templated-pages/src/examples.rs @@ -95,7 +95,7 @@ fn parse_categories() -> HashMap, String> { .iter() .map(|v| { ( - Box::from(v.get("name").unwrap().as_str().unwrap()), + v.get("name").unwrap().as_str().unwrap().into(), v.get("description").unwrap().as_str().unwrap().to_string(), ) }) From 18bce4168bd47d784a24c19c0307cee58e1eef1b Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 15:08:47 +0100 Subject: [PATCH 04/19] avoid double-allocation --- crates/bevy_asset/src/io/memory.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/crates/bevy_asset/src/io/memory.rs b/crates/bevy_asset/src/io/memory.rs index 52a1a6ffd6264..cc13d0482056a 100644 --- a/crates/bevy_asset/src/io/memory.rs +++ b/crates/bevy_asset/src/io/memory.rs @@ -46,11 +46,7 @@ impl Dir { dir = self.get_or_insert_dir(parent); } dir.0.write().assets.insert( - path.file_name() - .unwrap() - .to_string_lossy() - .into_owned() - .into_boxed_str(), + path.file_name().unwrap().to_string_lossy().into(), Data { value: value.into(), path: path.to_owned(), @@ -64,11 +60,7 @@ impl Dir { dir = self.get_or_insert_dir(parent); } dir.0.write().metadata.insert( - path.file_name() - .unwrap() - .to_string_lossy() - .into_owned() - .into_boxed_str(), + path.file_name().unwrap().to_string_lossy().into(), Data { value: value.into(), path: path.to_owned(), @@ -81,7 +73,7 @@ impl Dir { let mut full_path = PathBuf::new(); for c in path.components() { full_path.push(c); - let name = c.as_os_str().to_string_lossy().to_string().into_boxed_str(); + let name = c.as_os_str().to_string_lossy().into(); dir = { let dirs = &mut dir.0.write().dirs; dirs.entry(name) From d00220b73461028e6a1ad605c8d8cb17fd2bbf19 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 15:14:29 +0100 Subject: [PATCH 05/19] convert last `HashMap` --- tools/build-templated-pages/src/examples.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/build-templated-pages/src/examples.rs b/tools/build-templated-pages/src/examples.rs index 63370fbee2dcc..873765a6f324e 100644 --- a/tools/build-templated-pages/src/examples.rs +++ b/tools/build-templated-pages/src/examples.rs @@ -109,14 +109,13 @@ pub(crate) fn check(what_to_run: Command) { let categories = parse_categories(); let examples_by_category: HashMap, Category> = examples .into_iter() - .fold(HashMap::>::new(), |mut v, ex| { - v.entry(ex.category.clone()).or_default().push(ex); + .fold(HashMap::, Vec>::new(), |mut v, ex| { + v.entry(ex.category.as_str().into()).or_default().push(ex); v }) .into_iter() .map(|(key, mut examples)| { examples.sort(); - let key = key.into_boxed_str(); let description = categories.get(&key).cloned(); ( key, From 674fee438a82ab9f1e74123ebb4cf74b5ea0528f Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 15:21:53 +0100 Subject: [PATCH 06/19] cargo fmt --- crates/bevy_gltf/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 6dabb789c8858..9cd59d674a601 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -40,8 +40,7 @@ impl GltfPlugin { name: &str, attribute: MeshVertexAttribute, ) -> Self { - self.custom_vertex_attributes - .insert(name.into(), attribute); + self.custom_vertex_attributes.insert(name.into(), attribute); self } } From c6dda2f147f5fe61091103e8f8c9fb212fe981fd Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 12:01:21 +0100 Subject: [PATCH 07/19] set immutable key for HashMaps --- .../src/io/embedded/embedded_watcher.rs | 6 ++--- crates/bevy_asset/src/io/embedded/mod.rs | 6 ++--- crates/bevy_asset/src/io/gated.rs | 13 ++++----- crates/bevy_asset/src/io/memory.rs | 27 ++++++++++++++----- crates/bevy_asset/src/lib.rs | 12 +++------ crates/bevy_asset/src/processor/mod.rs | 4 +-- crates/bevy_asset/src/server/loaders.rs | 6 ++--- crates/bevy_ecs/src/bundle.rs | 4 +-- crates/bevy_ecs/src/storage/table.rs | 4 +-- crates/bevy_gltf/src/lib.rs | 14 +++++----- crates/bevy_gltf/src/loader.rs | 12 ++++----- crates/bevy_gltf/src/vertex_attributes.rs | 4 +-- .../src/render_resource/pipeline_cache.rs | 4 +-- tools/build-templated-pages/src/examples.rs | 7 ++--- 14 files changed, 65 insertions(+), 58 deletions(-) diff --git a/crates/bevy_asset/src/io/embedded/embedded_watcher.rs b/crates/bevy_asset/src/io/embedded/embedded_watcher.rs index 6e92caa5d3bb3..1e7f3057bbfd2 100644 --- a/crates/bevy_asset/src/io/embedded/embedded_watcher.rs +++ b/crates/bevy_asset/src/io/embedded/embedded_watcher.rs @@ -25,7 +25,7 @@ pub struct EmbeddedWatcher { impl EmbeddedWatcher { pub fn new( dir: Dir, - root_paths: Arc>>, + root_paths: Arc, PathBuf>>>, sender: crossbeam_channel::Sender, debounce_wait_time: Duration, ) -> Self { @@ -49,7 +49,7 @@ impl AssetWatcher for EmbeddedWatcher {} /// the initial static bytes from the file embedded in the binary. pub(crate) struct EmbeddedEventHandler { sender: crossbeam_channel::Sender, - root_paths: Arc>>, + root_paths: Arc, PathBuf>>>, root: PathBuf, dir: Dir, last_event: Option, @@ -61,7 +61,7 @@ impl FilesystemEventHandler for EmbeddedEventHandler { fn get_path(&self, absolute_path: &Path) -> Option<(PathBuf, bool)> { let (local_path, is_meta) = get_asset_path(&self.root, absolute_path); - let final_path = self.root_paths.read().get(&local_path)?.clone(); + let final_path = self.root_paths.read().get(local_path.as_path())?.clone(); if is_meta { warn!("Meta file asset hot-reloading is not supported yet: {final_path:?}"); } diff --git a/crates/bevy_asset/src/io/embedded/mod.rs b/crates/bevy_asset/src/io/embedded/mod.rs index bf22602436470..9af5e0ec55d18 100644 --- a/crates/bevy_asset/src/io/embedded/mod.rs +++ b/crates/bevy_asset/src/io/embedded/mod.rs @@ -22,7 +22,7 @@ pub const EMBEDDED: &str = "embedded"; pub struct EmbeddedAssetRegistry { dir: Dir, #[cfg(feature = "embedded_watcher")] - root_paths: std::sync::Arc>>, + root_paths: std::sync::Arc, PathBuf>>>, } impl EmbeddedAssetRegistry { @@ -35,7 +35,7 @@ impl EmbeddedAssetRegistry { #[cfg(feature = "embedded_watcher")] self.root_paths .write() - .insert(full_path.to_owned(), asset_path.to_owned()); + .insert(full_path.into_boxed_path(), asset_path.to_owned()); self.dir.insert_asset(asset_path, value); } @@ -48,7 +48,7 @@ impl EmbeddedAssetRegistry { #[cfg(feature = "embedded_watcher")] self.root_paths .write() - .insert(full_path.to_owned(), asset_path.to_owned()); + .insert(Box::from(full_path), asset_path.to_owned()); self.dir.insert_meta(asset_path, value); } diff --git a/crates/bevy_asset/src/io/gated.rs b/crates/bevy_asset/src/io/gated.rs index 8a049263dab70..4db9d755d3487 100644 --- a/crates/bevy_asset/src/io/gated.rs +++ b/crates/bevy_asset/src/io/gated.rs @@ -2,10 +2,7 @@ use crate::io::{AssetReader, AssetReaderError, PathStream, Reader}; use bevy_utils::{BoxedFuture, HashMap}; use crossbeam_channel::{Receiver, Sender}; use parking_lot::RwLock; -use std::{ - path::{Path, PathBuf}, - sync::Arc, -}; +use std::{path::Path, sync::Arc}; /// A "gated" reader that will prevent asset reads from returning until /// a given path has been "opened" using [`GateOpener`]. @@ -13,7 +10,7 @@ use std::{ /// This is built primarily for unit tests. pub struct GatedReader { reader: R, - gates: Arc, Receiver<()>)>>>, + gates: Arc, (Sender<()>, Receiver<()>)>>>, } impl Clone for GatedReader { @@ -27,7 +24,7 @@ impl Clone for GatedReader { /// Opens path "gates" for a [`GatedReader`]. pub struct GateOpener { - gates: Arc, Receiver<()>)>>>, + gates: Arc, (Sender<()>, Receiver<()>)>>>, } impl GateOpener { @@ -36,7 +33,7 @@ impl GateOpener { pub fn open>(&self, path: P) { let mut gates = self.gates.write(); let gates = gates - .entry(path.as_ref().to_path_buf()) + .entry(Box::from(path.as_ref())) .or_insert_with(crossbeam_channel::unbounded); gates.0.send(()).unwrap(); } @@ -65,7 +62,7 @@ impl AssetReader for GatedReader { let receiver = { let mut gates = self.gates.write(); let gates = gates - .entry(path.to_path_buf()) + .entry(Box::from(path)) .or_insert_with(crossbeam_channel::unbounded); gates.1.clone() }; diff --git a/crates/bevy_asset/src/io/memory.rs b/crates/bevy_asset/src/io/memory.rs index 94f31928a75f4..52a1a6ffd6264 100644 --- a/crates/bevy_asset/src/io/memory.rs +++ b/crates/bevy_asset/src/io/memory.rs @@ -12,9 +12,9 @@ use std::{ #[derive(Default, Debug)] struct DirInternal { - assets: HashMap, - metadata: HashMap, - dirs: HashMap, + assets: HashMap, Data>, + metadata: HashMap, Data>, + dirs: HashMap, Dir>, path: PathBuf, } @@ -46,7 +46,11 @@ impl Dir { dir = self.get_or_insert_dir(parent); } dir.0.write().assets.insert( - path.file_name().unwrap().to_string_lossy().to_string(), + path.file_name() + .unwrap() + .to_string_lossy() + .into_owned() + .into_boxed_str(), Data { value: value.into(), path: path.to_owned(), @@ -60,7 +64,11 @@ impl Dir { dir = self.get_or_insert_dir(parent); } dir.0.write().metadata.insert( - path.file_name().unwrap().to_string_lossy().to_string(), + path.file_name() + .unwrap() + .to_string_lossy() + .into_owned() + .into_boxed_str(), Data { value: value.into(), path: path.to_owned(), @@ -73,7 +81,7 @@ impl Dir { let mut full_path = PathBuf::new(); for c in path.components() { full_path.push(c); - let name = c.as_os_str().to_string_lossy().to_string(); + let name = c.as_os_str().to_string_lossy().to_string().into_boxed_str(); dir = { let dirs = &mut dir.0.write().dirs; dirs.entry(name) @@ -147,7 +155,12 @@ impl Stream for DirStream { let dir = this.dir.0.read(); let dir_index = this.dir_index; - if let Some(dir_path) = dir.dirs.keys().nth(dir_index).map(|d| dir.path.join(d)) { + if let Some(dir_path) = dir + .dirs + .keys() + .nth(dir_index) + .map(|d| dir.path.join(d.as_ref())) + { this.dir_index += 1; Poll::Ready(Some(dir_path)) } else { diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 41d6ef834f0fc..5160e24cdc7eb 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -460,10 +460,7 @@ mod tests { use bevy_utils::{BoxedFuture, Duration, HashMap}; use futures_lite::AsyncReadExt; use serde::{Deserialize, Serialize}; - use std::{ - path::{Path, PathBuf}, - sync::Arc, - }; + use std::{path::Path, sync::Arc}; use thiserror::Error; #[derive(Asset, TypePath, Debug)] @@ -554,7 +551,7 @@ mod tests { /// A dummy [`CoolText`] asset reader that only succeeds after `failure_count` times it's read from for each asset. #[derive(Default, Clone)] pub struct UnstableMemoryAssetReader { - pub attempt_counters: Arc>>, + pub attempt_counters: Arc, usize>>>, pub load_delay: Duration, memory_reader: MemoryAssetReader, failure_count: usize, @@ -598,13 +595,12 @@ mod tests { Result>, bevy_asset::io::AssetReaderError>, > { let attempt_number = { - let key = PathBuf::from(path); let mut attempt_counters = self.attempt_counters.lock().unwrap(); - if let Some(existing) = attempt_counters.get_mut(&key) { + if let Some(existing) = attempt_counters.get_mut(path) { *existing += 1; *existing } else { - attempt_counters.insert(key, 1); + attempt_counters.insert(Box::from(path), 1); 1 } }; diff --git a/crates/bevy_asset/src/processor/mod.rs b/crates/bevy_asset/src/processor/mod.rs index b5ef275103ac3..025f91fe214df 100644 --- a/crates/bevy_asset/src/processor/mod.rs +++ b/crates/bevy_asset/src/processor/mod.rs @@ -56,7 +56,7 @@ pub struct AssetProcessorData { log: async_lock::RwLock>, processors: RwLock>>, /// Default processors for file extensions - default_processors: RwLock>, + default_processors: RwLock, &'static str>>, state: async_lock::RwLock, sources: AssetSources, initialized_sender: async_broadcast::Sender<()>, @@ -482,7 +482,7 @@ impl AssetProcessor { /// Set the default processor for the given `extension`. Make sure `P` is registered with [`AssetProcessor::register_processor`]. pub fn set_default_processor(&self, extension: &str) { let mut default_processors = self.data.default_processors.write(); - default_processors.insert(extension.to_string(), std::any::type_name::

()); + default_processors.insert(Box::from(extension), std::any::type_name::

()); } /// Returns the default processor for the given `extension`, if it exists. diff --git a/crates/bevy_asset/src/server/loaders.rs b/crates/bevy_asset/src/server/loaders.rs index 671064b31aa81..e6bb5f86d2cd8 100644 --- a/crates/bevy_asset/src/server/loaders.rs +++ b/crates/bevy_asset/src/server/loaders.rs @@ -13,7 +13,7 @@ use thiserror::Error; pub(crate) struct AssetLoaders { loaders: Vec, type_id_to_loaders: TypeIdMap>, - extension_to_loaders: HashMap>, + extension_to_loaders: HashMap, Vec>, type_name_to_loader: HashMap<&'static str, usize>, preregistered_loaders: HashMap<&'static str, usize>, } @@ -44,7 +44,7 @@ impl AssetLoaders { for extension in loader.extensions() { let list = self .extension_to_loaders - .entry(extension.to_string()) + .entry(Box::from(*extension)) .or_default(); if !list.is_empty() { @@ -105,7 +105,7 @@ impl AssetLoaders { for extension in extensions { let list = self .extension_to_loaders - .entry(extension.to_string()) + .entry(Box::from(*extension)) .or_default(); if !list.is_empty() { diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index f0266f6a676cf..7f192547c4474 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -806,7 +806,7 @@ pub struct Bundles { /// Cache static [`BundleId`] bundle_ids: TypeIdMap, /// Cache dynamic [`BundleId`] with multiple components - dynamic_bundle_ids: HashMap, (BundleId, Vec)>, + dynamic_bundle_ids: HashMap, (BundleId, Vec)>, /// Cache optimized dynamic [`BundleId`] with single component dynamic_component_bundle_ids: HashMap, } @@ -871,7 +871,7 @@ impl Bundles { .from_key(component_ids) .or_insert_with(|| { ( - Vec::from(component_ids), + Box::from(component_ids), initialize_dynamic_bundle(bundle_infos, components, Vec::from(component_ids)), ) }); diff --git a/crates/bevy_ecs/src/storage/table.rs b/crates/bevy_ecs/src/storage/table.rs index 5b05e43c29d2b..ca7aa5dcbd7ec 100644 --- a/crates/bevy_ecs/src/storage/table.rs +++ b/crates/bevy_ecs/src/storage/table.rs @@ -796,7 +796,7 @@ impl Table { /// Can be accessed via [`Storages`](crate::storage::Storages) pub struct Tables { tables: Vec

, - table_ids: HashMap, TableId>, + table_ids: HashMap, TableId>, } impl Default for Tables { @@ -873,7 +873,7 @@ impl Tables { } tables.push(table.build()); ( - component_ids.to_vec(), + Box::from(component_ids), TableId::from_usize(tables.len() - 1), ) }); diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 5e8d8e3f1cb46..36b9ab512a046 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -26,7 +26,7 @@ use bevy_scene::Scene; /// Adds support for glTF file loading to the app. #[derive(Default)] pub struct GltfPlugin { - custom_vertex_attributes: HashMap, + custom_vertex_attributes: HashMap, MeshVertexAttribute>, } impl GltfPlugin { @@ -41,7 +41,7 @@ impl GltfPlugin { attribute: MeshVertexAttribute, ) -> Self { self.custom_vertex_attributes - .insert(name.to_string(), attribute); + .insert(Box::from(name), attribute); self } } @@ -75,19 +75,19 @@ pub struct Gltf { /// All scenes loaded from the glTF file. pub scenes: Vec>, /// Named scenes loaded from the glTF file. - pub named_scenes: HashMap>, + pub named_scenes: HashMap, Handle>, /// All meshes loaded from the glTF file. pub meshes: Vec>, /// Named meshes loaded from the glTF file. - pub named_meshes: HashMap>, + pub named_meshes: HashMap, Handle>, /// All materials loaded from the glTF file. pub materials: Vec>, /// Named materials loaded from the glTF file. - pub named_materials: HashMap>, + pub named_materials: HashMap, Handle>, /// All nodes loaded from the glTF file. pub nodes: Vec>, /// Named nodes loaded from the glTF file. - pub named_nodes: HashMap>, + pub named_nodes: HashMap, Handle>, /// Default scene to be displayed. pub default_scene: Option>, /// All animations loaded from the glTF file. @@ -95,7 +95,7 @@ pub struct Gltf { pub animations: Vec>, /// Named animations loaded from the glTF file. #[cfg(feature = "bevy_animation")] - pub named_animations: HashMap>, + pub named_animations: HashMap, Handle>, /// The gltf root of the gltf asset, see . Only has a value when `GltfLoaderSettings::include_source` is true. pub source: Option, } diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index b3306ef3d0b1d..442dec2550ef3 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -110,7 +110,7 @@ pub struct GltfLoader { /// Keys must be the attribute names as found in the glTF data, which must start with an underscore. /// See [this section of the glTF specification](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview) /// for additional details on custom attributes. - pub custom_vertex_attributes: HashMap, + pub custom_vertex_attributes: HashMap, MeshVertexAttribute>, } /// Specifies optional settings for processing gltfs at load time. By default, all recognized contents of @@ -293,7 +293,7 @@ async fn load_gltf<'a, 'b, 'c>( let handle = load_context .add_labeled_asset(format!("Animation{}", animation.index()), animation_clip); if let Some(name) = animation.name() { - named_animations.insert(name.to_string(), handle.clone()); + named_animations.insert(Box::from(name), handle.clone()); } animations.push(handle); } @@ -383,7 +383,7 @@ async fn load_gltf<'a, 'b, 'c>( for material in gltf.materials() { let handle = load_material(&material, load_context, false); if let Some(name) = material.name() { - named_materials.insert(name.to_string(), handle.clone()); + named_materials.insert(Box::from(name), handle.clone()); } materials.push(handle); } @@ -526,7 +526,7 @@ async fn load_gltf<'a, 'b, 'c>( }, ); if let Some(name) = gltf_mesh.name() { - named_meshes.insert(name.to_string(), handle.clone()); + named_meshes.insert(Box::from(name), handle.clone()); } meshes.push(handle); } @@ -563,7 +563,7 @@ async fn load_gltf<'a, 'b, 'c>( .filter_map(|(name, index)| { nodes .get(index) - .map(|handle| (name.to_string(), handle.clone())) + .map(|handle| (Box::from(name), handle.clone())) }) .collect(); @@ -661,7 +661,7 @@ async fn load_gltf<'a, 'b, 'c>( let scene_handle = load_context.add_loaded_labeled_asset(scene_label(&scene), loaded_scene); if let Some(name) = scene.name() { - named_scenes.insert(name.to_string(), scene_handle.clone()); + named_scenes.insert(Box::from(name), scene_handle.clone()); } scenes.push(scene_handle); } diff --git a/crates/bevy_gltf/src/vertex_attributes.rs b/crates/bevy_gltf/src/vertex_attributes.rs index 49f99241875bf..347b4e8ab5370 100644 --- a/crates/bevy_gltf/src/vertex_attributes.rs +++ b/crates/bevy_gltf/src/vertex_attributes.rs @@ -255,7 +255,7 @@ pub(crate) fn convert_attribute( semantic: gltf::Semantic, accessor: gltf::Accessor, buffer_data: &Vec>, - custom_vertex_attributes: &HashMap, + custom_vertex_attributes: &HashMap, MeshVertexAttribute>, ) -> Result<(MeshVertexAttribute, Values), ConvertAttributeError> { if let Some((attribute, conversion)) = match &semantic { gltf::Semantic::Positions => Some((Mesh::ATTRIBUTE_POSITION, ConversionMode::Any)), @@ -271,7 +271,7 @@ pub(crate) fn convert_attribute( Some((Mesh::ATTRIBUTE_JOINT_WEIGHT, ConversionMode::JointWeight)) } gltf::Semantic::Extras(name) => custom_vertex_attributes - .get(name) + .get(name.as_str()) .map(|attr| (attr.clone(), ConversionMode::Any)), _ => None, } { diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index ab921024129a5..f3812b4f1d14f 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -125,7 +125,7 @@ impl CachedPipelineState { #[derive(Default)] struct ShaderData { pipelines: HashSet, - processed_shaders: HashMap, ErasedShaderModule>, + processed_shaders: HashMap, ErasedShaderModule>, resolved_imports: HashMap>, dependents: HashSet>, } @@ -277,7 +277,7 @@ impl ShaderCache { data.pipelines.insert(pipeline); // PERF: this shader_defs clone isn't great. use raw_entry_mut when it stabilizes - let module = match data.processed_shaders.entry(shader_defs.to_vec()) { + let module = match data.processed_shaders.entry(Box::from(shader_defs)) { Entry::Occupied(entry) => entry.into_mut(), Entry::Vacant(entry) => { let mut shader_defs = shader_defs.to_vec(); diff --git a/tools/build-templated-pages/src/examples.rs b/tools/build-templated-pages/src/examples.rs index b1cdc3ad32dd3..c50f3e03ac2d9 100644 --- a/tools/build-templated-pages/src/examples.rs +++ b/tools/build-templated-pages/src/examples.rs @@ -80,7 +80,7 @@ fn parse_examples(panic_on_missing: bool) -> Vec { .collect() } -fn parse_categories() -> HashMap { +fn parse_categories() -> HashMap, String> { let manifest_file = std::fs::read_to_string("Cargo.toml").unwrap(); let manifest = manifest_file.parse::().unwrap(); manifest @@ -95,7 +95,7 @@ fn parse_categories() -> HashMap { .iter() .map(|v| { ( - v.get("name").unwrap().as_str().unwrap().to_string(), + Box::from(v.get("name").unwrap().as_str().unwrap()), v.get("description").unwrap().as_str().unwrap().to_string(), ) }) @@ -107,7 +107,7 @@ pub(crate) fn check(what_to_run: Command) { if what_to_run.contains(Command::UPDATE) { let categories = parse_categories(); - let examples_by_category: HashMap = examples + let examples_by_category: HashMap, Category> = examples .into_iter() .fold(HashMap::>::new(), |mut v, ex| { v.entry(ex.category.clone()).or_default().push(ex); @@ -116,6 +116,7 @@ pub(crate) fn check(what_to_run: Command) { .into_iter() .map(|(key, mut examples)| { examples.sort(); + let key = key.into_boxed_str(); let description = categories.get(&key).cloned(); ( key, From c69531c1d5b028888b6a4e2bbb50542108e665b8 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 12:09:32 +0100 Subject: [PATCH 08/19] change HashSet for immutable values --- crates/bevy_app/src/app.rs | 4 ++-- crates/bevy_asset/src/server/info.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 54a324a85f0b2..357f17852097d 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -79,7 +79,7 @@ pub struct App { pub main_schedule_label: InternedScheduleLabel, sub_apps: HashMap, plugin_registry: Vec>, - plugin_name_added: HashSet, + plugin_name_added: HashSet>, /// A private counter to prevent incorrect calls to `App::run()` from `Plugin::build()` building_plugin_depth: usize, plugins_state: PluginsState, @@ -643,7 +643,7 @@ impl App { plugin: Box, ) -> Result<&mut Self, AppError> { debug!("added plugin: {}", plugin.name()); - if plugin.is_unique() && !self.plugin_name_added.insert(plugin.name().to_string()) { + if plugin.is_unique() && !self.plugin_name_added.insert(Box::from(plugin.name())) { Err(AppError::DuplicatePlugin { plugin_name: plugin.name().to_string(), })?; diff --git a/crates/bevy_asset/src/server/info.rs b/crates/bevy_asset/src/server/info.rs index 22c03efbd0760..aeb2f3ba49bf8 100644 --- a/crates/bevy_asset/src/server/info.rs +++ b/crates/bevy_asset/src/server/info.rs @@ -71,7 +71,7 @@ pub(crate) struct AssetInfos { pub(crate) loader_dependants: HashMap, HashSet>>, /// Tracks living labeled assets for a given source asset. /// This should only be set when watching for changes to avoid unnecessary work. - pub(crate) living_labeled_assets: HashMap, HashSet>, + pub(crate) living_labeled_assets: HashMap, HashSet>>, pub(crate) handle_providers: TypeIdMap, pub(crate) dependency_loaded_event_sender: TypeIdMap, pub(crate) dependency_failed_event_sender: @@ -113,7 +113,7 @@ impl AssetInfos { fn create_handle_internal( infos: &mut HashMap, handle_providers: &TypeIdMap, - living_labeled_assets: &mut HashMap, HashSet>, + living_labeled_assets: &mut HashMap, HashSet>>, watching_for_changes: bool, type_id: TypeId, path: Option>, @@ -129,7 +129,7 @@ impl AssetInfos { let mut without_label = path.to_owned(); if let Some(label) = without_label.take_label() { let labels = living_labeled_assets.entry(without_label).or_default(); - labels.insert(label.to_string()); + labels.insert(Box::from(label.as_ref())); } } } @@ -613,7 +613,7 @@ impl AssetInfos { info: &AssetInfo, loader_dependants: &mut HashMap, HashSet>>, path: &AssetPath<'static>, - living_labeled_assets: &mut HashMap, HashSet>, + living_labeled_assets: &mut HashMap, HashSet>>, ) { for loader_dependency in info.loader_dependencies.keys() { if let Some(dependants) = loader_dependants.get_mut(loader_dependency) { @@ -642,7 +642,7 @@ impl AssetInfos { infos: &mut HashMap, path_to_id: &mut HashMap, TypeIdMap>, loader_dependants: &mut HashMap, HashSet>>, - living_labeled_assets: &mut HashMap, HashSet>, + living_labeled_assets: &mut HashMap, HashSet>>, watching_for_changes: bool, id: UntypedAssetId, ) -> bool { From e044b0a45ccd9a59389ac49d0226c1fd6ea5f62e Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 15:08:26 +0100 Subject: [PATCH 09/19] use `into` over `Box::from` --- crates/bevy_app/src/app.rs | 2 +- crates/bevy_asset/src/io/embedded/mod.rs | 4 ++-- crates/bevy_asset/src/io/gated.rs | 4 ++-- crates/bevy_asset/src/lib.rs | 2 +- crates/bevy_asset/src/processor/mod.rs | 2 +- crates/bevy_asset/src/server/info.rs | 2 +- crates/bevy_asset/src/server/loaders.rs | 4 ++-- crates/bevy_ecs/src/bundle.rs | 2 +- crates/bevy_ecs/src/storage/table.rs | 5 +---- crates/bevy_gltf/src/lib.rs | 2 +- crates/bevy_gltf/src/loader.rs | 14 +++++--------- .../src/render_resource/pipeline_cache.rs | 2 +- tools/build-templated-pages/src/examples.rs | 2 +- 13 files changed, 20 insertions(+), 27 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 357f17852097d..f32017aa0b852 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -643,7 +643,7 @@ impl App { plugin: Box, ) -> Result<&mut Self, AppError> { debug!("added plugin: {}", plugin.name()); - if plugin.is_unique() && !self.plugin_name_added.insert(Box::from(plugin.name())) { + if plugin.is_unique() && !self.plugin_name_added.insert(plugin.name().into()) { Err(AppError::DuplicatePlugin { plugin_name: plugin.name().to_string(), })?; diff --git a/crates/bevy_asset/src/io/embedded/mod.rs b/crates/bevy_asset/src/io/embedded/mod.rs index 9af5e0ec55d18..b953e1e669f08 100644 --- a/crates/bevy_asset/src/io/embedded/mod.rs +++ b/crates/bevy_asset/src/io/embedded/mod.rs @@ -35,7 +35,7 @@ impl EmbeddedAssetRegistry { #[cfg(feature = "embedded_watcher")] self.root_paths .write() - .insert(full_path.into_boxed_path(), asset_path.to_owned()); + .insert(full_path.into(), asset_path.to_owned()); self.dir.insert_asset(asset_path, value); } @@ -48,7 +48,7 @@ impl EmbeddedAssetRegistry { #[cfg(feature = "embedded_watcher")] self.root_paths .write() - .insert(Box::from(full_path), asset_path.to_owned()); + .insert(full_path.into(), asset_path.to_owned()); self.dir.insert_meta(asset_path, value); } diff --git a/crates/bevy_asset/src/io/gated.rs b/crates/bevy_asset/src/io/gated.rs index 4db9d755d3487..1250dabf75f4b 100644 --- a/crates/bevy_asset/src/io/gated.rs +++ b/crates/bevy_asset/src/io/gated.rs @@ -33,7 +33,7 @@ impl GateOpener { pub fn open>(&self, path: P) { let mut gates = self.gates.write(); let gates = gates - .entry(Box::from(path.as_ref())) + .entry(path.as_ref().into()) .or_insert_with(crossbeam_channel::unbounded); gates.0.send(()).unwrap(); } @@ -62,7 +62,7 @@ impl AssetReader for GatedReader { let receiver = { let mut gates = self.gates.write(); let gates = gates - .entry(Box::from(path)) + .entry(path.into()) .or_insert_with(crossbeam_channel::unbounded); gates.1.clone() }; diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 5160e24cdc7eb..4f560f7cb6cbe 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -600,7 +600,7 @@ mod tests { *existing += 1; *existing } else { - attempt_counters.insert(Box::from(path), 1); + attempt_counters.insert(path.into(), 1); 1 } }; diff --git a/crates/bevy_asset/src/processor/mod.rs b/crates/bevy_asset/src/processor/mod.rs index 025f91fe214df..625a484330078 100644 --- a/crates/bevy_asset/src/processor/mod.rs +++ b/crates/bevy_asset/src/processor/mod.rs @@ -482,7 +482,7 @@ impl AssetProcessor { /// Set the default processor for the given `extension`. Make sure `P` is registered with [`AssetProcessor::register_processor`]. pub fn set_default_processor(&self, extension: &str) { let mut default_processors = self.data.default_processors.write(); - default_processors.insert(Box::from(extension), std::any::type_name::

()); + default_processors.insert(extension.into(), std::any::type_name::

()); } /// Returns the default processor for the given `extension`, if it exists. diff --git a/crates/bevy_asset/src/server/info.rs b/crates/bevy_asset/src/server/info.rs index aeb2f3ba49bf8..080780bcd9045 100644 --- a/crates/bevy_asset/src/server/info.rs +++ b/crates/bevy_asset/src/server/info.rs @@ -129,7 +129,7 @@ impl AssetInfos { let mut without_label = path.to_owned(); if let Some(label) = without_label.take_label() { let labels = living_labeled_assets.entry(without_label).or_default(); - labels.insert(Box::from(label.as_ref())); + labels.insert(label.as_ref().into()); } } } diff --git a/crates/bevy_asset/src/server/loaders.rs b/crates/bevy_asset/src/server/loaders.rs index e6bb5f86d2cd8..65f21d6b9b52f 100644 --- a/crates/bevy_asset/src/server/loaders.rs +++ b/crates/bevy_asset/src/server/loaders.rs @@ -44,7 +44,7 @@ impl AssetLoaders { for extension in loader.extensions() { let list = self .extension_to_loaders - .entry(Box::from(*extension)) + .entry((*extension).into()) .or_default(); if !list.is_empty() { @@ -105,7 +105,7 @@ impl AssetLoaders { for extension in extensions { let list = self .extension_to_loaders - .entry(Box::from(*extension)) + .entry((*extension).into()) .or_default(); if !list.is_empty() { diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index 7f192547c4474..81f025d1024a5 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -871,7 +871,7 @@ impl Bundles { .from_key(component_ids) .or_insert_with(|| { ( - Box::from(component_ids), + component_ids.into(), initialize_dynamic_bundle(bundle_infos, components, Vec::from(component_ids)), ) }); diff --git a/crates/bevy_ecs/src/storage/table.rs b/crates/bevy_ecs/src/storage/table.rs index ca7aa5dcbd7ec..8d6b03d1da189 100644 --- a/crates/bevy_ecs/src/storage/table.rs +++ b/crates/bevy_ecs/src/storage/table.rs @@ -872,10 +872,7 @@ impl Tables { table = table.add_column(components.get_info_unchecked(*component_id)); } tables.push(table.build()); - ( - Box::from(component_ids), - TableId::from_usize(tables.len() - 1), - ) + (component_ids.into(), TableId::from_usize(tables.len() - 1)) }); *value diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 36b9ab512a046..6dabb789c8858 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -41,7 +41,7 @@ impl GltfPlugin { attribute: MeshVertexAttribute, ) -> Self { self.custom_vertex_attributes - .insert(Box::from(name), attribute); + .insert(name.into(), attribute); self } } diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 442dec2550ef3..cd623b23f36df 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -293,7 +293,7 @@ async fn load_gltf<'a, 'b, 'c>( let handle = load_context .add_labeled_asset(format!("Animation{}", animation.index()), animation_clip); if let Some(name) = animation.name() { - named_animations.insert(Box::from(name), handle.clone()); + named_animations.insert(name.into(), handle.clone()); } animations.push(handle); } @@ -383,7 +383,7 @@ async fn load_gltf<'a, 'b, 'c>( for material in gltf.materials() { let handle = load_material(&material, load_context, false); if let Some(name) = material.name() { - named_materials.insert(Box::from(name), handle.clone()); + named_materials.insert(name.into(), handle.clone()); } materials.push(handle); } @@ -526,7 +526,7 @@ async fn load_gltf<'a, 'b, 'c>( }, ); if let Some(name) = gltf_mesh.name() { - named_meshes.insert(Box::from(name), handle.clone()); + named_meshes.insert(name.into(), handle.clone()); } meshes.push(handle); } @@ -560,11 +560,7 @@ async fn load_gltf<'a, 'b, 'c>( .collect::>>(); let named_nodes = named_nodes_intermediate .into_iter() - .filter_map(|(name, index)| { - nodes - .get(index) - .map(|handle| (Box::from(name), handle.clone())) - }) + .filter_map(|(name, index)| nodes.get(index).map(|handle| (name.into(), handle.clone()))) .collect(); let skinned_mesh_inverse_bindposes: Vec<_> = gltf @@ -661,7 +657,7 @@ async fn load_gltf<'a, 'b, 'c>( let scene_handle = load_context.add_loaded_labeled_asset(scene_label(&scene), loaded_scene); if let Some(name) = scene.name() { - named_scenes.insert(Box::from(name), scene_handle.clone()); + named_scenes.insert(name.into(), scene_handle.clone()); } scenes.push(scene_handle); } diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index f3812b4f1d14f..0bd1c1d09ef6b 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -277,7 +277,7 @@ impl ShaderCache { data.pipelines.insert(pipeline); // PERF: this shader_defs clone isn't great. use raw_entry_mut when it stabilizes - let module = match data.processed_shaders.entry(Box::from(shader_defs)) { + let module = match data.processed_shaders.entry(shader_defs.into()) { Entry::Occupied(entry) => entry.into_mut(), Entry::Vacant(entry) => { let mut shader_defs = shader_defs.to_vec(); diff --git a/tools/build-templated-pages/src/examples.rs b/tools/build-templated-pages/src/examples.rs index c50f3e03ac2d9..63370fbee2dcc 100644 --- a/tools/build-templated-pages/src/examples.rs +++ b/tools/build-templated-pages/src/examples.rs @@ -95,7 +95,7 @@ fn parse_categories() -> HashMap, String> { .iter() .map(|v| { ( - Box::from(v.get("name").unwrap().as_str().unwrap()), + v.get("name").unwrap().as_str().unwrap().into(), v.get("description").unwrap().as_str().unwrap().to_string(), ) }) From c13f82808d712c106fe5461b688aad2df19db4b3 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 15:08:47 +0100 Subject: [PATCH 10/19] avoid double-allocation --- crates/bevy_asset/src/io/memory.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/crates/bevy_asset/src/io/memory.rs b/crates/bevy_asset/src/io/memory.rs index 52a1a6ffd6264..cc13d0482056a 100644 --- a/crates/bevy_asset/src/io/memory.rs +++ b/crates/bevy_asset/src/io/memory.rs @@ -46,11 +46,7 @@ impl Dir { dir = self.get_or_insert_dir(parent); } dir.0.write().assets.insert( - path.file_name() - .unwrap() - .to_string_lossy() - .into_owned() - .into_boxed_str(), + path.file_name().unwrap().to_string_lossy().into(), Data { value: value.into(), path: path.to_owned(), @@ -64,11 +60,7 @@ impl Dir { dir = self.get_or_insert_dir(parent); } dir.0.write().metadata.insert( - path.file_name() - .unwrap() - .to_string_lossy() - .into_owned() - .into_boxed_str(), + path.file_name().unwrap().to_string_lossy().into(), Data { value: value.into(), path: path.to_owned(), @@ -81,7 +73,7 @@ impl Dir { let mut full_path = PathBuf::new(); for c in path.components() { full_path.push(c); - let name = c.as_os_str().to_string_lossy().to_string().into_boxed_str(); + let name = c.as_os_str().to_string_lossy().into(); dir = { let dirs = &mut dir.0.write().dirs; dirs.entry(name) From 660c9a4d722984ce42eee27d81a156efdc9912c8 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 15:14:29 +0100 Subject: [PATCH 11/19] convert last `HashMap` --- tools/build-templated-pages/src/examples.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/build-templated-pages/src/examples.rs b/tools/build-templated-pages/src/examples.rs index 63370fbee2dcc..873765a6f324e 100644 --- a/tools/build-templated-pages/src/examples.rs +++ b/tools/build-templated-pages/src/examples.rs @@ -109,14 +109,13 @@ pub(crate) fn check(what_to_run: Command) { let categories = parse_categories(); let examples_by_category: HashMap, Category> = examples .into_iter() - .fold(HashMap::>::new(), |mut v, ex| { - v.entry(ex.category.clone()).or_default().push(ex); + .fold(HashMap::, Vec>::new(), |mut v, ex| { + v.entry(ex.category.as_str().into()).or_default().push(ex); v }) .into_iter() .map(|(key, mut examples)| { examples.sort(); - let key = key.into_boxed_str(); let description = categories.get(&key).cloned(); ( key, From a26b43665e501fa2d91331f1ad7035c129d0835c Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sat, 24 Feb 2024 15:21:53 +0100 Subject: [PATCH 12/19] cargo fmt --- crates/bevy_gltf/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 6dabb789c8858..9cd59d674a601 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -40,8 +40,7 @@ impl GltfPlugin { name: &str, attribute: MeshVertexAttribute, ) -> Self { - self.custom_vertex_attributes - .insert(name.into(), attribute); + self.custom_vertex_attributes.insert(name.into(), attribute); self } } From 2b3f08bf78bcac7a5ca8895cd43fb87c01cec949 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com> Date: Mon, 26 Feb 2024 10:10:30 +0100 Subject: [PATCH 13/19] use `entry_ref` Co-authored-by: James Liu --- crates/bevy_asset/src/io/gated.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_asset/src/io/gated.rs b/crates/bevy_asset/src/io/gated.rs index 1250dabf75f4b..aba5582f271aa 100644 --- a/crates/bevy_asset/src/io/gated.rs +++ b/crates/bevy_asset/src/io/gated.rs @@ -33,7 +33,7 @@ impl GateOpener { pub fn open>(&self, path: P) { let mut gates = self.gates.write(); let gates = gates - .entry(path.as_ref().into()) + .entry_ref(path.as_ref()) .or_insert_with(crossbeam_channel::unbounded); gates.0.send(()).unwrap(); } From f965ae3611e33d62c81d4e6fd46f6d86176025c5 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com> Date: Mon, 26 Feb 2024 10:10:39 +0100 Subject: [PATCH 14/19] use `entry_ref` Co-authored-by: James Liu --- crates/bevy_asset/src/io/gated.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_asset/src/io/gated.rs b/crates/bevy_asset/src/io/gated.rs index aba5582f271aa..76f531a04c88a 100644 --- a/crates/bevy_asset/src/io/gated.rs +++ b/crates/bevy_asset/src/io/gated.rs @@ -62,7 +62,7 @@ impl AssetReader for GatedReader { let receiver = { let mut gates = self.gates.write(); let gates = gates - .entry(path.into()) + .entry_ref(path.as_ref()) .or_insert_with(crossbeam_channel::unbounded); gates.1.clone() }; From 76327b44c94275142b2bc4985eec7b044a36f109 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com> Date: Mon, 26 Feb 2024 10:10:46 +0100 Subject: [PATCH 15/19] use `entry_ref` Co-authored-by: James Liu --- tools/build-templated-pages/src/examples.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/build-templated-pages/src/examples.rs b/tools/build-templated-pages/src/examples.rs index 873765a6f324e..09c94b4db9811 100644 --- a/tools/build-templated-pages/src/examples.rs +++ b/tools/build-templated-pages/src/examples.rs @@ -110,7 +110,7 @@ pub(crate) fn check(what_to_run: Command) { let examples_by_category: HashMap, Category> = examples .into_iter() .fold(HashMap::, Vec>::new(), |mut v, ex| { - v.entry(ex.category.as_str().into()).or_default().push(ex); + v.entry_ref(ex.category.as_str()).or_default().push(ex); v }) .into_iter() From 456a85f9c59bfa07ecbd348e1c7bbba064a7a30b Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com> Date: Mon, 26 Feb 2024 10:10:54 +0100 Subject: [PATCH 16/19] use `entry_ref` Co-authored-by: James Liu --- crates/bevy_render/src/render_resource/pipeline_cache.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index 0bd1c1d09ef6b..2b1cc712110dd 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -277,7 +277,7 @@ impl ShaderCache { data.pipelines.insert(pipeline); // PERF: this shader_defs clone isn't great. use raw_entry_mut when it stabilizes - let module = match data.processed_shaders.entry(shader_defs.into()) { + let module = match data.processed_shaders.entry_ref(&shader_defs) { Entry::Occupied(entry) => entry.into_mut(), Entry::Vacant(entry) => { let mut shader_defs = shader_defs.to_vec(); From 2e6df79eee53bc904b9ac73b4e925256e2900980 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Mon, 26 Feb 2024 10:20:07 +0100 Subject: [PATCH 17/19] fix usage of `entry_ref` --- crates/bevy_render/src/render_resource/pipeline_cache.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index 83b91997d182d..2e2f2eeafa78e 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -4,10 +4,11 @@ use bevy_asset::{AssetEvent, AssetId, Assets}; use bevy_ecs::system::{Res, ResMut}; use bevy_ecs::{event::EventReader, system::Resource}; use bevy_tasks::Task; +use bevy_utils::hashbrown::hash_map::EntryRef; use bevy_utils::{ default, tracing::{debug, error}, - Entry, HashMap, HashSet, + HashMap, HashSet, }; use naga::valid::Capabilities; use std::{ @@ -274,9 +275,9 @@ impl ShaderCache { data.pipelines.insert(pipeline); // PERF: this shader_defs clone isn't great. use raw_entry_mut when it stabilizes - let module = match data.processed_shaders.entry_ref(&shader_defs) { - Entry::Occupied(entry) => entry.into_mut(), - Entry::Vacant(entry) => { + let module = match data.processed_shaders.entry_ref(shader_defs) { + EntryRef::Occupied(entry) => entry.into_mut(), + EntryRef::Vacant(entry) => { let mut shader_defs = shader_defs.to_vec(); #[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))] { From 349dce25dbee9d54f8f90de81bbe6f45d632d53d Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Mon, 26 Feb 2024 10:20:33 +0100 Subject: [PATCH 18/19] build-template-page: use hashbrown --- tools/build-templated-pages/Cargo.toml | 1 + tools/build-templated-pages/src/examples.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/build-templated-pages/Cargo.toml b/tools/build-templated-pages/Cargo.toml index c62a0e23ab9df..a2e0fe23fa733 100644 --- a/tools/build-templated-pages/Cargo.toml +++ b/tools/build-templated-pages/Cargo.toml @@ -14,3 +14,4 @@ toml_edit = { version = "0.22", default-features = false, features = ["parse"] } tera = "1.15" serde = { version = "1.0", features = ["derive"] } bitflags = "2.3" +hashbrown = { version = "0.14" } diff --git a/tools/build-templated-pages/src/examples.rs b/tools/build-templated-pages/src/examples.rs index 09c94b4db9811..db942da289e06 100644 --- a/tools/build-templated-pages/src/examples.rs +++ b/tools/build-templated-pages/src/examples.rs @@ -1,5 +1,6 @@ -use std::{cmp::Ordering, collections::HashMap, fs::File}; +use std::{cmp::Ordering, fs::File}; +use hashbrown::HashMap; use serde::Serialize; use tera::{Context, Tera}; use toml_edit::Document; From 3b3dddeba944a8ae258dc5fd49d4041ccced4fe0 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Mon, 26 Feb 2024 10:22:06 +0100 Subject: [PATCH 19/19] enable serde feature for hashbrown --- tools/build-templated-pages/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/build-templated-pages/Cargo.toml b/tools/build-templated-pages/Cargo.toml index a2e0fe23fa733..d98a6084d714e 100644 --- a/tools/build-templated-pages/Cargo.toml +++ b/tools/build-templated-pages/Cargo.toml @@ -14,4 +14,4 @@ toml_edit = { version = "0.22", default-features = false, features = ["parse"] } tera = "1.15" serde = { version = "1.0", features = ["derive"] } bitflags = "2.3" -hashbrown = { version = "0.14" } +hashbrown = { version = "0.14", features = ["serde"] }