V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Kaleidopink
V2EX  ›  Rust

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

  •  1
     
  •   Kaleidopink · 17 天前 · 1214 次点击

    结果在我的 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());
        
    }
    
    13 条回复    2024-09-02 15:13:24 +08:00
    nagisaushio
        1
    nagisaushio  
       17 天前
    为啥不直接用 get_mut
    nagisaushio
        2
    nagisaushio  
       17 天前
    你这写法有问题啊,release build 时会报错。

    let mut arr = Array::from(vec![0u32; SIZE].as_ptr());

    这句完了之后 vec 被 drop 了
    Kaleidopink
        3
    Kaleidopink  
    OP
       17 天前
    @nagisaushio 确实, 但是我的上面 build --release 没有任何报错不知道为啥, 导致我没有发现😐
    nagisaushio
        4
    nagisaushio  
       17 天前
    这其实是个 UB ,release build 可以选择在任意地方 drop

    另外我用 debug 和 release 测了几遍。两段耗时是差不多的。其实这里 rand::random 的计算量比 IndexMut 大多了,占了时间的大头,IndexMut 那一点微小的差异基本观测不到。所以其实测了个寂寞
    Kaleidopink
        5
    Kaleidopink  
    OP
       17 天前
    @nagisaushio 不会吧, 即便我把循环次数拖到 2 亿次, 两者也能观察到 115 ms 和 85 ms 的差距.
    Kaleidopink
        6
    Kaleidopink  
    OP
       17 天前
    @nagisaushio 当然我才发现不管是 slice 还是 Vec 其实都有 get_unchecked_mut 这个方法, 所以确实写了个寂寞.
    nagisaushio
        7
    nagisaushio  
       17 天前
    https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=e7fe53cb9006e2ce5406778cdcc16296

    你用这个多跑几次看看呢。我换了个更轻的 rng ,固定种子,消除额外的影响
    gwy15
        8
    gwy15  
       17 天前 via iPhone
    1. 你可以直接 get_unchecked
    2. 有 use-after-drop ,你这不是 unsafe 是 unsound
    3. Array 这个命名是错误的,应该叫 Pointer ,里面没有存长度
    4. 你可以直接看 asm 确定有没有优化掉边界检查
    槽点太多了……建议多看代码和文档,思而不学也不行
    gwy15
        9
    gwy15  
       17 天前 via iPhone
    另外正确的 benchmark 应该是用 black_box 来避免编译器的优化,而不是在计时路径上引入一个 rng……
    Kaleidopink
        10
    Kaleidopink  
    OP
       16 天前
    @nagisaushio 在 playground 里面跑出来反而没有越界检查的更慢了, 在自己机器上跑就是正常的, 好生奇怪.
    Kaleidopink
        11
    Kaleidopink  
    OP
       16 天前
    @gwy15 学到了, benchmark 确实没写过
    Kauruus
        12
    Kauruus  
       16 天前
    之前有人做过测试,以 C 为基线,Rust 的运行时检测大约带来 1.77x 的损耗,其中 bound check 大约占 50%。

    https://dl.acm.org/doi/fullHtml/10.1145/3551349.3559494
    whoami9894
        13
    whoami9894  
       14 天前
    编译器又不傻,`index % SIZE`,bounds check 绝对被优化掉了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   955 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:46 · PVG 03:46 · LAX 12:46 · JFK 15:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.