|
From: Sasa M. <sa...@us...> - 2004-03-26 08:58:02
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3248/org/jrobin/core Modified Files: Archive.java Robin.java RrdDoubleArray.java RrdFile.java Log Message: Performance improvements Index: Robin.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Robin.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Robin.java 24 Mar 2004 13:55:52 -0000 1.8 --- Robin.java 26 Mar 2004 08:47:05 -0000 1.9 *************** *** 66,75 **** */ public double[] getValues() throws IOException { ! double[] result = new double[rows]; ! int start = pointer.get(); ! for(int i = start, j = 0; i < start + rows; i++, j++) { ! result[j] = values.get(i % rows); ! } ! return result; } --- 66,70 ---- */ public double[] getValues() throws IOException { ! return getValues(0, rows); } *************** *** 81,88 **** } - // TODO: NOT TESTED ENOUGH // stores the same value several times void bulkStore(double newValue, int bulkCount) throws IOException { ! assert bulkCount <= rows: "Invalid number of bulk updates"; int position = pointer.get(); // update tail --- 76,83 ---- } // stores the same value several times void bulkStore(double newValue, int bulkCount) throws IOException { ! assert bulkCount <= rows: "Invalid number of bulk updates: " + bulkCount + ! " rows=" + rows; int position = pointer.get(); // update tail *************** *** 91,98 **** pointer.set((position + tailUpdateCount) % rows); // do we need to update from the start? ! int startUpdateCount = bulkCount - tailUpdateCount; ! if(startUpdateCount > 0) { ! values.set(0, newValue, startUpdateCount); ! pointer.set(startUpdateCount); } } --- 86,93 ---- pointer.set((position + tailUpdateCount) % rows); // do we need to update from the start? ! int headUpdateCount = bulkCount - tailUpdateCount; ! if(headUpdateCount > 0) { ! values.set(0, newValue, headUpdateCount); ! pointer.set(headUpdateCount); } } *************** *** 122,126 **** */ public double getValue(int index) throws IOException { ! return values.get((pointer.get() + index) % rows); } --- 117,145 ---- */ public double getValue(int index) throws IOException { ! int arrayIndex = (pointer.get() + index) % rows; ! return values.get(arrayIndex); ! } ! ! double[] getValues(int index, int count) throws IOException { ! assert count <= rows: "Too many values requested: " + count + " rows=" + rows; ! int startIndex = (pointer.get() + index) % rows; ! int tailReadCount = Math.min(rows - startIndex, count); ! double[] tailValues = values.get(startIndex, tailReadCount); ! if(tailReadCount < count) { ! int headReadCount = count - tailReadCount; ! double[] headValues = values.get(0, headReadCount); ! double[] values = new double[count]; ! int k = 0; ! for(int i = 0; i < tailValues.length; i++) { ! values[k++] = tailValues[i]; ! } ! for(int i = 0; i < headValues.length; i++) { ! values[k++] = headValues[i]; ! } ! return values; ! } ! else { ! return tailValues; ! } } Index: RrdDoubleArray.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDoubleArray.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdDoubleArray.java 24 Mar 2004 13:55:52 -0000 1.3 --- RrdDoubleArray.java 26 Mar 2004 08:47:05 -0000 1.4 *************** *** 62,64 **** --- 62,72 ---- } + double[] get(int index, int count) throws IOException { + assert index + count <= length: "Invalid index/count supplied: " + index + + "/" + count + " (length=" + length + ")"; + RrdFile rrdFile = getRrdFile(); + rrdFile.seek(getPointer() + index * RrdDouble.SIZE); + return rrdFile.readDouble(count); + } + } Index: RrdFile.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdFile.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** RrdFile.java 24 Mar 2004 13:55:52 -0000 1.5 --- RrdFile.java 26 Mar 2004 08:47:05 -0000 1.6 *************** *** 33,37 **** import java.util.LinkedList; import java.util.List; - import java.util.Arrays; /** --- 33,36 ---- *************** *** 180,188 **** void writeDouble(double value, int count) throws IOException { ! // THIS IS SLOW! RandomAccessFile is crap ! // for(int i = 0; i < count; i++) { ! // super.writeDouble(value); ! // } ! // HACK to speed things up! long valueAsLong = Double.doubleToLongBits(value); byte[] valueAsBytes = new byte[8]; --- 179,183 ---- void writeDouble(double value, int count) throws IOException { ! // HACK: Speed things up! long valueAsLong = Double.doubleToLongBits(value); byte[] valueAsBytes = new byte[8]; *************** *** 212,214 **** --- 207,232 ---- } + double[] readDouble(int count) throws IOException { + // HACK: Speed this up + double[] values = new double[count]; + int byteCount = 8 * count; + byte[] image = new byte[byteCount]; + if(super.read(image) == -1) { + throw new IOException("End of file reached"); + } + for(int i = 0, k = -1; i < count; i++) { + long valueAsLong = + ((image[++k] & 0xFFL) << 56) | + ((image[++k] & 0xFFL) << 48) | + ((image[++k] & 0xFFL) << 40) | + ((image[++k] & 0xFFL) << 32) | + ((image[++k] & 0xFFL) << 24) | + ((image[++k] & 0xFFL) << 16) | + ((image[++k] & 0xFFL) << 8) | + ((image[++k] & 0xFFL) ); + values[i] = Double.longBitsToDouble(valueAsLong); + } + image = null; + return values; + } } Index: Archive.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Archive.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** Archive.java 24 Mar 2004 13:55:52 -0000 1.11 --- Archive.java 26 Mar 2004 08:47:05 -0000 1.12 *************** *** 159,170 **** } } - // update robin in bulk int bulkUpdateCount = (int) Math.min(numUpdates / steps.get(), (long) rows.get()); - //TODO: Check this - //for(long i = 0; i < bulkUpdates; i++) { - // robin.store(value); - //} - // OLD CODE ENDS robin.bulkStore(value, bulkUpdateCount); // update remaining steps --- 159,164 ---- *************** *** 335,354 **** long[] timestamps = new long[ptsCount]; double[][] values = new double[dsCount][ptsCount]; for(int ptIndex = 0; ptIndex < ptsCount; ptIndex++) { long time = fetchStart + ptIndex * arcStep; timestamps[ptIndex] = time; ! if(time >= startTime && time <= endTime) { ! // inbound time ! int robinIndex = (int)((time - startTime) / arcStep); ! for(int i = 0; i < dsCount; i++) { ! int dsIndex = parentDb.getDsIndex(dsToFetch[i]); ! values[i][ptIndex] = robins[dsIndex].getValue(robinIndex); ! } ! } ! else { ! // time out of bounds ! for(int i = 0; i < dsCount; i++) { ! values[i][ptIndex] = Double.NaN; } } } --- 329,356 ---- long[] timestamps = new long[ptsCount]; double[][] values = new double[dsCount][ptsCount]; + long matchStartTime = Math.max(fetchStart, startTime); + long matchEndTime = Math.min(fetchEnd, endTime); + double[][] robinValues = null; + if(matchStartTime <= matchEndTime) { + // preload robin values + int matchCount = (int)((matchEndTime - matchStartTime) / arcStep + 1); + int matchStartIndex = (int)((matchStartTime - startTime) / arcStep); + robinValues = new double[dsCount][]; + for(int i = 0; i < dsCount; i++) { + int dsIndex = parentDb.getDsIndex(dsToFetch[i]); + robinValues[i] = robins[dsIndex].getValues(matchStartIndex, matchCount); + } + } for(int ptIndex = 0; ptIndex < ptsCount; ptIndex++) { long time = fetchStart + ptIndex * arcStep; timestamps[ptIndex] = time; ! for(int i = 0; i < dsCount; i++) { ! double value = Double.NaN; ! if(time >= matchStartTime && time <= matchEndTime) { ! // inbound time ! int robinValueIndex = (int)((time - matchStartTime) / arcStep); ! value = robinValues[i][robinValueIndex]; } + values[i][ptIndex] = value; } } |