> I was looking into array bounds checks, and what I found via Google
> indicated that hotspot leaves in array bounds checks as there was only a
> minor performance improvement found in practice.  This lead me to wonder
> if there is a faster way to do the code since I would be doing lots of
> array accesses, each with a bounds check.

Apropos avoiding array bounds check, you could also venture into how
NIO is implemented and get a bit more low-level. Something along the
line of this (cooked up UDP packet decoding as proof-of-concept):

byte[] datagram = new byte[]{   0x50, 0x00, (byte)0xbb, 0x1,
                                0xC, 0x0, 0x0, 0x0,
                                0x74, 0x65, 0x73, 0x74};

Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
Unsafe $ = (Unsafe) field.get(null);

long shortByteSize = Short.SIZE >> 3;
long offset = $.arrayBaseOffset(byte[].class);

short source = $.getShort(datagram, offset );
short dest = $.getShort(datagram, offset += shortByteSize);
short length = $.getShort(datagram, offset += shortByteSize);
short checksum = $.getShort(datagram, offset += shortByteSize);
String payload = new String( copyOfRange(datagram, UDP_HEADER_SIZE,
length) );

out.println("UDP packed content");
out.println("Source: " + source);
out.println("Destination: " + dest);
out.println("Length: " + length);
out.println("Checksum: " + checksum);
out.println("Payload: " +payload);

It's hard to know without your benchmarking suite, but it strikes me
that the above, though unsafe, has a good chance of mapping to
efficient native code.

/Casper

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to