Skip to content

Commit

Permalink
Start using hashbrown (#6925)
Browse files Browse the repository at this point in the history
  • Loading branch information
brody4hire authored Jan 16, 2025
1 parent 779261e commit 623f143
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 20 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion wgpu-core/src/command/memory_init.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/hash_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> =
Expand Down
4 changes: 3 additions & 1 deletion wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/pool.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
3 changes: 2 additions & 1 deletion wgpu-core/src/validation.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions wgpu-hal/src/gles/egl.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/gles/wgl.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ mod surface;
mod time;

use std::{
collections::HashMap,
fmt, iter, ops,
ptr::NonNull,
sync::{atomic, Arc},
Expand All @@ -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};

Expand Down
3 changes: 2 additions & 1 deletion wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ mod sampler;

use std::{
borrow::Borrow,
collections::HashSet,
ffi::{CStr, CString},
fmt, mem,
num::NonZeroU32,
Expand All @@ -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;

Expand Down Expand Up @@ -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,
}
Expand Down
3 changes: 1 addition & 2 deletions wgpu-hal/src/vulkan/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions wgpu-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion wgpu-info/src/report.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 623f143

Please sign in to comment.