gpt4 book ai didi

c# - 'real'语句存在的 "GOTO"目的是性能提升吗?

转载 作者:行者123 更新时间:2023-11-30 19:32:47 24 4
gpt4 key购买 nike

今天我打开了一个 Microsoft 的 .Net regex 实现,它让我震惊,这可能是像 C# 这样的语言中存在 goto 语句的唯一原因(主要强调“开发人员的清晰度”概念,其中 goto 似乎根本没有位置),并且可能也是微软将它用于 CLR 库实现的唯一原因 - 以提高性能。我还记得在 System.Web.UI.dll 中的 Microsoft ASP.NET 页面/控件呈现中也看到过类似的优化。这会是一个有效的(或记录在案的?)假设吗?您是否见过 Microsoft 在除缩短代码执行时间之外的任何其他情况下使用它?谢谢。

下面是 Microsoft 自己的正则表达式实现的摘录(不打算详细检查,只是扫描它),他们再次广泛使用 goto 似乎只是为了提高性能:

public override void Go() 
{
int num4;
int num5;
string runtext = base.runtext;
int runtextstart = base.runtextstart;
int runtextbeg = base.runtextbeg;
int runtextend = base.runtextend;
int runtextpos = base.runtextpos;
int[] runtrack = base.runtrack;
int runtrackpos = base.runtrackpos;
int[] runstack = base.runstack;
int runstackpos = base.runstackpos;
runtrack[--runtrackpos] = runtextpos;
runtrack[--runtrackpos] = 0;
runstack[--runstackpos] = runtextpos;
runtrack[--runtrackpos] = 1;
if ((((runtextpos != base.runtextstart) ||
(4 > (runtextend - runtextpos))) ||
((runtext[runtextpos] != '<') ||
(runtext[runtextpos + 1] != '%'))) ||
((runtext[runtextpos + 2] != '-') ||
(runtext[runtextpos + 3] != '-')))
{
goto Label_02F8;
}
runtextpos += 4;
runstack[--runstackpos] = -1;
runtrack[--runtrackpos] = 1;
goto Label_0213;
Label_0161:
if (num5 > num4)
{
runtrack[--runtrackpos] = (num5 - num4) - 1;
runtrack[--runtrackpos] = runtextpos - 1;
runtrack[--runtrackpos] = 2;
}
Label_0194:
num4 = runstack[runstackpos++];
this.Capture(2, num4, runtextpos);
runtrack[--runtrackpos] = num4;
runtrack[--runtrackpos] = 3;
if (runtextpos >= runtextend)
{
goto Label_02F8;
}
runtextpos++;
if (runtext[runtextpos] != '-')
{
goto Label_02F8;
}
num4 = runstack[runstackpos++];
this.Capture(1, num4, runtextpos);
runtrack[--runtrackpos] = num4;
runtrack[--runtrackpos] = 3;
Label_0213:
if (num4 != -1)
{
runtrack[--runtrackpos] = num4;
}
else
{
runtrack[--runtrackpos] = runtextpos;
}
if ((num4 = runstack[runstackpos++]) != runtextpos)
{
runtrack[--runtrackpos] = runtextpos;
runtrack[--runtrackpos] = 4;
}
else
{
runstack[--runstackpos] = num4;
runtrack[--runtrackpos] = 5;
}
if (((3 > (runtextend - runtextpos)) ||
(runtext[runtextpos] != '-')) ||
((runtext[runtextpos + 1] != '%') ||
(runtext[runtextpos + 2] != '>')))
{
goto Label_02F8;
}
runtextpos += 3;
num4 = runstack[runstackpos++];
this.Capture(0, num4, runtextpos);
runtrack[--runtrackpos] = num4;
runtrack[--runtrackpos] = 3;
Label_02EF:
base.runtextpos = runtextpos;
return;
Label_02F8:
base.runtrackpos = runtrackpos;
base.runstackpos = runstackpos;
this.EnsureStorage();
runtrackpos = base.runtrackpos;
runstackpos = base.runstackpos;
runtrack = base.runtrack;
runstack = base.runstack;
switch (runtrack[runtrackpos++])
{
case 1:
runstackpos++;
goto Label_02F8;

case 2:
runtextpos = runtrack[runtrackpos++];
num4 = runtrack[runtrackpos++];
if (num4 > 0)
{
runtrack[--runtrackpos] = num4 - 1;
runtrack[--runtrackpos] = runtextpos - 1;
runtrack[--runtrackpos] = 2;
}
goto Label_0194;

case 3:
runstack[--runstackpos] = runtrack[runtrackpos++];
this.Uncapture();
goto Label_02F8;

case 4:
runtextpos = runtrack[runtrackpos++];
runstack[--runstackpos] = runtextpos;
runtrack[--runtrackpos] = 5;
if ((runtrackpos > 40) && (runstackpos > 30))
{
runstack[--runstackpos] = runtextpos;
runtrack[--runtrackpos] = 1;
runstack[--runstackpos] = runtextpos;
runtrack[--runtrackpos] = 1;
num4 = (num5 = runtextend - runtextpos) + 1;
do
{
if (--num4 <= 0)
{
goto Label_0161;
}
runtextpos++;
}
while (runtext[runtextpos] != '-');
runtextpos--;
goto Label_0161;
}
runtrack[--runtrackpos] = 6;
goto Label_02F8;

case 5:
runstack[runstackpos] = runtrack[runtrackpos++];
goto Label_02F8;
}
runtextpos = runtrack[runtrackpos++];
goto Label_02EF;
}

最佳答案

我看到的一个用例是自动生成的代码。高级控制流构造对我们人类来说很容易,但作为代码生成算法的输出不一定方便。

你的代码反编译了吗?在那种情况下,它可能只是以反编译器不使用 goto 就无法处理的方式编写的。在 IL 级别,所有控制流结构都被转换为 goto,并且反编译器试图猜测它们是什么。在复杂的情况下,它可能找不到使用高级构造来表示它的好方法,因此求助于 goto

另一个用例是状态机。您有多个状态,在每个状态之后,它可以使用 goto 语句转换到一个新状态。解析器是状态机的常见应用。

关于c# - 'real'语句存在的 "GOTO"目的是性能提升吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833039/

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