diskann_vector/utils.rs
1/*
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT license.
4 */
5
6#[cfg(target_arch = "x86_64")]
7use std::arch::x86_64::{_mm_prefetch, _MM_HINT_T0};
8
9/// Prefetch the given vector in chunks of 64 bytes, which is a cache line size
10/// NOTE: good efficiency when total_vec_size is integral multiple of 64
11#[inline]
12pub fn prefetch_vector<T>(vec: &[T]) {
13 #[cfg(target_arch = "x86_64")]
14 {
15 let vec_ptr = vec.as_ptr() as *const i8;
16 let vecsize = std::mem::size_of_val(vec);
17 let max_prefetch_size = (vecsize / 64) * 64;
18
19 for d in (0..max_prefetch_size).step_by(64) {
20 unsafe {
21 _mm_prefetch(vec_ptr.add(d), _MM_HINT_T0);
22 }
23 }
24 }
25
26 #[cfg(not(target_arch = "x86_64"))]
27 {
28 // No-op for non-x86_64 architectures
29 // Prefetching is not critical for functionality, just performance optimization
30 let _ = vec;
31 }
32}