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.
The assembly produced by this code is a few MB large.
yield returnis 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).