B
Ben Phillips
Daniel said:As for the infinite loop, you might be able to detect that in another
manor. If you can take a state snapshot at certain intervals, you can
check each state against all previous state snapshots. If the state
is completely unchanged, then it is in an infinite loop. This may
not be feasible depending on the size of your state, but you might be
able to get away with a maximum cycle size.
You can detect cycles of any size without a max-cycle-size-proportional
increase in memory usage or CPU usage. Run two copies side by side, one
faster than the other, and compare their states periodically. If they
have identical states there's a loop. E.g. do two instructions for one,
one for the other, compare. Two for the one, one for the other, compare.
Etc. A cycle of length n will be detected n instructions after it's
entered, so short cycles will be detected very quickly, but long ones
will still be detected eventually. Cost is to double the memory usage
and a bit more than double the CPU usage, but it gets you arbitrarily
large cycles detected.