反射器错误或优化?
长话短说:我在 System.Security.Util.Tokenizer 类上使用了 Reflector,其中有大量 goto 语句。
这是一个简短的示例片段:
Label_0026:
if (this._inSavedCharacter != -1)
{
num = this._inSavedCharacter;
this._inSavedCharacter = -1;
}
else
{
switch (this._inTokenSource)
{
case TokenSource.UnicodeByteArray:
if ((this._inIndex + 1) < this._inSize)
{
break;
}
stream.AddToken(-1);
return;
case TokenSource.UTF8ByteArray:
if (this._inIndex < this._inSize)
{
goto Label_00CF;
}
stream.AddToken(-1);
return;
case TokenSource.ASCIIByteArray:
if (this._inIndex < this._inSize)
{
goto Label_023C;
}
stream.AddToken(-1);
return;
case TokenSource.CharArray:
if (this._inIndex < this._inSize)
{
goto Label_0272;
}
stream.AddToken(-1);
return;
case TokenSource.String:
if (this._inIndex < this._inSize)
{
goto Label_02A8;
}
stream.AddToken(-1);
return;
case TokenSource.NestedStrings:
if (this._inNestedSize == 0)
{
goto Label_030D;
}
if (this._inNestedIndex >= this._inNestedSize)
{
goto Label_0306;
}
num = this._inNestedString[this._inNestedIndex++];
goto Label_0402;
default:
num = this._inTokenReader.Read();
if (num == -1)
{
stream.AddToken(-1);
return;
}
goto Label_0402;
}
num = (this._inBytes[this._inIndex + 1] << 8) + this._inBytes[this._inIndex];
this._inIndex += 2;
}
goto Label_0402;
Label_00CF:
num = this._inBytes[this._inIndex++];
if ((num & 0x80) != 0)
{
switch (((num & 240) >> 4))
{
case 8:
case 9:
case 10:
case 11:
throw new XmlSyntaxException(this.LineNo);
case 12:
case 13:
num &= 0x1f;
num3 = 2;
break;
case 14:
num &= 15;
num3 = 3;
break;
case 15:
throw new XmlSyntaxException(this.LineNo);
}
if (this._inIndex >= this._inSize)
{
throw new XmlSyntaxException(this.LineNo, Environment.GetResourceString("XMLSyntax_UnexpectedEndOfFile"));
}
byte num2 = this._inBytes[this._inIndex++];
if ((num2 & 0xc0) != 0x80)
{
throw new XmlSyntaxException(this.LineNo);
}
num = (num << 6) | (num2 & 0x3f);
if (num3 != 2)
{
if (this._inIndex >= this._inSize)
{
throw new XmlSyntaxException(this.LineNo, Environment.GetResourceString("XMLSyntax_UnexpectedEndOfFile"));
}
num2 = this._inBytes[this._inIndex++];
if ((num2 & 0xc0) != 0x80)
{
throw new XmlSyntaxException(this.LineNo);
}
num = (num << 6) | (num2 & 0x3f);
}
}
goto Label_0402;
Label_023C:
num = this._inBytes[this._inIndex++];
goto Label_0402;
Label_0272:
num = this._inChars[this._inIndex++];
goto Label_0402;
Label_02A8:
num = this._inString[this._inIndex++];
goto Label_0402;
Label_0306:
this._inNestedSize = 0;
我本质上想知道这个类是如何工作的,但是 goto 的数量使得它变得不可能。可以说像 Tokenizer 类这样的东西需要大量优化,所以我的问题是:Reflector 是否出错了,或者 goto
是对该类的优化?
Long story short: I used reflector on the System.Security.Util.Tokenizer class, and there's loads of goto statements in there.
Here's a brief example snippet:
Label_0026:
if (this._inSavedCharacter != -1)
{
num = this._inSavedCharacter;
this._inSavedCharacter = -1;
}
else
{
switch (this._inTokenSource)
{
case TokenSource.UnicodeByteArray:
if ((this._inIndex + 1) < this._inSize)
{
break;
}
stream.AddToken(-1);
return;
case TokenSource.UTF8ByteArray:
if (this._inIndex < this._inSize)
{
goto Label_00CF;
}
stream.AddToken(-1);
return;
case TokenSource.ASCIIByteArray:
if (this._inIndex < this._inSize)
{
goto Label_023C;
}
stream.AddToken(-1);
return;
case TokenSource.CharArray:
if (this._inIndex < this._inSize)
{
goto Label_0272;
}
stream.AddToken(-1);
return;
case TokenSource.String:
if (this._inIndex < this._inSize)
{
goto Label_02A8;
}
stream.AddToken(-1);
return;
case TokenSource.NestedStrings:
if (this._inNestedSize == 0)
{
goto Label_030D;
}
if (this._inNestedIndex >= this._inNestedSize)
{
goto Label_0306;
}
num = this._inNestedString[this._inNestedIndex++];
goto Label_0402;
default:
num = this._inTokenReader.Read();
if (num == -1)
{
stream.AddToken(-1);
return;
}
goto Label_0402;
}
num = (this._inBytes[this._inIndex + 1] << 8) + this._inBytes[this._inIndex];
this._inIndex += 2;
}
goto Label_0402;
Label_00CF:
num = this._inBytes[this._inIndex++];
if ((num & 0x80) != 0)
{
switch (((num & 240) >> 4))
{
case 8:
case 9:
case 10:
case 11:
throw new XmlSyntaxException(this.LineNo);
case 12:
case 13:
num &= 0x1f;
num3 = 2;
break;
case 14:
num &= 15;
num3 = 3;
break;
case 15:
throw new XmlSyntaxException(this.LineNo);
}
if (this._inIndex >= this._inSize)
{
throw new XmlSyntaxException(this.LineNo, Environment.GetResourceString("XMLSyntax_UnexpectedEndOfFile"));
}
byte num2 = this._inBytes[this._inIndex++];
if ((num2 & 0xc0) != 0x80)
{
throw new XmlSyntaxException(this.LineNo);
}
num = (num << 6) | (num2 & 0x3f);
if (num3 != 2)
{
if (this._inIndex >= this._inSize)
{
throw new XmlSyntaxException(this.LineNo, Environment.GetResourceString("XMLSyntax_UnexpectedEndOfFile"));
}
num2 = this._inBytes[this._inIndex++];
if ((num2 & 0xc0) != 0x80)
{
throw new XmlSyntaxException(this.LineNo);
}
num = (num << 6) | (num2 & 0x3f);
}
}
goto Label_0402;
Label_023C:
num = this._inBytes[this._inIndex++];
goto Label_0402;
Label_0272:
num = this._inChars[this._inIndex++];
goto Label_0402;
Label_02A8:
num = this._inString[this._inIndex++];
goto Label_0402;
Label_0306:
this._inNestedSize = 0;
I essentially wanted to know how the class worked, but the number of goto's makes it impossible. Arguably something like a Tokenizer class needs to be heavily optimised, so my question is: is Reflector getting it wrong, or is goto
an optimisation for this class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 Reflector 无法确定原始循环结构。
有趣的是(以及为什么有时会出现这种情况),没有用于
while
和for
循环以及break
之类的 IL 操作码并继续
。等效的 IL 只是if
测试与跳转(即 gotos)的结合。It looks like Reflector is unable to determine the original loop structure.
As a point of interest (and why this is sometimes the case) there aren't IL opcodes for things like
while
andfor
loops andbreak
andcontinue
. The equivalent IL is justif
tests combined with jumps (i.e. gotos).