Java optimization.

S

Serguei.Goumeniouk

Dear Experts,
I have the following lines in my codes to test a string:

String str = " ... some integer ...";
try {
int value = Integer.parseInt(str);
} catch (Exception ex) {
// This is not an integer
}

The variable "value" is not used anymore, so a clever compiler may
remove the try block.
In my Java environment the compiler does not "optimize" these lines
and everything works OK.
What I am worrying about is the following: "Are there any known Java
compilers which will optimize these codes?"

Regards,
Serguei.
 
M

Mark Thornton

Dear Experts,
I have the following lines in my codes to test a string:

String str = " ... some integer ...";
try {
int value = Integer.parseInt(str);
} catch (Exception ex) {
// This is not an integer
}

The variable "value" is not used anymore, so a clever compiler may
remove the try block.
In my Java environment the compiler does not "optimize" these lines
and everything works OK.
What I am worrying about is the following: "Are there any known Java
compilers which will optimize these codes?"

Regards,
Serguei.

Provided you have something more than a comment in the catch clause then
no legal implementation of Java could optimise the lines away.

Mark Thornton
 
J

Joshua Cranmer

Dear Experts,
I have the following lines in my codes to test a string:

String str = " ... some integer ...";
try {
int value = Integer.parseInt(str);
} catch (Exception ex) {
// This is not an integer
}

The variable "value" is not used anymore, so a clever compiler may
remove the try block.

But even though value may not be used, the action of Integer.parseInt
may have some side effects. Suppose str is set to be "dw23f~~"? The
value of str would have to be known (or limited in some form) and the
semantics of Integer.parseInt would have to be guaranteed to not throw
an exception.

One can argue that the call related to the assignment of the value
should be optimized as well, but some assignments for unused variables
may be purposeful in effect.

I can't see how the optimization would avoid violating the JLS in these
circumstances without requiring serious in-depth analysis of the code
and also keeping binary compatibility.

As a final note: a try-catch statement adds very little overhead when
not called, and the assignment is equally negligible in effect.
In my Java environment the compiler does not "optimize" these lines
and everything works OK.

The Sun Java compiler does not optimize because optimized code hampers
the ability of the JIT optimizer to optimize the code (although variable
propagation analysis probably doesn't have an impact).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top