diff --git a/CHANGELOG.md b/CHANGELOG.md index e6009f3c4..83aa4a68b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,14 @@ Bottom level categories: ## Unreleased +### Changes + +#### Start using `hashbrown` + +Use `hashbrown` in `wgpu-core`, `wgpu-hal` & `wgpu-info` to simplify no-std support. (This may help improve performance as well.) + +By @brodycj in [#6925](https://github.com/gfx-rs/wgpu/pull/6925). + ## v24.0.0 (2025-01-15) ### Major changes diff --git a/Cargo.lock b/Cargo.lock index 03229043a..41d277851 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1605,6 +1605,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" dependencies = [ "foldhash", + "serde", ] [[package]] @@ -4047,6 +4048,7 @@ dependencies = [ "bytemuck", "cfg_aliases 0.2.1", "document-features", + "hashbrown", "indexmap", "log", "naga", @@ -4117,6 +4119,7 @@ dependencies = [ "gpu-alloc", "gpu-allocator", "gpu-descriptor", + "hashbrown", "js-sys", "khronos-egl", "libc", @@ -4153,6 +4156,7 @@ dependencies = [ "anyhow", "bitflags 2.8.0", "env_logger", + "hashbrown", "pico-args", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 698993493..fc5c0e0b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,6 +94,10 @@ flume = "0.11" futures-lite = "2" getrandom = "0.2" glam = "0.29" +hashbrown = { version = "0.15.2", default-features = false, features = [ + "default-hasher", + "inline-more", +] } heck = "0.5.0" image = { version = "0.24", default-features = false, features = ["png"] } indexmap = "2" diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index 4e2ed64d0..00872dcbb 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -119,6 +119,7 @@ bit-vec.workspace = true bitflags.workspace = true bytemuck = { workspace = true, optional = true } document-features.workspace = true +hashbrown.workspace = true indexmap.workspace = true log.workspace = true once_cell.workspace = true diff --git a/wgpu-core/src/command/memory_init.rs b/wgpu-core/src/command/memory_init.rs index 50a2772a9..909b001b6 100644 --- a/wgpu-core/src/command/memory_init.rs +++ b/wgpu-core/src/command/memory_init.rs @@ -1,4 +1,6 @@ -use std::{collections::hash_map::Entry, ops::Range, sync::Arc, vec::Drain}; +use std::{ops::Range, sync::Arc, vec::Drain}; + +use hashbrown::hash_map::Entry; use crate::{ device::Device, diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 681367735..2d326a392 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -2712,8 +2712,8 @@ impl Device { .map(|mut bgl_entry_map| { bgl_entry_map.sort(); match unique_bind_group_layouts.entry(bgl_entry_map) { - std::collections::hash_map::Entry::Occupied(v) => Ok(Arc::clone(v.get())), - std::collections::hash_map::Entry::Vacant(e) => { + hashbrown::hash_map::Entry::Occupied(v) => Ok(Arc::clone(v.get())), + hashbrown::hash_map::Entry::Vacant(e) => { match self.create_bind_group_layout( &None, e.key().clone(), diff --git a/wgpu-core/src/hash_utils.rs b/wgpu-core/src/hash_utils.rs index 056c84f53..fa2db6bf2 100644 --- a/wgpu-core/src/hash_utils.rs +++ b/wgpu-core/src/hash_utils.rs @@ -4,10 +4,10 @@ /// HashMap using a fast, non-cryptographic hash algorithm. pub type FastHashMap = - std::collections::HashMap>; + hashbrown::HashMap>; /// HashSet using a fast, non-cryptographic hash algorithm. pub type FastHashSet = - std::collections::HashSet>; + hashbrown::HashSet>; /// IndexMap using a fast, non-cryptographic hash algorithm. pub type FastIndexMap = diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 6b1e721d4..dff1106be 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -1,5 +1,7 @@ +use std::borrow::Cow; use std::sync::Arc; -use std::{borrow::Cow, collections::HashMap}; + +use hashbrown::HashMap; use crate::{ api_log, api_log_debug, diff --git a/wgpu-core/src/pool.rs b/wgpu-core/src/pool.rs index d14b8162e..375b36c32 100644 --- a/wgpu-core/src/pool.rs +++ b/wgpu-core/src/pool.rs @@ -1,9 +1,9 @@ use std::{ - collections::{hash_map::Entry, HashMap}, hash::Hash, sync::{Arc, Weak}, }; +use hashbrown::{hash_map::Entry, HashMap}; use once_cell::sync::OnceCell; use crate::lock::{rank, Mutex}; diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 9f0c8b78e..17c42ecf4 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -1,6 +1,7 @@ use crate::{device::bgl, resource::InvalidResourceError, FastHashMap, FastHashSet}; use arrayvec::ArrayVec; -use std::{collections::hash_map::Entry, fmt}; +use hashbrown::hash_map::Entry; +use std::fmt; use thiserror::Error; use wgt::{BindGroupLayoutEntry, BindingType}; diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index 7287246df..0d810c2cf 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -121,6 +121,7 @@ required-features = ["gles"] [dependencies] bitflags.workspace = true +hashbrown.workspace = true parking_lot.workspace = true profiling = { workspace = true, default-features = false } raw-window-handle.workspace = true diff --git a/wgpu-hal/src/gles/egl.rs b/wgpu-hal/src/gles/egl.rs index 24d5a125b..9a58bbc89 100644 --- a/wgpu-hal/src/gles/egl.rs +++ b/wgpu-hal/src/gles/egl.rs @@ -1,10 +1,9 @@ use glow::HasContext; +use hashbrown::HashMap; use once_cell::sync::Lazy; use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, RwLock}; -use std::{ - collections::HashMap, ffi, mem::ManuallyDrop, os::raw, ptr, rc::Rc, sync::Arc, time::Duration, -}; +use std::{ffi, mem::ManuallyDrop, os::raw, ptr, rc::Rc, sync::Arc, time::Duration}; /// The amount of time to wait while trying to obtain a lock to the adapter context const CONTEXT_LOCK_TIMEOUT_SECS: u64 = 1; diff --git a/wgpu-hal/src/gles/wgl.rs b/wgpu-hal/src/gles/wgl.rs index 2d6c91aee..b5d68e76f 100644 --- a/wgpu-hal/src/gles/wgl.rs +++ b/wgpu-hal/src/gles/wgl.rs @@ -1,5 +1,4 @@ use std::{ - collections::HashSet, ffi::{c_void, CStr, CString}, mem::{self, size_of, size_of_val, ManuallyDrop}, os::raw::c_int, @@ -17,6 +16,7 @@ use glutin_wgl_sys::wgl_extra::{ Wgl, CONTEXT_CORE_PROFILE_BIT_ARB, CONTEXT_DEBUG_BIT_ARB, CONTEXT_FLAGS_ARB, CONTEXT_PROFILE_MASK_ARB, }; +use hashbrown::HashSet; use once_cell::sync::Lazy; use parking_lot::{Mutex, MutexGuard, RwLock}; use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 6f0d478ef..cd1136a3b 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -26,7 +26,6 @@ mod surface; mod time; use std::{ - collections::HashMap, fmt, iter, ops, ptr::NonNull, sync::{atomic, Arc}, @@ -35,6 +34,7 @@ use std::{ use arrayvec::ArrayVec; use bitflags::bitflags; +use hashbrown::HashMap; use metal::foreign_types::ForeignTypeRef as _; use parking_lot::{Mutex, RwLock}; diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index fdf1f3e3e..03fa9c0c5 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -2,12 +2,13 @@ use super::{conv, RawTlasInstance}; use arrayvec::ArrayVec; use ash::{khr, vk}; +use hashbrown::hash_map::Entry; use parking_lot::Mutex; use crate::TlasInstance; use std::{ borrow::Cow, - collections::{hash_map::Entry, BTreeMap}, + collections::BTreeMap, ffi::{CStr, CString}, mem::{self, size_of, MaybeUninit}, num::NonZeroU32, diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index 1f75bba21..31a893e56 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -33,7 +33,6 @@ mod sampler; use std::{ borrow::Borrow, - collections::HashSet, ffi::{CStr, CString}, fmt, mem, num::NonZeroU32, @@ -42,12 +41,17 @@ use std::{ use arrayvec::ArrayVec; use ash::{ext, khr, vk}; +use hashbrown::{HashMap, HashSet}; use parking_lot::{Mutex, RwLock}; +use rustc_hash::FxHasher; use wgt::InternalCounter; const MILLIS_TO_NANOS: u64 = 1_000_000; const MAX_TOTAL_ATTACHMENTS: usize = crate::MAX_COLOR_ATTACHMENTS * 2 + 1; +// NOTE: This type alias is similar to rustc_hash::FxHashMap but works with hashbrown. +type FxHashMap = HashMap>; + #[derive(Clone, Debug)] pub struct Api; @@ -641,8 +645,8 @@ struct DeviceShared { private_caps: PrivateCapabilities, workarounds: Workarounds, features: wgt::Features, - render_passes: Mutex>, - framebuffers: Mutex>, + render_passes: Mutex>, + framebuffers: Mutex>, sampler_cache: Mutex, memory_allocations_counter: InternalCounter, } diff --git a/wgpu-hal/src/vulkan/sampler.rs b/wgpu-hal/src/vulkan/sampler.rs index 11030226f..5fd32c33d 100644 --- a/wgpu-hal/src/vulkan/sampler.rs +++ b/wgpu-hal/src/vulkan/sampler.rs @@ -2,9 +2,8 @@ //! //! Nearly identical to the DX12 sampler cache, without descriptor heap management. -use std::collections::{hash_map::Entry, HashMap}; - use ash::vk; +use hashbrown::{hash_map::Entry, HashMap}; use ordered_float::OrderedFloat; /// If the allowed sampler count is above this value, the sampler cache is disabled. diff --git a/wgpu-info/Cargo.toml b/wgpu-info/Cargo.toml index 8d05139ad..9deb4603e 100644 --- a/wgpu-info/Cargo.toml +++ b/wgpu-info/Cargo.toml @@ -13,6 +13,7 @@ license.workspace = true anyhow.workspace = true bitflags.workspace = true env_logger.workspace = true +hashbrown = { workspace = true, features = ["serde"] } pico-args.workspace = true serde = { workspace = true, features = ["default"] } serde_json.workspace = true diff --git a/wgpu-info/src/report.rs b/wgpu-info/src/report.rs index 974885d2b..954908b09 100644 --- a/wgpu-info/src/report.rs +++ b/wgpu-info/src/report.rs @@ -1,5 +1,6 @@ -use std::{collections::HashMap, io}; +use std::io; +use hashbrown::HashMap; use serde::{Deserialize, Serialize}; use wgpu::{ AdapterInfo, DownlevelCapabilities, Features, Limits, TextureFormat, TextureFormatFeatures,