-
Notifications
You must be signed in to change notification settings - Fork 348
Add a way to prefetch a hash table bucket (#677) #727
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
Open
RyanJamesStewart
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
RyanJamesStewart:feat/bucket-prefetch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| //! Batch-lookup benchmark: look up a list of keys in a large `HashMap`, with | ||
| //! and without software-prefetching a key a few iterations ahead. | ||
| //! | ||
| //! Prefetching only pays off when the table is large enough that its control | ||
| //! bytes spill out of the L2/L3 cache *and* the caller can issue the prefetch | ||
| //! far enough ahead of the use. So this benchmark sweeps the table size and | ||
| //! uses a randomized lookup order (so the access pattern is cache-hostile). | ||
| //! On a small, cache-resident table the prefetch is noise (or a slight loss); | ||
| //! the win shows up on the large sizes. | ||
|
|
||
| use criterion::{BenchmarkId, Criterion, Throughput}; | ||
| use hashbrown::{DefaultHashBuilder, HashMap}; | ||
| use std::hint::black_box; | ||
|
|
||
| // 16-byte keys, like a common join-key shape (two u64s). | ||
| type Key = (u64, u64); | ||
|
|
||
| const SIZES: &[usize] = &[1 << 12, 1 << 16, 1 << 18, 1 << 20, 1 << 22]; | ||
| const LOOKAHEAD: usize = 8; | ||
| const N_QUERIES: usize = 1 << 16; | ||
|
|
||
| fn build_map(n: usize) -> HashMap<Key, u64, DefaultHashBuilder> { | ||
| let mut m = HashMap::with_capacity_and_hasher(n, DefaultHashBuilder::default()); | ||
| for i in 0..n as u64 { | ||
| m.insert((i, i.wrapping_mul(0x9E37_79B9_7F4A_7C15)), i); | ||
| } | ||
| m | ||
| } | ||
|
|
||
| // A cheap PRNG so the lookup order is unpredictable to the prefetcher. | ||
| fn xorshift(state: &mut u64) -> u64 { | ||
| let mut x = *state; | ||
| x ^= x << 13; | ||
| x ^= x >> 7; | ||
| x ^= x << 17; | ||
| *state = x; | ||
| x | ||
| } | ||
|
|
||
| fn query_keys(n: usize) -> Vec<Key> { | ||
| let mut state = 0x1234_5678_9ABC_DEF0u64; | ||
| (0..N_QUERIES) | ||
| .map(|_| { | ||
| let i = xorshift(&mut state) % n as u64; | ||
| (i, i.wrapping_mul(0x9E37_79B9_7F4A_7C15)) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| fn lookup_naive(map: &HashMap<Key, u64, DefaultHashBuilder>, keys: &[Key]) -> u64 { | ||
| let mut sum = 0u64; | ||
| for k in keys { | ||
| if let Some(&v) = map.get(k) { | ||
| sum = sum.wrapping_add(v); | ||
| } | ||
| } | ||
| sum | ||
| } | ||
|
|
||
| fn lookup_prefetched(map: &HashMap<Key, u64, DefaultHashBuilder>, keys: &[Key]) -> u64 { | ||
| let mut sum = 0u64; | ||
| for (i, k) in keys.iter().enumerate() { | ||
| if let Some(next) = keys.get(i + LOOKAHEAD) { | ||
| map.prefetch(next); | ||
| } | ||
| if let Some(&v) = map.get(k) { | ||
| sum = sum.wrapping_add(v); | ||
| } | ||
| } | ||
| sum | ||
| } | ||
|
|
||
| pub(crate) fn register_benches(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("batch_lookup"); | ||
| group.throughput(Throughput::Elements(N_QUERIES as u64)); | ||
| for &n in SIZES { | ||
| let map = build_map(n); | ||
| let keys = query_keys(n); | ||
| group.bench_with_input(BenchmarkId::new("naive", n), &n, |b, _| { | ||
| b.iter(|| black_box(lookup_naive(black_box(&map), black_box(&keys)))); | ||
| }); | ||
| group.bench_with_input(BenchmarkId::new("prefetch", n), &n, |b, _| { | ||
| b.iter(|| black_box(lookup_prefetched(black_box(&map), black_box(&keys)))); | ||
| }); | ||
| } | ||
| group.finish(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,7 @@ mod macros; | |
| mod alloc; | ||
| mod control; | ||
| mod hasher; | ||
| mod prefetch; | ||
| mod raw; | ||
| mod util; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| //! Software prefetch hint. | ||
| //! | ||
| //! A prefetch is a *hint* to the CPU that the cache line containing a given | ||
| //! address will be accessed soon, so the memory subsystem can start fetching it | ||
| //! while the core does other work. It is purely advisory: it never reads or | ||
| //! writes memory, never faults (even for an invalid or dangling pointer), and is | ||
| //! a no-op in the Rust abstract machine. Architectures without a stable prefetch | ||
| //! intrinsic simply compile it away. | ||
| //! | ||
| //! `core::intrinsics::prefetch_read_data` is unstable, so we cannot use it here. | ||
| //! Instead we use the stable per-architecture intrinsics where they exist | ||
| //! (`_mm_prefetch` on x86/x86-64) and fall back to a no-op everywhere else. | ||
|
|
||
| /// Issues an L1 read prefetch for the cache line containing `ptr`. | ||
| /// | ||
| /// This is a hint only. `ptr` does not need to be valid, aligned, or even | ||
| /// non-null; an out-of-bounds or dangling pointer is fine and will not fault. | ||
| /// On targets without a stable prefetch intrinsic this is a no-op. | ||
| #[inline] | ||
| #[allow(clippy::let_unit_value)] | ||
| pub(crate) fn prefetch_read_l1(ptr: *const u8) { | ||
|
clarfonthey marked this conversation as resolved.
|
||
| #[cfg(all( | ||
| any(target_arch = "x86", target_arch = "x86_64"), | ||
| target_feature = "sse", | ||
| not(miri), | ||
| ))] | ||
| { | ||
| #[cfg(target_arch = "x86")] | ||
| use core::arch::x86::{_MM_HINT_T0, _mm_prefetch}; | ||
| #[cfg(target_arch = "x86_64")] | ||
| use core::arch::x86_64::{_MM_HINT_T0, _mm_prefetch}; | ||
|
|
||
| // SAFETY: `_mm_prefetch` is a hint instruction; it performs no memory | ||
| // access, never faults, and accepts any address (the Intel SDM and the | ||
| // `core::arch` docs both spell this out). The only safety requirement is | ||
| // that the `sse` target feature is available, which the `cfg` above | ||
| // guarantees on x86 / x86-64. | ||
| unsafe { | ||
| _mm_prefetch::<_MM_HINT_T0>(ptr.cast::<i8>()); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(all( | ||
| any(target_arch = "x86", target_arch = "x86_64"), | ||
| target_feature = "sse", | ||
| not(miri), | ||
| )))] | ||
| { | ||
| // No stable prefetch intrinsic on this target (aarch64 has none yet, | ||
| // and `core::intrinsics::prefetch_read_data` is unstable). Make sure | ||
| // `ptr` is still "used" so callers don't trip an unused-variable lint. | ||
| let _ = ptr; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.