Sagewire Logo

Optimization question

3 Message(s) by 3 Author(s) originally posted in java machine


From: Christopher Diggins Date:   Saturday, September 22, 2007
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);
}

wrote in message an optimizer that pre-evaluated the code
and just generated ~~~byte-code~~~ that output the value 42? In other words,
didn't even bother allocating the array, if it could avoid it?'d
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 direction would be much appreciated!

Christopher Diggins
http://www.cdiggins.com


From: Joshua Cranmer Date:   Saturday, September 22, 2007
wrote in message:
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);
}
wrote in message an optimizer that pre-evaluated the code
and just generated ~~~byte-code~~~ that output the value 42? In other words,
didn't even bother allocating the array, if it could avoid it?'d
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?



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 doesn't, 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 does not recommend it because (I think) they found that the JIT
optimizer worked better with the unoptimized code. I don't know if that
optimizer did constant propagation though.

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

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


From: glen herrmannsfeldt Date:   Thursday, September 27, 2007
wrote in message:

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);
}



wrote in message 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'd 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'd fail on most systems without optimization, but with
yours it wouldn't fail. In more complicated programs there is
interaction with other uses of memory.

-- glen



Next Message: Questions about JAVA VM spec


Blogs related to Optimization question

finpingvin I have a question lets say I have a table and I have it ...
which leads to better optimization (unless im completely wrong). hmm, not sure really, i haven't ever tested it. someone told me that APC "compiles" static includes into larger blobs. berry__ enormous thanks, you are the best ;P ...

Seo Web Design
Resolved Question: What is the best SEO SEM PPC Management Search engine optimization firm on the internet ?What is the best SEO SEM PPC Management Search engine optimization firm on the internet ?Please list the top 10 companies that ...

Java Faqs
Question How many types of JDBC Drivers are present and what are they? (JDBC) Answer There are 4 types of JDBC Drivers Type 1: JDBC-ODBC Bridge Driver Type 2: Native API Partly Java Driver Type 3: Network protocol Driver ...

*question* Take multiple results records based on a multiple ...
yes i installed java on ubuntu and FC cyth. I have a broken java app on my winbox which is version dependent. i am gonna try to use sqlite then. hwo does windows handle alternate installs of java? multiple subdirs ...

RIA Live Chat Transcript Available
Question: Hi, I`ve used Flex for a year now - I am still only really producing read-only apps - should I take it seriously for full apps and if so - with what framework/tools if I dont want to build a java middle tier? ...

Java Html Editor. Frequently Asked Linux Questions And Answers
Because it was written on a Java framework it is scalable and easy to integrate, Bober says. Marketing Optimization SAS has. In addition, a digital content repository has been added to complement the in-line content editor that enables ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional