为什么在Delphi中嵌套子例程会降低性能?

afa*_*rah 9 delphi

我们使用的静态分析器的报告显示:

带有本地子程序(OPTI7)的子程序

本节列出了本身具有本地子程序的子程序。特别是当这些子程序共享局部变量时,可能会对性能产生负面影响。

本指南说:

请勿使用嵌套例程嵌套例程(其他例程中的例程;也称为“本地过程”)需要某些特殊的堆栈操作,以便内部例程可以看到外部例程的变量。这导致大量开销。而不是嵌套,将过程移至单位作用域级别并传递必要的变量(如果需要,可以通过引用(使用var关键字)进行传递),或者使变量在单位范围内变为全局。

我们很想知道在验证我们的代码时是否应考虑此报告。这个问题的答案表明,应该对自己的应用程序进行概要分析以查看是否存在性能差异,但是对于嵌套例程和普通子例程之间的差异却没有多说。

嵌套例程和普通例程之间的实际区别是什么?它会导致性能下降吗?

afa*_*rah 19

tl; dr

  • 嵌套子例程有额外的push/ pops
  • 启用优化可能会消除这些优化,以使嵌套子例程和普通子例程的生成代码相同
  • 内联导致嵌套和常规子例程生成相同的代码
  • 对于带有很少参数和局部变量的简单例程,即使关闭了优化,我们也认为性能没有差异

我写了一个小测试来确定这一点,在哪里GetRTClock以1ns的精度测量当前时间:

function subprogram_main(z : Integer) : Int64;
var
  n : Integer;
  s : Int64;

  function subprogram_aux(n, z : Integer) : Integer;
  var
    i : Integer;
  begin
    // Do some useless work on the aux program
    for i := 0 to n - 1 do begin
      if (i > z) then
        z := z + i
      else
        z := z - i;
    end;
    Result := z;
  end;
begin
  s := GetRTClock;

  // Do some minor work on the main program
  n := z div 100 * 100 + 100;
  // Call the aux program
  z := subprogram_aux(n, z);

  Result := GetRTClock - s;
end;

function normal_aux(n, z : Integer) : Integer;
var
  i : Integer;
begin
    // Do some useless work on the aux program
  for i := 0 to n - 1 do begin
    if (i > z) then
      z := z + i
    else
      z := z - i;
  end;
  Result := z;
end;

function normal_main(z : Integer) : Int64;
var
  n : Integer;
  s : Int64;
begin
  s := GetRTClock;

  // Do some minor work on the main program
  n := z div 100 * 100 + 100;
  // Call the aux program
  z := normal_aux(n, z);

  Result := GetRTClock - s;
end;
Run Code Online (Sandbox Code Playgroud)

编译为:

subprogram_main

MyFormU.pas.41: begin
005CE7D0 55               push ebp
005CE7D1 8BEC             mov ebp,esp
005CE7D3 83C4E0           add esp,-$20
005CE7D6 8945FC           mov [ebp-$04],eax
MyFormU.pas.42: s := GetRTClock;
...
MyFormU.pas.45: n := z div 100 * 100 + 100;
...
MyFormU.pas.47: z := subprogram_aux(n, z);
005CE7F8 55               push ebp
005CE7F9 8B55FC           mov edx,[ebp-$04]
005CE7FC 8B45EC           mov eax,[ebp-$14]
005CE7FF E880FFFFFF       call subprogram_aux
005CE804 59               pop ecx
005CE805 8945FC           mov [ebp-$04],eax
MyFormU.pas.49: Result := GetRTClock - s;
...

normal_main

MyFormU.pas.70: begin
005CE870 55               push ebp
005CE871 8BEC             mov ebp,esp
005CE873 83C4E0           add esp,-$20
005CE876 8945FC           mov [ebp-$04],eax
MyFormU.pas.71: s := GetRTClock;
...
MyFormU.pas.74: n := z div 100 * 100 + 100;
...
MyFormU.pas.76: z := normal_aux(n, z);
005CE898 8B55FC           mov edx,[ebp-$04]
005CE89B 8B45EC           mov eax,[ebp-$14]
005CE89E E881FFFFFF       call normal_aux
005CE8A3 8945FC           mov [ebp-$04],eax
MyFormU.pas.78: Result := GetRTClock - s;
...

subprogram_aux:

MyFormU.pas.31: begin
005CE784 55               push ebp
005CE785 8BEC             mov ebp,esp
005CE787 83C4EC           add esp,-$14
005CE78A 8955F8           mov [ebp-$08],edx
005CE78D 8945FC           mov [ebp-$04],eax
MyFormU.pas.33: for i := 0 to n - 1 do begin
005CE790 8B45FC           mov eax,[ebp-$04]
005CE793 48               dec eax
005CE794 85C0             test eax,eax
005CE796 7C29             jl $005ce7c1
005CE798 40               inc eax
005CE799 8945EC           mov [ebp-$14],eax
005CE79C C745F000000000   mov [ebp-$10],$00000000
MyFormU.pas.34: if (i > z) then
005CE7A3 8B45F0           mov eax,[ebp-$10]
005CE7A6 3B45F8           cmp eax,[ebp-$08]
005CE7A9 7E08             jle $005ce7b3
MyFormU.pas.35: z := z + i
005CE7AB 8B45F0           mov eax,[ebp-$10]
005CE7AE 0145F8           add [ebp-$08],eax
005CE7B1 EB06             jmp $005ce7b9
MyFormU.pas.37: z := z - i;
005CE7B3 8B45F0           mov eax,[ebp-$10]
005CE7B6 2945F8           sub [ebp-$08],eax

normal_aux:

MyFormU.pas.55: begin
005CE824 55               push ebp
005CE825 8BEC             mov ebp,esp
005CE827 83C4EC           add esp,-$14
005CE82A 8955F8           mov [ebp-$08],edx
005CE82D 8945FC           mov [ebp-$04],eax
MyFormU.pas.57: for i := 0 to n - 1 do begin
005CE830 8B45FC           mov eax,[ebp-$04]
005CE833 48               dec eax
005CE834 85C0             test eax,eax
005CE836 7C29             jl $005ce861
005CE838 40               inc eax
005CE839 8945EC           mov [ebp-$14],eax
005CE83C C745F000000000   mov [ebp-$10],$00000000
MyFormU.pas.58: if (i > z) then
005CE843 8B45F0           mov eax,[ebp-$10]
005CE846 3B45F8           cmp eax,[ebp-$08]
005CE849 7E08             jle $005ce853
MyFormU.pas.59: z := z + i
005CE84B 8B45F0           mov eax,[ebp-$10]
005CE84E 0145F8           add [ebp-$08],eax
005CE851 EB06             jmp $005ce859
MyFormU.pas.61: z := z - i;
005CE853 8B45F0           mov eax,[ebp-$10]
005CE856 2945F8           sub [ebp-$08],eax
Run Code Online (Sandbox Code Playgroud)

唯一的区别是push一一pop。如果我们启用优化会怎样?

MyFormU.pas.47: z := subprogram_aux(n, z);
005CE7C5 8BD3             mov edx,ebx
005CE7C7 8BC6             mov eax,esi
005CE7C9 E8B6FFFFFF       call subprogram_aux

MyFormU.pas.76: z := normal_aux(n, z);
005CE82D 8BD3             mov edx,ebx
005CE82F 8BC6             mov eax,esi
005CE831 E8B6FFFFFF       call normal_aux
Run Code Online (Sandbox Code Playgroud)

两者完全可以编译为同一件事。

内联时会发生什么?

MyFormU.pas.76: z := normal_aux(n, z);
005CE804 8BD3             mov edx,ebx
005CE806 8BC8             mov ecx,eax
005CE808 49               dec ecx
005CE809 85C9             test ecx,ecx
005CE80B 7C11             jl $005ce81e
005CE80D 41               inc ecx
005CE80E 33C0             xor eax,eax
005CE810 3BD0             cmp edx,eax
005CE812 7D04             jnl $005ce818
005CE814 03D0             add edx,eax
005CE816 EB02             jmp $005ce81a
005CE818 2BD0             sub edx,eax
005CE81A 40               inc eax
005CE81B 49               dec ecx
005CE81C 75F2             jnz $005ce810

subprogram_main:

MyFormU.pas.47: z := subprogram_aux(n, z);
005CE7A8 8BD3             mov edx,ebx
005CE7AA 8BC8             mov ecx,eax
005CE7AC 49               dec ecx
005CE7AD 85C9             test ecx,ecx
005CE7AF 7C11             jl $005ce7c2
005CE7B1 41               inc ecx
005CE7B2 33C0             xor eax,eax
005CE7B4 3BD0             cmp edx,eax
005CE7B6 7D04             jnl $005ce7bc
005CE7B8 03D0             add edx,eax
005CE7BA EB02             jmp $005ce7be
005CE7BC 2BD0             sub edx,eax
005CE7BE 40               inc eax
005CE7BF 49               dec ecx
005CE7C0 75F2             jnz $005ce7b4
Run Code Online (Sandbox Code Playgroud)

同样,没有区别。

我还介绍了这个小示例,每个示例(正常程序和子程序)平均执行30次执行,以随机顺序调用:

constructor TForm1.Create(AOwner: TComponent);
const
  c_nSamples = 60;
  rnd_sample : array[0..c_nSamples - 1] of byte = (1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0);
var
  subprogram_gt_ns : Int64;
  normal_gt_ns     : Int64;
  rnd_input        : Integer;
  i                : Integer;
begin
  inherited Create(AOwner);

  normal_gt_ns     := 0;
  subprogram_gt_ns := 0;

  rnd_input        := Random(1000);

  for i := 0 to c_nSamples - 1 do
    if (rnd_sample[i] = 1) then
      Inc(subprogram_gt_ns, subprogram_main(rnd_input))
    else
      Inc(normal_gt_ns, normal_main(rnd_input));

  OutputDebugString(PChar(' Normal ' + FloatToStr(normal_gt_ns / 30) + ' Subprogram ' + FloatToStr(subprogram_gt_ns / 30)));
end;
Run Code Online (Sandbox Code Playgroud)

即使关闭了优化,也没有明显的区别:

Debug Output:  Normal 1166,66666666667 Subprogram 1203,33333333333 Process MyProject.exe (1824)
Run Code Online (Sandbox Code Playgroud)

最后,两个警告性能的文本都提到了共享局部变量。

如果我们不传递zsubprogram_aux,而是直接访问它,则会得到:

MyFormU.pas.47: z := subprogram_aux(n);
005CE7D2 55               push ebp
005CE7D3 8BC3             mov eax,ebx
005CE7D5 E8AAFFFFFF       call subprogram_aux
005CE7DA 59               pop ecx
005CE7DB 8945FC           mov [ebp-$04],eax
Run Code Online (Sandbox Code Playgroud)

即使启用了优化。

  • 看看其他编译器可以做什么,例如Win64编译器,会很有趣 (2认同)