Hi everyone,
I have some performance critical code and was wondering whether there is
some way to emulate C++ templates in order to tell the Java compiler to
optimize it somehow.
The following is a much simplified example, but the principle is the
same. Consider a class
class Vec {
public Vec(int dim) {
this.dim = dim;
this.x = new double[dim];
}
private int dim;
public double x[];
public void axpy(double alpha, Vec b) {
for (int i = 0; i < dim; i++)
this.x[i] += alpha * b.x[i];
}
}
Suppose now that the dimension is defined once in the program (possibly
as a static final variable) and then all instances of Vec have the same
dimension (say dim = 3). In this case, there should be some way for the
compiler to understand this, and unroll and inline the axpy() function.
Is there any way to achieve this in Java?
What if I declared dim as static final in the class Vec? If I would
hard-code the dimension so-to-speak?
Many thanks,
Christoph
Stefan Ram - 11 Apr 2007 21:25 GMT
christoph.ortner@comlab.ox.ac.uk did not write:
|class Vec
|{ public Vec( final int dim ){ this.dim = dim; this.x = new double[ dim ]; }
| public void add( final double alpha, final Vec b )
| { for( int i = 0; i < dim; ++i )this.x[ i ]+= alpha * b.x[ i ]; }
| final private int dim; public double x[]; }
|Is there any way to inline »add« this in Java?
No - Java does not have inlining.
Specific Java implementations might have. See for example the
recent discussion
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103956
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6533165
These discussions also provide hints about making inlining happen.
See also
http://java.sun.com/docs/hotspot/HotSpotFAQ.html
But do not invest any work unless you have /measured/
the most obvious straight-forward readable implementation
to be too slow.
Be aware that any optimization might result in worse
performance on any other platform and version than the one
optimized for, might introduce bugs or raise maintenance
costs.
>emulate C++ templates
Hotspot already is quite smart and can outperform C++ in
some cases because it uses information obtained at runtime.
Fixing »dim« to a compile-time constant might have no or only
a very small influence on the speed of the execution of the
methods. You might like to measure this in the target
environment.
Stefan Ram - 11 Apr 2007 21:27 GMT
Supersedes: <optimization-20070411221157@ram.dialup.fu-berlin.de>
christoph.ortner@comlab.ox.ac.uk did not write:
|class Vec
|{ public Vec( final int dim ){ this.dim = dim; this.x = new double[ dim ]; }
| public void add( final double alpha, final Vec b )
| { for( int i = 0; i < dim; ++i )this.x[ i ]+= alpha * b.x[ i ]; }
| final private int dim; public double x[]; }
|Is there any way to inline »add« in Java?
No - Java does not have inlining.
Specific Java implementations might have. See for example the
recent discussion
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103956
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6533165
These discussions also provide hints about making inlining happen.
See also
http://java.sun.com/docs/hotspot/HotSpotFAQ.html
But do not invest any work unless you have /measured/
the most obvious straight-forward readable implementation
to be too slow.
Be aware that any optimization might result in worse
performance on any other platform and version than the one
optimized for, might introduce bugs or raise maintenance
costs.
>emulate C++ templates
Hotspot already is quite smart and can outperform C++ in
some cases because it uses information obtained at runtime.
Fixing »dim« to a compile-time constant might have no or only
a very small influence on the speed of the execution of the
methods. You might like to measure this in the target
environment.
Supersedes: <optimization-20070411221157@ram.dialup.fu-berlin.de>
Eric Sosman - 11 Apr 2007 21:41 GMT
christoph.ortner@comlab.ox.ac.uk wrote On 04/11/07 15:53,:
> Hi everyone,
>
[quoted text clipped - 28 lines]
> What if I declared dim as static final in the class Vec? If I would
> hard-code the dimension so-to-speak?
First, see Stefan Ram's response. His principal message
is that the only way to tell for sure is to try it both ways
and measure, to which I'd add that micro-benchmarks in Java
are *very* tricky.
Second, the `dim' element seems pointless since Java
arrays already know their lengths: it merely duplicates the
value `x.length'. Whether eliinating `dim' in favor of
`x.length' would be faster or slower is a matter for yet
another measurement.

Signature
Eric.Sosman@sun.com
Daniel Pitts - 12 Apr 2007 00:42 GMT
On Apr 11, 12:53 pm, christoph.ort...@comlab.ox.ac.uk wrote:
> Hi everyone,
>
[quoted text clipped - 30 lines]
> Many thanks,
> Christoph
Are you sure the particular snippet you are optimizing is really as
performance critical as you believe? Often the only way to determine
this is by using a profiler. The only way to use a profiler is to run
working code. The only way to run working code is to have written
working code.
I suggest you work on creating a well-designed working project before
you decide to complicate your design to optimize an unprofiled
application. The laws of profiling are: 1. Don't do it, and 2. (For
experts only) Don't do it yet.
Follow these laws and your life will be *so* much easier. Let the
profiler tell you whats slow, not your gut.