Mar*_*ade 31 c# compiler-construction algorithm iterator state-machine
我想自己弄明白,但我想知道将带有yield语句的函数转换为枚举器状态机的算法是什么?例如,C#如何转变:
IEnumerator<string> strings(IEnumerable<string> args)
{ IEnumerator<string> enumerator2 = getAnotherEnumerator();
foreach(var arg in arg)
{ enumerator2.MoveNext();
yield return arg+enumerator.Current;
}
}
Run Code Online (Sandbox Code Playgroud)
进入这个:
bool MoveNext()
{ switch (this.state)
{
case 0:
this.state = -1;
this.enumerator2 = getAnotherEnumerator();
this.argsEnumerator = this.args.GetEnumerator();
this.state = 1;
while (this.argsEnumerator.MoveNext())
{
this.arg = this.argsEnumerator.Current;
this.enumerator2.MoveNext();
this.current = this.arg + this.enumerator2.Current;
this.state = 2;
return true;
state1:
this.state = 1;
}
this.state = -1;
if (this.argsEnumerator != null) this.argsEnumerator.Dispose();
break;
case 2:
goto state1;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
当然,根据原始代码,结果可能完全不同.
Sco*_*ski 59
您正在查看的特定代码示例涉及一系列转换.请注意,这是算法的近似描述.编译器使用的实际名称及其生成的确切代码可能不同.然而,想法是一样的.
第一个转换是"foreach"转换,它转换了这个代码:
foreach (var x in y)
{
//body
}
Run Code Online (Sandbox Code Playgroud)
进入这段代码:
var enumerator = y.GetEnumerator();
while (enumerator.MoveNext())
{
var x = enumerator.Current;
//body
}
if (y != null)
{
enumerator.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
第二个转换在函数体中查找所有yield return语句,为每个语句赋值(状态值),并在yield之后创建一个"goto标签".
第三个转换将方法体中的所有局部变量和函数参数提升到一个名为闭包的对象中.
鉴于您的示例中的代码,它看起来类似于:
class ClosureEnumerable : IEnumerable<string>
{
private IEnumerable<string> args;
private ClassType originalThis;
public ClosureEnumerator(ClassType origThis, IEnumerable<string> args)
{
this.args = args;
this.origianlThis = origThis;
}
public IEnumerator<string> GetEnumerator()
{
return new Closure(origThis, args);
}
}
class Closure : IEnumerator<string>
{
public Closure(ClassType originalThis, IEnumerable<string> args)
{
state = 0;
this.args = args;
this.originalThis = originalThis;
}
private IEnumerable<string> args;
private IEnumerator<string> enumerator2;
private IEnumerator<string> argEnumerator;
//- Here ClassType is the type of the object that contained the method
// This may be optimized away if the method does not access any
// class members
private ClassType originalThis;
//This holds the state value.
private int state;
//The current value to return
private string currentValue;
public string Current
{
get
{
return currentValue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后将方法体从原始方法移动到名为MoveNext的"Closure"内部的方法,该方法返回bool,并实现IEnumerable.MoveNext.对任何本地人的任何访问都通过"this"进行路由,对任何类成员的任何访问都通过this.originalThis进行路由.
任何"收益率回报expr"都被翻译成:
currentValue = expr;
state = //the state number of the yield statement;
return true;
Run Code Online (Sandbox Code Playgroud)
任何yield break语句都被翻译成:
state = -1;
return false;
Run Code Online (Sandbox Code Playgroud)
函数末尾有一个"隐式"yield break语句.然后在过程开始时引入switch语句,该语句查看状态编号并跳转到关联的标签.
然后将原始方法转换为以下内容:
IEnumerator<string> strings(IEnumerable<string> args)
{
return new ClosureEnumerable(this,args);
}
Run Code Online (Sandbox Code Playgroud)
将方法的状态全部推入对象并且MoveNext方法使用switch语句/状态变量这一事实允许迭代器的行为就像控件被传递回最后一次"yield return"之后的那一点一样"下次调用"MoveNext"语句.
然而,重要的是要指出,C#编译器使用的转换不是执行此操作的最佳方法.当尝试使用递归算法的"yield"时,它的性能很差.有一篇好文章概述了一种更好的方法:
http://research.microsoft.com/en-us/projects/specsharp/iterators.pdf
如果你还没读过它,那值得一读.
雷蒙德陈回答这个问题; http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx
(编辑指向系列的第1部分,而不是第4部分)