Ian Mercer
Ian Mercer

Reputation: 39277

Explain this odd behavior with IEnumerable / yield

Take a guess ... how long will this program take to produce the very first output when i == 0? It should be instant, right? And through lazy evaluation of the yield it should produce output in rapid succession after that, right?

static void Main(string[] args)
{
   Stopwatch stopwatch = Stopwatch.StartNew();
   int i = 0;
   foreach (var item in massiveYieldStatement())
   {
        if (i++ % 10000 == 0) 
           Console.WriteLine(stopwatch.ElapsedMilliseconds / 1000);
   }
   Console.ReadKey();
}

static IEnumerable<string> massiveYieldStatement()
{
   yield return "a"; 
   yield return "a";

   .. repeat 200,000 times !!

   yield return "a";
}

But it doesn't! It sits there with no output for between 4 and 21 minutes and then completes quickly - under 60ms in one case! During those minutes, 100% of one core's worth of CPU is used and memory usage grows. In the actual scenario where I encountered this a Stackoverflow exception is thrown before the first iteration even happens! I have tried it in debug mode in Visual Studio and in release mode from the command prompt. I have tried it on Windows 7 x64 and Windows Server 2008 R2 x64.

Can anyone explain what's happening here? Does it repro for you?

NOTE: This is not the real code: the real code has far fewer yield statements but is much more complex. This is just the simplest repro.

Upvotes: 4

Views: 266

Answers (2)

esskar
esskar

Reputation: 10950

the problem is not the yield, but the function that returns the 200K yields (btw. 100K lines already slowed down my VS). It needs to be evaluated and generate a state class new every time you do your first MoveNext() on the IEnumerator returned from IEnumerable<string>.GetEnumerator.

static IEnumerable<string> massiveYieldStatement()
{
    for(int i = 0; i < 200000; ++i)
        yield return "a";
}

runs fast as expected since evaluation is fast.

Upvotes: 2

jeroenh
jeroenh

Reputation: 26782

The assembly produced by this code is a few MB large. yield return is a special beast, in that it looks deceptively simple but the C# compiler actually generates a class ('state machine') to implement the massiveYieldStatement method. I'm pretty sure that you're waiting for the JIT compiler to compile the MoveNext() method of that class (you can verify that with ildasm: if you try to open the MoveNext() method it takes a lot of time as well).

Upvotes: 4

Related Questions