[Mono-list] [Off-topic] "if else" or "switch" ?

Mike Welham mike@digitalnova.co.za
Sat, 16 Oct 2004 11:36:30 +0200

> I guess the entire idea behind the .NET is that the compiler don't have
> to deal with choices like switch vs if -- the runtime is going to take
> care of it. Using switch in the IL should be just a hint for the
> runtime. So there is nothing to change in the mcs. The runtime is what
> should be fixed, if anything.

I agree with this, but only up to a point. The JIT is very time-constrained,
and if the high-level compiler can perform high-level optimizations it
should. (When I say high-level optimizations I mean things like dead-code
elimination or tail-call optimization.)

On the discussion of switches, a good example would be a switch against a
number of strings. If the compiler maps this to IL naively, it would end up
being a very time-consuming operation with a number of string comparisons on
each pass through the statement. If, however, the compiler were to map it to
a hash table lookup, it would become a very cheap constant-time operation.

I think there are a wide range of (runtime agnostic) optimizations that
belong in .NET compilers, because they're just not feasible to spot and
perform at JIT time.