实现C#yield语句的算法 [英] Algorithm for implementing C# yield statement

查看:224
本文介绍了实现C#yield语句的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自己搞清楚,但我想知道大约什么算法用于将yield语句转换为枚举器的状态机?例如C#如何转换:

I'd love to figure it out myself but I was wondering roughly what's the algorithm for converting a function with yield statements into a state machine for an enumerator? For example how does C# turn this:

IEnumerator<string> strings(IEnumerable<string> args)
 { IEnumerator<string> enumerator2 = getAnotherEnumerator();     
   foreach(var arg in arg) 
    { enumerator2.MoveNext();
      yield return arg+enumerator.Current;
    } 
 }

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;
}

当然,根据原始代码,结果可能完全不同。 p>

Of course the result can be completely different depending on the original code.

推荐答案

您正在查看的特定代码示例涉及一系列变换。
请注意,这是算法的近似描述。编译器使用的实际名称和它生成的确切代码可能不同。然而,想法是一样的。

The particular code sample you are looking at involves a series of transformations. Please note that this is an approximate description of the algorithm. The actual names used by the compiler, and the exact code it generates may be different. Then idea is the same, however.

第一个转换是foreach转换,转换这个代码:

The first transformation is the "foreach" transformation, which transforms this code:

foreach (var x in y)
{
   //body
}

到此代码中:

var enumerator = y.GetEnumerator();
while (enumerator.MoveNext())
{
    var x = enumerator.Current;
    //body
}

if (y != null)
{
    enumerator.Dispose();
}

第二个转换在函数体中找到所有yield return语句,数字(每个状态值),并在收益之后立即创建goto标签。

The second transformation finds all the yield return statements in the function body, assigns a number to each (a state value), and creates a "goto label" right after the yield.

第三个转换将方法体中的所有局部变量和函数参数提升为一个称为闭包的对象。

The third transformation lifts all the local variables and function arguments in the method body into an object called a closure.

在您的示例中,代码如下:

Given the code in your example, that would look similar to this:

 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;
        }
    }
}

从原来的方法,到一个方法里面的Closure,称为MoveNext,它返回一个bool,并实现IEnumerable.MoveNext。
任何访问任何本地的都是通过this路由的,任何类成员的访问都通过this.originalThis路由。

The method body is then moved from the original method, to a method inside "Closure" called MoveNext, which returns a bool, and implements IEnumerable.MoveNext. Any access to any locals is routed through "this", and any access to any class members is routed through this.originalThis.

任何yield return expr 翻译成:

Any "yield return expr" is translated into:

currentValue = expr;
state = //the state number of the yield statement;
return true;

任何yield break语句都被翻译成:

Any yield break statement is translated into:

state = -1;
return false;

在函数结尾处有一个隐式yield break语句。
然后在过程开始时引入switch语句,查看状态数并跳转到相关标签。

There is an "implicit" yield break statement at the end of the function. A switch statement is then introduced at the beginning of the procedure that looks at the state number and jumps to the associated label.

原始方法将翻译成以下形式:

The original method is then translated into something like this:

IEnumerator<string> strings(IEnumerable<string> args)
{
   return new ClosureEnumerable(this,args);
}

方法的状态全部推入对象, MoveNext方法使用switch语句/状态变量是什么允许迭代器的行为,如果控制被传递回紧接在最后一个yield return语句之后,下一次MoveNext被调用。

The fact that the state of the method is all pushed into an object and that the MoveNext method uses a switch statement / state variable is what allows the iterator to behave as if control is being passed back to the point immediately after the last "yield return" statement the next time "MoveNext" is called.

然而,重要的是要指出,C#编译器使用的转换不是最好的方法。当尝试使用递归算法使用yield时,它的性能很差。有一个很好的文章,概述了一个更好的方法来做到这一点:

It is important to point out, however, that the transformation used by the C# compiler is not the best way to do this. It suffers from poor performance when trying to use "yield" with recursive algorithms. There is a good paper that outlines a better way to do this here:

http://research.microsoft.com/en-us/projects/specsharp/iterators.pdf

如果你还没有阅读,这是值得阅读的。

It's worth a read if you haven't read it yet.

这篇关于实现C#yield语句的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆