Start using hashbrown (#6925)

This commit is contained in:
@brodycj - C. Jonathan Brody 2025-01-16 08:53:36 -05:00 committed by GitHub
parent 779261e64d
commit 623f143a82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 48 additions and 20 deletions

View File

@ -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

4
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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

View File

@ -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,

View File

@ -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(),

View File

@ -4,10 +4,10 @@
/// HashMap using a fast, non-cryptographic hash algorithm.
pub type FastHashMap<K, V> =
std::collections::HashMap<K, V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
hashbrown::HashMap<K, V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
/// HashSet using a fast, non-cryptographic hash algorithm.
pub type FastHashSet<K> =
std::collections::HashSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
hashbrown::HashSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
/// IndexMap using a fast, non-cryptographic hash algorithm.
pub type FastIndexMap<K, V> =

View File

@ -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,

View File

@ -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};

View File

@ -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};

View File

@ -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

View File

@ -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;

View File

@ -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};

View File

@ -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};

View File

@ -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,

View File

@ -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<T, U> = HashMap<T, U, core::hash::BuildHasherDefault<FxHasher>>;
#[derive(Clone, Debug)]
pub struct Api;
@ -641,8 +645,8 @@ struct DeviceShared {
private_caps: PrivateCapabilities,
workarounds: Workarounds,
features: wgt::Features,
render_passes: Mutex<rustc_hash::FxHashMap<RenderPassKey, vk::RenderPass>>,
framebuffers: Mutex<rustc_hash::FxHashMap<FramebufferKey, vk::Framebuffer>>,
render_passes: Mutex<FxHashMap<RenderPassKey, vk::RenderPass>>,
framebuffers: Mutex<FxHashMap<FramebufferKey, vk::Framebuffer>>,
sampler_cache: Mutex<sampler::SamplerCache>,
memory_allocations_counter: InternalCounter,
}

View File

@ -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.

View File

@ -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

View File

@ -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,