Re: Magic Number's dangerous?
- From: Roedy Green <my_email_is_posted_on_my_website@xxxxxxxxxxxxxx>
- Date: Sat, 26 Nov 2005 19:40:19 GMT
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.
.
- References:
- [Newbie Question] Magic Number's dangerous?
- From: seungwoo . yu
- Re: Magic Number's dangerous?
- From: clarke . jonathan
- Re: Magic Number's dangerous?
- From: Mike Gaab
- Re: Magic Number's dangerous?
- From: zero
- Re: Magic Number's dangerous?
- From: Thomas Hawtin
- Re: Magic Number's dangerous?
- From: zero
- Re: Magic Number's dangerous?
- From: Chris Smith
- [Newbie Question] Magic Number's dangerous?
- Prev by Date: Re: Bit Manipulation in Java
- Next by Date: Re: attn roedy
- Previous by thread: Re: Magic Number's dangerous?
- Next by thread: Re: Magic Number's dangerous?
- Index(es):