C、C++ 和 Java 中的提升/重新排序:变量声明必须始终位于上下文的顶部吗?

2024-02-20

我读了一些关于提升和重新排序 https://stackoverflow.com/questions/11430095/are-hoisting-and-reordering-the-same-thing,所以看来Java VM可能会选择提升一些表达式。我还阅读了有关 Javascript 中函数声明提升的内容。

第一个问题:有人可以确认 C、C++ 和 Java 中是否通常存在提升吗?或者它们都依赖于编译器/优化吗?

我读过很多示例 C 代码,它们总是将变量声明放在顶部,在任何变量之前assert or 边界条件。我认为完成所有操作会更快一些asserts and 边界情况在变量声明之前,因为函数可能会终止。

主要问题:变量声明必须始终位于上下文的顶部吗? (这里有提升在起作用吗?)或者编译器是否通过检查这些独立的来自动优化代码asserts and 边界情况首先(在不相关的变量声明之前)?

这是一个相关的例子:

void MergeSort(struct node** headRef) {
    struct node* a;
    struct node* b;
    if ((*headRef == NULL) || ((*headRef)->next == NULL)) {
        return;
    }
    FrontBackSplit(*headRef, &a, &b);
    MergeSort(&a);
    MergeSort(&b);
    *headRef = SortedMerge(a, b);
}

如上所示,边界情况不依赖于变量“a”和“b”。因此,将边界情况放在变量声明之上会使速度稍微快一些吗?


Updates:

上面的例子并不像我希望的那么好,因为变量“a”和“b”只是被声明,没有在那里初始化。编译器会忽略声明,直到我们真正需要使用它们。

我检查了 GNU GCC 程序集的变量声明和初始化,这些程序集具有不同的执行顺序。编译器没有改变我的独立顺序asserts and 边界情况. 所以,重新排序这些asserts and 边界情况确实更改组件,从而改变机器运行它们的方式。

我认为差异很小,大多数人从不关心这一点。


编译器可以根据需要重新排序/修改您的代码,只要修改后的代码与按顺序执行的原始代码等效即可。所以吊装是允许的,但不是必需的。这是一种优化,并且完全是编译器特定的。

C++ 中的变量声明可以在您希望的任何位置。在 C 中,它们过去必须位于上下文的顶部,但是当引入 c99 标准时,规则被放宽,现在它们可以位于任何您想要的位置,类似于 C++。尽管如此,许多 C 程序员仍然坚持将它们放在上下文中的顶部。

在您的示例中,编译器可以自由地将 if 语句移至顶部,但我认为不会。这些变量只是在堆栈上声明且未初始化的指针,声明它们的成本是最小的,而且在函数开头而不是在断言之后创建它们可能更有效。

例如,如果您的声明会涉及任何副作用

struct node *a = some_function();

那么编译器可以重新排序的内容将受到限制。

Edit:

我用这个短程序在实践中检查了 GCC 的循环提升:

#include <stdio.h>
int main(int argc, char **argv) {
    int dummy = 2 * argc;
    int i = 1;
    while (i<=10 && dummy != 4)
        printf("%d\n", i++);
    return 0;
}

我用这个命令编译它:

gcc -std=c99 -pedantic test.c -S -o test.asm

这是输出:

    .file   "test.c"
    .def    ___main;    .scl    2;  .type   32; .endef
    .section .rdata,"dr"
LC0:
    .ascii "%d\12\0"
    .text
    .globl  _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
LFB7:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $32, %esp
    call    ___main
    movl    8(%ebp), %eax
    addl    %eax, %eax
    movl    %eax, 24(%esp)
    movl    $1, 28(%esp)
    jmp L2
L4:
    movl    28(%esp), %eax
    leal    1(%eax), %edx
    movl    %edx, 28(%esp)
    movl    %eax, 4(%esp)
    movl    $LC0, (%esp)
    call    _printf
L2:
    cmpl    $10, 28(%esp)
    jg  L3
    cmpl    $4, 24(%esp)
    jne L4
L3:
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE7:
    .ident  "GCC: (GNU) 4.8.2"
    .def    _printf;    .scl    2;  .type   32; .endef

然后我用这个命令编译它:

gcc -std=c99 -pedantic test.c -O3 -S -o test.asm

这是输出:

    .file   "test.c"
    .def    ___main;    .scl    2;  .type   32; .endef
    .section .rdata,"dr"
LC0:
    .ascii "%d\12\0"
    .section    .text.startup,"x"
    .p2align 4,,15
    .globl  _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
LFB7:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    pushl   %ebx
    andl    $-16, %esp
    subl    $16, %esp
    .cfi_offset 3, -12
    call    ___main
    movl    8(%ebp), %eax
    leal    (%eax,%eax), %edx
    movl    $1, %eax
    cmpl    $4, %edx
    jne L8
    jmp L6
    .p2align 4,,7
L12:
    movl    %ebx, %eax
L8:
    leal    1(%eax), %ebx
    movl    %eax, 4(%esp)
    movl    $LC0, (%esp)
    call    _printf
    cmpl    $11, %ebx
    jne L12
L6:
    xorl    %eax, %eax
    movl    -4(%ebp), %ebx
    leave
    .cfi_restore 5
    .cfi_restore 3
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE7:
    .ident  "GCC: (GNU) 4.8.2"
    .def    _printf;    .scl    2;  .type   32; .endef

基本上,打开优化后,原始代码将转换为如下所示:

#include <stdio.h>
int main(int argc, char **argv) {
    int dummy = 2 * argc;
    int i = 1;
    if (dummy != 4)
        while (i<=10)
            printf("%d\n", i++);
    return 0;
}

所以,正如你所看到的,C 中确实存在提升。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C、C++ 和 Java 中的提升/重新排序:变量声明必须始终位于上下文的顶部吗? 的相关文章