Re: Magic Number's dangerous?



On Sat, 26 Nov 2005 09:26:15 -0700, Chris Smith <cdsmith@xxxxxxx>
wrote, quoted or indirectly quoted someone who said :

>The overhead for declaring an enumerated type for the seven days of the
>week cannot POSSIBLY be significant enough to merit any thought. If it
>were, then Sun would have failed miserably and it would be the buzz of
>the Java world by now.

I have done a fair bit of staring at decompiled enum code to figure
out how it works.

The crucial problem with type-safe enum kludges is getting a decently
fast SWITCH. The Java enum implementors went to considerable
shenanigans to implement the switch with a plain old jump table using
a fast tableswitch JVM instruction instead of a lookupswitch.

It looks like this to ensure dense ints even for very large enums.
// use an ordinary int switch, using pre-mapped ordinals to sort the
switch.
switch ( $switchMap[ breed.ordinal() ] )
{
case 1: // DASCHUND
return true;

case 2: // DALMATIAN
case 3: // LABRADOR
default:
return false;
}


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
.