Eliminate allocs - float array buffers (#1892)

Creating field level buffers for frequently instantiated float arrays.  Cuts instantiations of many small objects.
This commit is contained in:
Tony Patino 2016-06-28 16:34:49 -07:00
parent f86c1632a7
commit d946f3e3ff
10 changed files with 114 additions and 28 deletions

View file

@ -1004,6 +1004,7 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData<
return mDrawListener;
}
protected float[] mGetPositionBuffer = new float[2];
/**
* Returns a recyclable MPPointF instance.
* Returns the position (in pixels) the provided Entry has inside the chart
@ -1017,9 +1018,9 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData<
if (e == null)
return null;
float[] vals = new float[]{
e.getX(), e.getY()
};
float[] vals = mGetPositionBuffer;
vals[0] = e.getX();
vals[1] = e.getY();
getTransformer(axis).pointValuesToPixel(vals);
@ -1535,11 +1536,13 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData<
return null;
}
protected float[] mOnSizeChangedBuffer = new float[2];
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// Saving current position of chart.
float[] pts = new float[2];
float[] pts = mOnSizeChangedBuffer;
pts[0] = pts[1] = 0;
if (mKeepPositionOnRotation) {
pts[0] = mViewPortHandler.contentLeft();
pts[1] = mViewPortHandler.contentTop();

View file

@ -165,6 +165,7 @@ public class HorizontalBarChart extends BarChart {
return bounds;
}
protected float[] mGetPositionBuffer = new float[2];
/**
* Returns a recyclable MPPointF instance.
*
@ -178,7 +179,9 @@ public class HorizontalBarChart extends BarChart {
if (e == null)
return null;
float[] vals = new float[]{e.getY(), e.getX()};
float[] vals = mGetPositionBuffer;
vals[0] = e.getY();
vals[1] = e.getX();
getTransformer(axis).pointValuesToPixel(vals);

View file

@ -41,12 +41,12 @@ public class PieChart extends PieRadarChartBase<PieData> {
/**
* array that holds the width of each pie-slice in degrees
*/
private float[] mDrawAngles;
private float[] mDrawAngles = new float[1];
/**
* array that holds the absolute angle in degrees of each slice
*/
private float[] mAbsoluteAngles;
private float[] mAbsoluteAngles = new float[1];
/**
* if true, the white hole inside the chart will be drawn
@ -210,8 +210,20 @@ public class PieChart extends PieRadarChartBase<PieData> {
int entryCount = mData.getEntryCount();
mDrawAngles = new float[entryCount];
mAbsoluteAngles = new float[entryCount];
if(mDrawAngles.length != entryCount) {
mDrawAngles = new float[entryCount];
}else{
for(int i = 0 ; i < entryCount ; i++){
mDrawAngles[i] = 0;
}
}
if(mAbsoluteAngles.length != entryCount) {
mAbsoluteAngles = new float[entryCount];
}else{
for(int i = 0 ; i < entryCount ; i++){
mAbsoluteAngles[i] = 0;
}
}
float yValueSum = mData.getYValueSum();

View file

@ -356,8 +356,8 @@ public class LineChartRenderer extends LineRadarRenderer {
} else { // only one color per dataset
if (mLineBuffer.length != Math.max((entryCount) * pointsPerEntryPair, pointsPerEntryPair) * 2)
mLineBuffer = new float[Math.max((entryCount) * pointsPerEntryPair, pointsPerEntryPair) * 2];
if (mLineBuffer.length < Math.max((entryCount) * pointsPerEntryPair, pointsPerEntryPair) * 2)
mLineBuffer = new float[Math.max((entryCount) * pointsPerEntryPair, pointsPerEntryPair) * 4];
Entry e1, e2;
@ -527,6 +527,7 @@ public class LineChartRenderer extends LineRadarRenderer {
}
private Path mCirclePathBuffer = new Path();
private float[] circlesBuffer = new float[2];
protected void drawCircles(Canvas c) {
@ -534,7 +535,8 @@ public class LineChartRenderer extends LineRadarRenderer {
float phaseY = mAnimator.getPhaseY();
float[] circlesBuffer = new float[2];
circlesBuffer[0] = 0;
circlesBuffer[1] = 0;
List<ILineDataSet> dataSets = mChart.getLineData().getDataSets();

View file

@ -169,6 +169,7 @@ public class XAxisRenderer extends AxisRenderer {
}
}
protected float[] mDrawLabelsBuffer = new float[2];
/**
* draws the x-labels on the specified y-position
*
@ -179,7 +180,10 @@ public class XAxisRenderer extends AxisRenderer {
final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled();
float[] positions = new float[mXAxis.mEntryCount * 2];
if(mDrawLabelsBuffer.length != mAxis.mEntryCount * 2){
mDrawLabelsBuffer = new float[mXAxis.mEntryCount * 2];
}
float[] positions = mDrawLabelsBuffer;
for (int i = 0; i < positions.length; i += 2) {
@ -189,6 +193,8 @@ public class XAxisRenderer extends AxisRenderer {
} else {
positions[i] = mXAxis.mEntries[i / 2];
}
// init to 0
positions[i+1] = 0;
}
mTrans.pointValuesToPixel(positions);
@ -228,13 +234,17 @@ public class XAxisRenderer extends AxisRenderer {
Utils.drawXAxisValue(c, formattedLabel, x, y, mAxisLabelPaint, anchor, angleDegrees);
}
protected float[] mRenderGridLinesBuffer = new float[2];
@Override
public void renderGridLines(Canvas c) {
if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled())
return;
float[] positions = new float[mXAxis.mEntryCount * 2];
if(mRenderGridLinesBuffer.length != mAxis.mEntryCount * 2){
mRenderGridLinesBuffer = new float[mXAxis.mEntryCount * 2];
}
float[] positions = mRenderGridLinesBuffer;
for (int i = 0; i < positions.length; i += 2) {
positions[i] = mXAxis.mEntries[i / 2];
@ -272,6 +282,7 @@ public class XAxisRenderer extends AxisRenderer {
gridLinePath.reset();
}
protected float[] mRenderLimitLinesBuffer = new float[2];
/**
* Draws the LimitLines associated with this axis to the screen.
*
@ -285,7 +296,9 @@ public class XAxisRenderer extends AxisRenderer {
if (limitLines == null || limitLines.size() <= 0)
return;
float[] position = new float[2];
float[] position = mRenderLimitLinesBuffer;
position[0] = 0;
position[1] = 0;
for (int i = 0; i < limitLines.size(); i++) {

View file

@ -5,7 +5,6 @@ import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Path;
import android.graphics.PointF;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.LimitLine;
@ -130,13 +129,17 @@ public class XAxisRendererHorizontalBarChart extends XAxisRenderer {
MPPointF.recycleInstance(pointF);
}
protected float[] mDrawLabelsBuffer = new float[2];
@Override
protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled();
float[] positions = new float[mXAxis.mEntryCount * 2];
if(mDrawLabelsBuffer.length != mAxis.mEntryCount * 2){
mDrawLabelsBuffer = new float[mXAxis.mEntryCount * 2];
}
float[] positions = mDrawLabelsBuffer;
for (int i = 0; i < positions.length; i += 2) {
@ -200,7 +203,7 @@ public class XAxisRendererHorizontalBarChart extends XAxisRenderer {
}
}
/**
/**
* Draws the LimitLines associated with this axis to the screen.
* This is the standard YAxis renderer using the XAxis limit lines.
*
@ -214,7 +217,10 @@ public class XAxisRendererHorizontalBarChart extends XAxisRenderer {
if (limitLines == null || limitLines.size() <= 0)
return;
float[] pts = new float[2];
float[] pts = mRenderLimitLinesBuffer;
pts[0] = 0;
pts[1] = 0;
Path limitLinePath = new Path();
for (int i = 0; i < limitLines.size(); i++) {

View file

@ -171,6 +171,7 @@ public class YAxisRenderer extends AxisRenderer {
return p;
}
protected float[] mGetTransformedPositionsBuffer = new float[2];
/**
* Transforms the values contained in the axis entries to screen pixels and returns them in form of a float array
* of x- and y-coordinates.
@ -179,7 +180,10 @@ public class YAxisRenderer extends AxisRenderer {
*/
protected float[] getTransformedPositions() {
float[] positions = new float[mYAxis.mEntryCount * 2];
if(mGetTransformedPositionsBuffer.length != mYAxis.mEntryCount * 2){
mGetTransformedPositionsBuffer = new float[mYAxis.mEntryCount * 2];
}
float[] positions = mGetTransformedPositionsBuffer;
for (int i = 0; i < positions.length; i += 2) {
// only fill y values, x values are not needed for y-labels
@ -210,6 +214,7 @@ public class YAxisRenderer extends AxisRenderer {
c.drawPath(zeroLinePath, mZeroLinePaint);
}
protected float[] mRenderLimitLinesBuffer = new float[2];
/**
* Draws the LimitLines associated with this axis to the screen.
*
@ -223,7 +228,9 @@ public class YAxisRenderer extends AxisRenderer {
if (limitLines == null || limitLines.size() <= 0)
return;
float[] pts = new float[2];
float[] pts = mRenderLimitLinesBuffer;
pts[0] = 0;
pts[1] = 0;
Path limitLinePath = new Path();
for (int i = 0; i < limitLines.size(); i++) {

View file

@ -150,7 +150,10 @@ public class YAxisRendererHorizontalBarChart extends YAxisRenderer {
@Override
protected float[] getTransformedPositions() {
float[] positions = new float[mYAxis.mEntryCount * 2];
if(mGetTransformedPositionsBuffer.length != mYAxis.mEntryCount * 2) {
mGetTransformedPositionsBuffer = new float[mYAxis.mEntryCount * 2];
}
float[] positions = mGetTransformedPositionsBuffer;
for (int i = 0; i < positions.length; i += 2) {
// only fill x values, y values are not needed for x-labels
@ -188,6 +191,7 @@ public class YAxisRendererHorizontalBarChart extends YAxisRenderer {
c.drawPath(zeroLinePath, mZeroLinePaint);
}
protected float[] mRenderLimitLinesBuffer = new float[4];
/**
* Draws the LimitLines associated with this axis to the screen.
* This is the standard XAxis renderer using the YAxis limit lines.
@ -202,7 +206,11 @@ public class YAxisRendererHorizontalBarChart extends YAxisRenderer {
if (limitLines == null || limitLines.size() <= 0)
return;
float[] pts = new float[4];
float[] pts = mRenderLimitLinesBuffer;
pts[0] = 0;
pts[1] = 0;
pts[2] = 0;
pts[3] = 0;
Path limitLinePath = new Path();
for (int i = 0; i < limitLines.size(); i++) {

View file

@ -93,6 +93,7 @@ public class Transformer {
// mOffsetBottom);
}
protected float[] valuePointsForGenerateTransformedValuesScatter = new float[1];
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the SCATTERCHART.
@ -105,7 +106,10 @@ public class Transformer {
final int count = (int) ((to - from) * phaseX + 1) * 2;
float[] valuePoints = new float[count];
if(valuePointsForGenerateTransformedValuesScatter.length != count){
valuePointsForGenerateTransformedValuesScatter = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesScatter;
for (int j = 0; j < count; j += 2) {
@ -114,6 +118,9 @@ public class Transformer {
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getY() * phaseY;
}else{
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
@ -122,6 +129,7 @@ public class Transformer {
return valuePoints;
}
protected float[] valuePointsForGenerateTransformedValuesBubble = new float[1];
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the BUBBLECHART.
@ -133,7 +141,10 @@ public class Transformer {
final int count = (to - from + 1) * 2; // (int) Math.ceil((to - from) * phaseX) * 2;
float[] valuePoints = new float[count];
if(valuePointsForGenerateTransformedValuesBubble.length != count){
valuePointsForGenerateTransformedValuesBubble = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesBubble;
for (int j = 0; j < count; j += 2) {
@ -142,6 +153,9 @@ public class Transformer {
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getY() * phaseY;
}else{
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
@ -150,6 +164,7 @@ public class Transformer {
return valuePoints;
}
protected float[] valuePointsForGenerateTransformedValuesLine = new float[1];
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the LINECHART.
@ -162,7 +177,10 @@ public class Transformer {
final int count = (int) ((to - from) * phaseX + 1) * 2;
float[] valuePoints = new float[count];
if(valuePointsForGenerateTransformedValuesLine.length != count){
valuePointsForGenerateTransformedValuesLine = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesLine;
for (int j = 0; j < count; j += 2) {
@ -171,6 +189,9 @@ public class Transformer {
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getY() * phaseY;
}else{
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
@ -179,6 +200,7 @@ public class Transformer {
return valuePoints;
}
protected float[] valuePointsForGenerateTransformedValuesCandle = new float[1];
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the CANDLESTICKCHART.
@ -191,7 +213,10 @@ public class Transformer {
final int count = (int) ((to - from) * phaseX + 1) * 2;
float[] valuePoints = new float[count];
if(valuePointsForGenerateTransformedValuesCandle.length != count){
valuePointsForGenerateTransformedValuesCandle = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesCandle;
for (int j = 0; j < count; j += 2) {
@ -200,6 +225,9 @@ public class Transformer {
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getHigh() * phaseY;
}else{
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}

View file

@ -291,6 +291,7 @@ public class ViewPortHandler {
return save;
}
protected float[] valsBufferForFitScreen = new float[9];
/**
* Resets all zooming and dragging and makes the chart fit exactly it's
* bounds.
@ -303,7 +304,10 @@ public class ViewPortHandler {
Matrix save = new Matrix();
save.set(mMatrixTouch);
float[] vals = new float[9];
float[] vals = valsBufferForFitScreen;
for(int i = 0 ; i < 9 ; i++){
vals[i] = 0;
}
save.getValues(vals);