Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / September 2007

Tip: Looking for answers? Try searching our database.

Optimization question

Thread view: 
Christopher Diggins - 22 Sep 2007 22:55 GMT
Hi All,

Quick question about optimizing JVM bytecode. Suppose I have the
following Java code:

public static void main(String[] args) {
 int a[] = new int[42];
 for (int i=0; i < a.length; ++i)
   a[i] = 1;
 int sum = 0;
 for (int i=0; i < a.length; ++i)
    sum = sum + a[i] ;
  System.out.println(sum);
}

Would it be okay if I wrote an optimizer that pre-evaluated the code
and just generated byte-code that output the value 42? In other words,
did not even bother allocating the array, if it could avoid it? Would
it violate the Java virtual machine spec, or the language spec? If it
is okay, does anyone know of optimizers that do such a pre-
evaluation?

Any guidance would be much appreciated!

Christopher Diggins
http://www.cdiggins.com
Joshua Cranmer - 23 Sep 2007 00:01 GMT
> Hi All,
>
[quoted text clipped - 17 lines]
> is okay, does anyone know of optimizers that do such a pre-
> evaluation?

Optimization appears to be legal according to the JLS with a few caveats:

1. All constant-expression code is strictfp
2. Application of the commutative and associative properties is strictly
forbidden.
3. There's more restrictions, see §15 for the full scoop.

The Java VM does not, to my knowledge, prohibit any optimization; the
JVM actually does hefty optimization.

I believe that there is a flag in javac that causes it to optimize, but
Sun doesn't recommend it because (I think) they found that the JIT
optimizer worked better with the unoptimized code. I do not know if that
optimizer did constant propagation though.

Many optimizers (gcc included) do use constant propagation and constant
folding--it is very trivial optimization.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

glen herrmannsfeldt - 28 Sep 2007 02:04 GMT
> Quick question about optimizing JVM bytecode. Suppose I have the
> following Java code:

> public static void main(String[] args) {
>   int a[] = new int[42];
[quoted text clipped - 5 lines]
>    System.out.println(sum);
> }

> Would it be okay if I wrote an optimizer that pre-evaluated the code
> and just generated byte-code that output the value 42?

This is very common for optimizers for other languages.  My
understanding for Java is that the exception model limits some
optimizations that might otherwise be possible.   In this case
you can easily show that no exceptions will occur so it should
be legal.

The only one I can see that could possibly happen is that
yours would still work even if not enough memory was available.
Consider the slightly different:

public static void main(String[] args) {
  int a[] = new int[1000000000];
  for (int i=0; i < a.length; ++i)
    a[i] = 1;
  int sum = 0;
  for (int i=0; i < a.length; ++i)
     sum = sum + a[i] ;
   System.out.println(sum);
}

This would fail on most systems without optimization, but with
yours it would not fail.   In more complicated programs there is
interaction with other uses of memory.

-- glen


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.