>>>>"Sridhar" == Sridhar Ayengar <ploopster at gmail.com>
writes:
Sridhar> But I wasn't talking about efficiency, I was talking about
Sridhar> cleanliness. Hand-optimized iterative assembler is some of
Sridhar> the most efficient code I can think of, but a lot of the
Sridhar> time, it tends not to be very clean.
Sridhar> The right tool for the right job, and all.
Right. But part of the "right job" is the error cases. On a machine
with VM and lots of memory and/or a large page file, and
auto-expanding stacks, recursion is great.
In an embedded system, or in the kernel, where the stack is small and
not expandable, recursion is very dangerous. You will often end up
with unrealiable systems with hard to track bugs. It will turn out
that the stack overflowed and smashed some other bits, and you'll have
a very hard time finding the bug. And since the amount of stack space
used (and available) is hard to predict and will depend on what the
compiler happens to be doing in that version, up front range checks
are difficult.
Conversely, an iterative routine that uses an explicitly allocated
context array is slightly more wordy, but massively safer. And with a
small amount of care you can make it look a whole lot like the
recursive solution, but with explicit stacking of state in an explicit
state vector.
I think you're agreeing with me here.
Write clean code when the code needs to be clean. Write efficient code
when the code needs to be efficient.
Most people overestimate the former and underestimate the latter, of course.
Peace... Sridhar