From 7b79bbeb6dd100eaab5572065e7a01ccc48f45b7 Mon Sep 17 00:00:00 2001 From: "@brodycj - C. Jonathan Brody" Date: Wed, 15 Jan 2025 21:59:10 -0500 Subject: [PATCH 1/7] start using hashbrown (in wgpu-core) --- CHANGELOG.md | 6 ++++++ Cargo.lock | 1 + Cargo.toml | 1 + wgpu-core/Cargo.toml | 1 + wgpu-core/src/command/memory_init.rs | 4 +++- wgpu-core/src/device/resource.rs | 4 ++-- wgpu-core/src/hash_utils.rs | 4 ++-- wgpu-core/src/instance.rs | 4 +++- wgpu-core/src/pool.rs | 2 +- wgpu-core/src/validation.rs | 3 ++- 10 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6009f3c46..0d26e09894 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,12 @@ Bottom level categories: ## Unreleased +### Changes + +#### Use `hashbrown` in `wgpu-core` + +Using in `hashbrown` in `wgpu-core` should improve performance and simplify no-std support. + ## v24.0.0 (2025-01-15) ### Major changes diff --git a/Cargo.lock b/Cargo.lock index 03229043af..c23968c7c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4047,6 +4047,7 @@ dependencies = [ "bytemuck", "cfg_aliases 0.2.1", "document-features", + "hashbrown", "indexmap", "log", "naga", diff --git a/Cargo.toml b/Cargo.toml index 6989934936..5073491713 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,6 +94,7 @@ 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 4e2ed64d08..00872dcbb8 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 50a2772a95..909b001b61 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 681367735f..2d326a392a 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 056c84f539..fa2db6bf27 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 6b1e721d4e..dff1106be3 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 d14b8162e3..375b36c32e 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 9f0c8b78ea..17c42ecf4e 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}; From 213f61a5ce9298da7e5869f704740fa9c1a9012e Mon Sep 17 00:00:00 2001 From: "@brodycj - C. Jonathan Brody" Date: Wed, 15 Jan 2025 22:04:20 -0500 Subject: [PATCH 2/7] update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d26e09894..0d57219e25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,8 @@ Bottom level categories: Using in `hashbrown` in `wgpu-core` should improve performance and simplify no-std support. +By @brodycj in [#6925](https://github.com/gfx-rs/wgpu/pull/6925). + ## v24.0.0 (2025-01-15) ### Major changes From d402fe5378f58613050b76efa874bbca26cda6dc Mon Sep 17 00:00:00 2001 From: "@brodycj - C. Jonathan Brody" Date: Wed, 15 Jan 2025 23:54:47 -0500 Subject: [PATCH 3/7] FIXUP taplo fmt --- Cargo.toml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5073491713..fc5c0e0b85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,7 +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"] } +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" From dc51e3bf8d99eaf226eec85998c0482dc4933833 Mon Sep 17 00:00:00 2001 From: "@brodycj - C. Jonathan Brody" Date: Thu, 16 Jan 2025 00:41:52 -0500 Subject: [PATCH 4/7] use hashbrown in `wgpu-hal`; update CHANGELOG.md (again) --- CHANGELOG.md | 4 ++-- Cargo.lock | 1 + wgpu-hal/Cargo.toml | 1 + wgpu-hal/src/gles/egl.rs | 5 ++--- wgpu-hal/src/gles/wgl.rs | 2 +- wgpu-hal/src/metal/mod.rs | 2 +- wgpu-hal/src/vulkan/device.rs | 3 ++- wgpu-hal/src/vulkan/mod.rs | 10 +++++++--- wgpu-hal/src/vulkan/sampler.rs | 3 +-- 9 files changed, 18 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d57219e25..f9824dde1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,9 +42,9 @@ Bottom level categories: ### Changes -#### Use `hashbrown` in `wgpu-core` +#### Use `hashbrown` in `wgpu-core` & `wgpu-hal` -Using in `hashbrown` in `wgpu-core` should improve performance and simplify no-std support. +Using in `hashbrown` should improve performance and simplify no-std support. By @brodycj in [#6925](https://github.com/gfx-rs/wgpu/pull/6925). diff --git a/Cargo.lock b/Cargo.lock index c23968c7c3..7ff536b27a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4118,6 +4118,7 @@ dependencies = [ "gpu-alloc", "gpu-allocator", "gpu-descriptor", + "hashbrown", "js-sys", "khronos-egl", "libc", diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index 7287246dfc..0d810c2cfb 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 24d5a125b4..9a58bbc89e 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 2d6c91aee0..b5d68e76f8 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 6f0d478ef1..cd1136a3b4 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 fdf1f3e3ef..03fa9c0c59 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 1f75bba215..31a893e56e 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 11030226f0..5fd32c33d1 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. From e8abd3dd88a065571bedb565910a8ef65a649028 Mon Sep 17 00:00:00 2001 From: "@brodycj - C. Jonathan Brody" Date: Thu, 16 Jan 2025 04:23:08 -0500 Subject: [PATCH 5/7] use hashbrown in wgpu-info --- Cargo.lock | 2 ++ wgpu-info/Cargo.toml | 1 + wgpu-info/src/report.rs | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 7ff536b27a..41d277851d 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]] @@ -4155,6 +4156,7 @@ dependencies = [ "anyhow", "bitflags 2.8.0", "env_logger", + "hashbrown", "pico-args", "serde", "serde_json", diff --git a/wgpu-info/Cargo.toml b/wgpu-info/Cargo.toml index 8d05139ad0..9deb4603e2 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 974885d2ba..954908b09e 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, From dd69c6fcd6648eacef44c341f85c143a1f3b318e Mon Sep 17 00:00:00 2001 From: "@brodycj - C. Jonathan Brody" Date: Thu, 16 Jan 2025 04:38:28 -0500 Subject: [PATCH 6/7] update CHANGELOG.md (yet again) --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9824dde1c..f5437d2086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,9 +42,9 @@ Bottom level categories: ### Changes -#### Use `hashbrown` in `wgpu-core` & `wgpu-hal` +#### Start using `hashbrown` -Using in `hashbrown` should improve performance and simplify no-std support. +Using `hashbrown` in `wgpu-core`, `wgpu-hal` & `wgpu-info` should help improve performance and simplify no-std support. By @brodycj in [#6925](https://github.com/gfx-rs/wgpu/pull/6925). From dafff7c4aedc2914c5a6e675133176b4d1c1eb47 Mon Sep 17 00:00:00 2001 From: "@brodycj - C. Jonathan Brody" Date: Thu, 16 Jan 2025 04:43:24 -0500 Subject: [PATCH 7/7] reword & update CHANGELOG.md again & yet again --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5437d2086..83aa4a68b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ Bottom level categories: #### Start using `hashbrown` -Using `hashbrown` in `wgpu-core`, `wgpu-hal` & `wgpu-info` should help improve performance and simplify no-std support. +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).