代码之家  ›  专栏  ›  技术社区  ›  kalkronline

当转换为Box<str>时,空字符串是否分配?

  •  1
  • kalkronline  · 技术社区  · 1 年前

    我正在为我的应用程序使用消息传递架构。一种常见的类型是 io::Result<Box<str>> 。有时,我想传递一个空字符串。这样做时会发生分配吗?

    fn pass_empty_string(tx: mpsc::Sender<io::Result<Box<str>>>) {
       tx.send(Ok("".into())) 
    }
    
    2 回复  |  直到 1 年前
        1
  •  4
  •   John Kugelman Michael Hodel    1 年前

    不。控制流通过了几个功能,但最终达到 RawVec::allocate_in() ,具有以下检查:

    // Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
    if T::IS_ZST || capacity == 0 {
        Self::new_in(alloc)
    }
    

    如果字符串是常数, LLVM will also optimize the code away .

    但是,请注意,这不是 放心 .

        2
  •  1
  •   kalkronline    1 年前

    基准库 divan 有一个分配跟踪器可以帮助我们回答这个问题。在里面 benches/alloc_test.rs :

    fn main() {
        divan::main();
    }
    
    #[global_allocator]
    static ALLOC: divan::AllocProfiler = divan::AllocProfiler::system();
    
    #[divan::bench]
    fn allocates() -> String {
        String::from("hello")
    }
    
    #[divan::bench]
    fn maybe_allocates() -> Box<str> {
        "".into()
    }
    

    然后,当我们运行基准时。。。

    $ cargo bench
    
         Running benches/alloc_test.rs
    Timer precision: 12 ns
    parser              fastest       │ slowest       │ median        │ mean          │ samples │ iters
    ├─ allocates        4.918 ns      │ 25.84 ns      │ 5.168 ns      │ 7.119 ns      │ 100     │ 6400
    │                   alloc:        │               │               │               │         │
    │                     1           │ 1             │ 1             │ 1             │         │
    │                     5 B         │ 5 B           │ 5 B           │ 5 B           │         │
    ╰─ maybe_allocates  0.233 ns      │ 3.125 ns      │ 0.239 ns      │ 0.277 ns      │ 100     │ 102400
    

    ……我们看到它不是!