跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Rust] 试图通过 unsafe 绕过越界检查看越界检查有多少额外开销

发表于

结果在我的 M2 Mac mini 上,

  • 有边界检查: 1152 ms
  • 无边界检查: 1084 ms

基本是 6% 左右的时间开销(不确定我这个封装是否有额外开销).

附源代码:

struct Array<T>(*mut T);

impl<T> From<*const T> for Array<T> {
    fn from(ptr: *const T) -> Self {
        Self(ptr as *mut _)
    }
}

impl<T> std::ops::Index<usize> for Array<T> {
    type Output = T;
    fn index(&self, index: usize) -> &Self::Output {
        unsafe {
            let ptr = self.0.offset(index as isize);
            &*ptr
        }
    }
}
impl<T> std::ops::IndexMut<usize> for Array<T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        unsafe {
            let ptr = self.0.offset(index as isize);
            &mut *ptr
        }
    }
}

fn main() {
    const SIZE: usize = 1024 * 1024;
    const LOOP: usize = 2_000_000;

    let mut arr = vec![0u32; SIZE];
    let start = std::time::Instant::now();
    // array indexing with boundary check
    {
        for _ in 0..LOOP {
            let index = rand::random::<usize>() % SIZE;
            arr[index] += 1;
        }
    }
    let elapsed = start.elapsed();
    println!("Array indexing with boundary check runtime: {}ms", elapsed.as_millis());

    // to avoid cache, use a different raw array.
    let mut arr = Array::from(vec![0u32; SIZE].as_ptr());
    let start = std::time::Instant::now();
    // array indexing wthout boundary check
    {
        for _ in 0..LOOP {
            let index = rand::random::<usize>() % SIZE;
            arr[index] += 1;
        }
    }
    let elapsed = start.elapsed();
    println!("Array indexing without boundary check runtime: {}ms", elapsed.as_millis());
    
}

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.