Optimization question
Vitaly Davidovich
vitalyd at gmail.com
Thu Dec 24 00:56:01 UTC 2015
Hi guys,
Consider code like this:
static double mean(double[] array, double[] weights) {
if (array.length != weights.length) throw ...;
double sum = 0;
double wsum = 0;
for(int i = 0; i < array.length; i++) {
sum += array[i] * weights[i];
wsum += weights[i];
}
return sum / wsum;
}
static double mean(double[] array) {
return mean(array, allOnes(array.length));
}
static double[] allOnes(int n) {
double[] d = new double[n];
Arrays.fill(d, 1);
return d;
}
Now suppose I call mean(double[]) overload like this:
double[] d = {1,2,3,4};
Using 8u51 with C2 compiler:
1) it looks like the array allocation from allOnes isn't eliminated.
2) moreover it looked like array was zeroed (rep stosd with rax holding
zero). Unless I misread the asm, I thought an allocation followed by
Arrays.fill skips the zeroing?
3) ideally, this case would reduce to code that just does a plain
unweighted mean with no multiplication by the weight and no summation for the
weighted sum (weight sum is just array length). Is this simply too much
analysis to ask for?
Thanks
--
Sent from my phone
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20151223/82b63dba/attachment.html>
More information about the hotspot-compiler-dev
mailing list