gpt4 book ai didi

rust - 为什么 Rust 堆栈框架如此之大?

转载 作者:行者123 更新时间:2023-12-03 11:23:43 24 4
gpt4 key购买 nike

我遇到了意外的早期堆栈溢出并创建了以下程序来测试该问题:

#![feature(asm)]
#[inline(never)]
fn get_rsp() -> usize {
let rsp: usize;
unsafe {
asm! {
"mov {}, rsp",
out(reg) rsp
}
}
rsp
}

fn useless_function(x: usize) {
if x > 0 {
println!("{:x}", get_rsp());
useless_function(x - 1);
}
}

fn main() {
useless_function(10);
}
这是 get_rsp拆解(根据 cargo-asm ):
tests::get_rsp:
push rax
#APP
mov rax, rsp
#NO_APP
pop rcx
ret
我不确定是什么 #APP#NO_APP做或为什么 rax被插入然后弹出 rcx ,但似乎该函数确实返回了堆栈指针。
我惊讶地发现在 Debug模式下,两个连续打印的 rsp之间的差异是 192(!),即使在 Release模式下也是 128。
据我了解,每次调用 useless_function 时都需要存储所有这些信息。是一个 usize和一个返回地址,所以我希望每个堆栈帧大约 16 字节大。
我正在使用 rustc 1.46.0 运行它在 64 位 Windows 机器上。
我的结果在机器上是否一致?这是如何解释的?

好像是用 println!有相当显着的效果。为了避免这种情况,我更改了程序(感谢@Shepmaster 的想法)以将值存储在静态数组中:
static mut RSPS: [usize; 10] = [0; 10];

#[inline(never)]
fn useless_function(x: usize) {
unsafe { RSPS[x] = get_rsp() };
if x == 0 {
return;
}
useless_function(x - 1);
}

fn main() {
useless_function(9);
println!("{:?}", unsafe { RSPS });
}
递归在 Release模式下得到优化,但在 Debug模式下,每帧仍然需要 80 个字节,这比我预期的要多得多。这只是堆栈帧在 x86 上的工作方式吗?其他语言做得更好吗?这似乎有点低效。

最佳答案

使用格式化机制,如 println!在堆栈上创建了许多东西。扩展代码中使用的宏:

fn useless_function(x: usize) {
if x > 0 {
{
::std::io::_print(::core::fmt::Arguments::new_v1(
&["", "\n"],
&match (&get_rsp(),) {
(arg0,) => [::core::fmt::ArgumentV1::new(
arg0,
::core::fmt::LowerHex::fmt,
)],
},
));
};
useless_function(x - 1);
}
}
我相信这些结构占用了大部分空间。为了证明这一点,我打印了 format_args 创建的值的大小。 ,由 println! 使用:
let sz = std::mem::size_of_val(&format_args!("{:x}", get_rsp()));
println!("{}", sz);
这表明它是48个字节。
也可以看看:
  • How do I see the expanded macro code that's causing my compile error?

  • 这样的事情应该从等式中删除打印,但编译器/优化器忽略了 inline(never)在这里提示并内联它,导致顺序值都相同。
    /// SAFETY:
    /// The length of `rsp` and the value of `x` must always match
    #[inline(never)]
    unsafe fn useless_function(x: usize, rsp: &mut [usize]) {
    if x > 0 {
    *rsp.get_unchecked_mut(0) = get_rsp();
    useless_function(x - 1, rsp.get_unchecked_mut(1..));
    }
    }

    fn main() {
    unsafe {
    let mut rsp = [0; 10];
    useless_function(rsp.len(), &mut rsp);

    for w in rsp.windows(2) {
    println!("{}", w[0] - w[1]);
    }
    }
    }
    也就是说,您可以公开该函数并查看其程序集(稍微清理一下):
    playground::useless_function:
    pushq %r15
    pushq %r14
    pushq %rbx
    testq %rdi, %rdi
    je .LBB6_3
    movq %rsi, %r14
    movq %rdi, %r15
    xorl %ebx, %ebx

    .LBB6_2:
    callq playground::get_rsp
    movq %rax, (%r14,%rbx,8)
    addq $1, %rbx
    cmpq %rbx, %r15
    jne .LBB6_2

    .LBB6_3:
    popq %rbx
    popq %r14
    popq %r15
    retq

    but in debug mode each frame still takes 80 bytes


    比较未优化的程序集:
    playground::useless_function:
    subq $104, %rsp
    movq %rdi, 80(%rsp)
    movq %rsi, 88(%rsp)
    movq %rdx, 96(%rsp)
    cmpq $0, %rdi
    movq %rdi, 56(%rsp) # 8-byte Spill
    movq %rsi, 48(%rsp) # 8-byte Spill
    movq %rdx, 40(%rsp) # 8-byte Spill
    ja .LBB44_2
    jmp .LBB44_8

    .LBB44_2:
    callq playground::get_rsp
    movq %rax, 32(%rsp) # 8-byte Spill
    xorl %eax, %eax
    movl %eax, %edx
    movq 48(%rsp), %rdi # 8-byte Reload
    movq 40(%rsp), %rsi # 8-byte Reload
    callq core::slice::<impl [T]>::get_unchecked_mut
    movq %rax, 24(%rsp) # 8-byte Spill
    movq 24(%rsp), %rax # 8-byte Reload
    movq 32(%rsp), %rcx # 8-byte Reload
    movq %rcx, (%rax)
    movq 56(%rsp), %rdx # 8-byte Reload
    subq $1, %rdx
    setb %sil
    testb $1, %sil
    movq %rdx, 16(%rsp) # 8-byte Spill
    jne .LBB44_9
    movq $1, 72(%rsp)
    movq 72(%rsp), %rdx
    movq 48(%rsp), %rdi # 8-byte Reload
    movq 40(%rsp), %rsi # 8-byte Reload
    callq core::slice::<impl [T]>::get_unchecked_mut
    movq %rax, 8(%rsp) # 8-byte Spill
    movq %rdx, (%rsp) # 8-byte Spill
    movq 16(%rsp), %rdi # 8-byte Reload
    movq 8(%rsp), %rsi # 8-byte Reload
    movq (%rsp), %rdx # 8-byte Reload
    callq playground::useless_function
    jmp .LBB44_8

    .LBB44_8:
    addq $104, %rsp
    retq

    .LBB44_9:
    leaq str.0(%rip), %rdi
    leaq .L__unnamed_7(%rip), %rdx
    movq core::panicking::panic@GOTPCREL(%rip), %rax
    movl $33, %esi
    callq *%rax
    ud2

    关于rust - 为什么 Rust 堆栈框架如此之大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64016229/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com