Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Array bounds checking

Reply
Thread Tools

Array bounds checking

 
 
Chris
Guest
Posts: n/a
 
      07-05-2005
I have some super time-critical code, where I'm doing a lot of integer math
across very large arrays. Performance isn't as good as it could be. I'm
wondering if the fact that Java does bounds checking on every array access
could be slowing things up.

Question: is the bounds-checking performed in bytecode, or in native JVM
code? I'm thinking that if it's in bytecode, I could generate a little
custom code that turns it off for those time-critical methods.


 
Reply With Quote
 
 
 
 
Stefan Schulz
Guest
Posts: n/a
 
      07-05-2005
On Tue, 05 Jul 2005 14:25:56 -0500, Chris wrote:

> Question: is the bounds-checking performed in bytecode, or in native JVM
> code? I'm thinking that if it's in bytecode, I could generate a little
> custom code that turns it off for those time-critical methods.


It is done in the JVM itself.

--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"


 
Reply With Quote
 
 
 
 
Harald
Guest
Posts: n/a
 
      07-05-2005
"Chris" <anon> writes:

> Question: is the bounds-checking performed in bytecode, or in native JVM
> code? I'm thinking that if it's in bytecode, I could generate a little
> custom code that turns it off for those time-critical methods.


Run your program with

Java -Xprof

and it will most likely tell you that your code is *compiled*, and
that means native assembler code running.

Harald.

--
---------------------+---------------------------------------------
Harald Kirsch (@home)|
Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-05-2005
On Tue, 5 Jul 2005 14:25:56 -0500, "Chris" <anon> wrote or quoted :

>I have some super time-critical code, where I'm doing a lot of integer math
>across very large arrays. Performance isn't as good as it could be. I'm
>wondering if the fact that Java does bounds checking on every array access
>could be slowing things up.


It does not really do an explicit check. It just has to make sure you
don't go out of bounds. So for example

for ( i<0; i<n; i++ )

if n in a final constant, then it only needs check once before doing
any looping.

If n is a variable in may have to check i once per loop iteration, no
matter how many references to i there are.

Clever compilers (see http://mindprod.com/jgloss/jet.html)
can be clever about avoiding the work while still ensuring 100%
safety.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-05-2005
On Tue, 5 Jul 2005 14:25:56 -0500, "Chris" <anon> wrote or quoted :

>Question: is the bounds-checking performed in bytecode, or in native JVM
>code?


There are bytecode ops in the Java virtual machine that do array
operations. See the goldfish book for details. Those ops have built-in
bounds checking. There are no explicit checks in byte code. Of course
the implementation of the Java victual machine on any real machine is
free to avoid actually checking whenever it can prove to itself it
would be safe. This is true both of jit and native compilation, but
not interpreter.


See http://mindprod.com/jgloss/disassembler.html
http://mindprod.com/jgloss/jasm.html


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
ldv@mail.com
Guest
Posts: n/a
 
      07-06-2005
Chris wrote:
> I have some super time-critical code, where I'm doing a lot of integer math
> across very large arrays. Performance isn't as good as it could be. I'm
> wondering if the fact that Java does bounds checking on every array access
> could be slowing things up.
>
> Question: is the bounds-checking performed in bytecode, or in native JVM
> code? I'm thinking that if it's in bytecode, I could generate a little
> custom code that turns it off for those time-critical methods.


A native compiler (whether JIT or AOT) will remove the checks when it
is safe to do that. So you should write your code in a manner that
enables the compiler to remove checks. One rule of thumb is: "use 'for'
loops and do not change the loop variable anywhere inside the loop
body". I.e. your loops must be equivalent to the Pascal/Modula FOR
statement. Then, you better not call any methods from the loop body
except when you are sure they will be substituted inline by the
compiler. An example is final set/get methods.

Also, the bytecode supplied to the JVM must not be optimized. So use
the standard javac compiler to produce bytecode, and never ever use any
bytecode obfuscators/optimizers.

Finally, check out Excelsior JET and Excelsior's performance tuning
services:
http://www.excelsior-usa.com/jet.html
http://www.excelsior-usa.com/services30.html

The current version, Excelsior JET 3.7, is not very good at doing
floating point math, but for integer arrays it should be a killer.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the meaning of the array indexing brackets "[ ]" to do bounds checking and then ... Casey Hawthorne C Programming 15 11-04-2009 03:42 PM
To increase speed on manipulating big arrays in Java with minimal bounds checking, ... Casey Hawthorne Java 16 09-01-2005 06:33 AM
I thought that array bounds checking needed two comparisons; however, ... Casey Hawthorne Java 21 06-05-2004 08:04 PM
Bounds Checking? Julian Zhang C Programming 3 01-20-2004 11:56 PM
Macro argument bounds checking? Jim Cook C Programming 7 09-22-2003 02:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57