7

I want to make sure that my web application is performance wise and will not make any performance problems. When I searched, I found that one of the most common problems related to the performance issue is the "infinite loops" problem.

I want to ask:

Where should I begin to check that my code never causes infinite loops?

Are there any articles, advices, instructions, examples? I will be grateful.

ex:

May this code cause an infinite loop?

public static IEnumerable<SubjectNode> ReadSubjectNodes(string filePath)
{
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
    using (XmlReader xrdr = new XmlTextReader(fs))
        while (xrdr.Read())
            if (xrdr.NodeType == XmlNodeType.Element && xrdr.LocalName == "subject")
                yield return new SubjectNode(xrdr.GetAttribute("id"), xrdr.GetAttribute("name"), xrdr.GetAttribute("short"));
}

Thanks in advance

Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • 1
    Maybe put some huge upper limit i.e. `int ReadCount = 100000; while(xrdr.Read() && ReadCount-- > 0)` – George Duckett Jan 16 '12 at 12:25
  • 6
    This is most likely not what you're looking for, but the general problem of finding out whether a program enters an infinite loop or finishes (["halting problem"](http://en.wikipedia.org/wiki/Halting_problem)) is unsolvable. – svick Jan 16 '12 at 12:27
  • What exactly is the code doing? Are you going to output this to the user or is this a behind-the-scenes execution? If you are outputting results to a user you could get the first 10 -> display the results -> then have a navigation to go from one page to the other each time only iterating the next 10 files. – Carl Winder Jan 16 '12 at 12:29

4 Answers4

7

Well, AFAIK that code won't cause an infinite loop - it is not recursive, and an XmlReader from the file-system will eventually run out of data. It might be long running, though. In some cases, adding a sanity check may help - this could be a counter, or a check against the time. For example:

public static IEnumerable<SubjectNode> ReadSubjectNodes(string filePath)
{
    int safetyCheck = 10000;
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
    using (XmlReader xrdr = new XmlTextReader(fs))
        while (xrdr.Read()) {
            if(--safetyCheck == 0) throw new SomeTypeOfException();
            ... more code
        }
    ... more code
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

There is no way to deterministically pre-determine whether a loop will run infinite times, except for the ones which have a compile-time defined number of iterations and don't alter the iteration counter in their body.

Just write good code which is not prone to infinite looping: inspect the conditions inside the while loops and think about a case in which they might cause infinite looping. That's it.

gd1
  • 11,300
  • 7
  • 49
  • 88
2

The code you have posted here has an exit condition: When the XmlTextReader reaches the end of its stream. Therefore, it's not an infinite loop.

That's really what you need to do to prevent these: make sure every loop has a condition which will cause it to exit. It's much easier to do when writing the code than reviewing it later.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
2

Infinate loops occur when you are looping until a parameter is met ( a while loop ), but it is possible that the while clause is never satisfied to exit the loop. And hence it goes on forever.

It is also possible to cause a recursive loop, by where you call a function from within itself (or similar), where it will constantly call up a function, but never get closer to exiting.

while (xrdr.Read()), is fine as long as .Read() is invoking the moving to the next element automatically. At which, as your xrdr is not an infinite source, it will eventually reach the end and exit.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154