Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use hashbrown in more crates (etc.) #6938

Open
wants to merge 29 commits into
base: trunk
Choose a base branch
from

Conversation

brody4hire
Copy link
Contributor

@brody4hire brody4hire commented Jan 17, 2025

Connections

Description

  • use hashbrown in naga & top-level wgpu crates
  • completely replace indirect usage of std::collections::HashMap via rustc_hash::FxHashMap in wgpu-hal
  • use hashbrown in main wgpu crate
  • use hashbrown in deno_webgpu
  • use hashbrown with default-features = false, as needed to support no-std build
  • other minor updates

POSSIBLE API IMPACT

Exported naga API with these updates would be exporting types & members using HashMap & HashSet from hashbrown rather than std::collections. I think this is a crucial part of supporting no-std. I don't expect the blast radius to be very high, just wanted to point this out to avoid potential surprises for anyone.

/cc @bushrat011899

Testing

  • cargo test --all-features -p naga
  • cargo test --all-features -p wgpu-core
  • cargo test --all-features -p wgpu-hal
  • cargo test --all-features -p wgpu
  • cargo xtask test

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.
  • Resolve or defer XXX todo items in these updates

@brody4hire brody4hire mentioned this pull request Jan 17, 2025
9 tasks
@brody4hire brody4hire changed the title use hashbrown in naga etc. use hashbrown in naga & main wgpu crates` etc. Jan 19, 2025
@brody4hire brody4hire changed the title use hashbrown in naga & main wgpu crates` etc. use hashbrown in naga & main wgpu crates etc. Jan 19, 2025
naga/Cargo.toml Outdated Show resolved Hide resolved
@brody4hire brody4hire marked this pull request as ready for review January 19, 2025 09:51
@brody4hire brody4hire requested review from a team and crowlKats as code owners January 19, 2025 09:51
@brody4hire brody4hire changed the title use hashbrown in naga & main wgpu crates etc. use hashbrown in naga & main wgpu crates, etc. Jan 19, 2025
@brody4hire brody4hire requested a review from Wumpf January 20, 2025 04:53
Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking generally good, some comments!

CHANGELOG.md Outdated
Comment on lines 55 to 60
##### Use `hashbrown` in `naga` & main `wgpu` crates

Use `hashbrown` to simplify no-std support. (This may help improve performance as well.)

By Brody in [#6938](https://github.com/gfx-rs/wgpu/pull/6938).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually applies to more than just this section - we generally reserve the larger "Major Changes" items for breaking changes or very heavy refactors. So like when no-std lands in the whole stack, that definitely will deserve a large item. But for this, a single line is fine as this is all internal and not user facing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes total sense, I just pushed updates to compress all of this info & remove the unmeasured performance speculation. I have also squished the older hashbrown changelog update into here, out of the other place. I think this should be much cleaner.

Incidentally for small edits like this, I wouldn't mind if the reviewer wants to make the edit as a co-author if we want to save time going back-and-forth. Totally up to your best judgment on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes total sense, I just pushed updates to compress all of this info & remove the unmeasured performance speculation. I have also squished the older hashbrown changelog update into here, out of the other place. I think this should be much cleaner.

Nice

Incidentally for small edits like this, I wouldn't mind if the reviewer wants to make the edit as a co-author if we want to save time going back-and-forth. Totally up to your best judgment on this.

Yeah I do that for complex changes where explaining it is harder than just doing it :) For most cases though, commenting is easier than fully task switching to a new branch and getting out of "review" mode.

CHANGELOG.md Outdated Show resolved Hide resolved
naga/src/back/spv/writer.rs Outdated Show resolved Hide resolved
@brody4hire brody4hire changed the title use hashbrown in naga & main wgpu crates, etc. use hashbrown in more crates (etc.) Jan 21, 2025
Comment on lines +286 to +294
/// (Similar to rustc_hash::FxHashMap but using hashbrown::HashMap instead of std::collections::HashMap.)
/// To construct a new instance: `FastHashMap::default()`
pub type FastHashMap<K, T> =
hashbrown::HashMap<K, T, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;

/// Hash set that is faster but not resilient to DoS attacks.
pub type FastHashSet<K> = rustc_hash::FxHashSet<K>;
/// (Similar to rustc_hash::FxHashSet but using hashbrown::HashSet instead of std::collections::HashMap.)
pub type FastHashSet<K> =
hashbrown::HashSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that egui is using hashers from ahash & nohash-hasher, started wondering if either of these would be worth considering. I also noticed what looks like some more hashers listed in: https://github.com/RustCrypto/hashes

I am guessing we should defer consideration to the future, just wanted to note this before I forget.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah probably. Using a special hasher likely needs to be supported by benchmarks anyway. I would be interested in seeing this investigated at some point. Could you make an issue?

Copy link
Contributor

@bushrat011899 bushrat011899 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Splitting out these "controversial" changes into more bite-sized pieces is really nice to review.

Comment on lines +135 to +136
# (with default-features = false to help avoid using any features that may require std::collections & support no-std build)
rustc-hash = { version = "1", default-features = false }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, I think libraries should generally use default-features = false anyway, since there is no way for a user to disable a feature if it's listed in a dependency, even if it's not actually being used.

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last iteration. Might be worth setting up a hashmap on a banned type list to properly enforce this at least until we're fully checking non-std.

naga/src/back/spv/writer.rs Outdated Show resolved Hide resolved
Comment on lines +286 to +294
/// (Similar to rustc_hash::FxHashMap but using hashbrown::HashMap instead of std::collections::HashMap.)
/// To construct a new instance: `FastHashMap::default()`
pub type FastHashMap<K, T> =
hashbrown::HashMap<K, T, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;

/// Hash set that is faster but not resilient to DoS attacks.
pub type FastHashSet<K> = rustc_hash::FxHashSet<K>;
/// (Similar to rustc_hash::FxHashSet but using hashbrown::HashSet instead of std::collections::HashMap.)
pub type FastHashSet<K> =
hashbrown::HashSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah probably. Using a special hasher likely needs to be supported by benchmarks anyway. I would be interested in seeing this investigated at some point. Could you make an issue?

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oop wrong button sorry 😅

@cwfitzgerald cwfitzgerald self-assigned this Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants