Eliminate allocs - Rect and RectF buffers (#1892)

Buffering temporary Rect and RectF instances.  Created methods to modify RectF instances as performance alternatives to methods that create disposable RectF instances.
This commit is contained in:
Tony Patino 2016-06-29 17:05:07 -07:00
parent 0b72b5588b
commit d372fd305c
6 changed files with 50 additions and 16 deletions

View file

@ -3,6 +3,7 @@ package com.xxmassdeveloper.mpchartexample;
import android.annotation.SuppressLint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
@ -257,6 +258,7 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
}
}
protected RectF mOnValueSelectedRectF = new RectF();
@SuppressLint("NewApi")
@Override
public void onValueSelected(Entry e, Highlight h) {
@ -264,7 +266,8 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
if (e == null)
return;
RectF bounds = mChart.getBarBounds((BarEntry) e);
RectF bounds = mOnValueSelectedRectF;
mChart.getBarBounds((BarEntry) e, bounds);
MPPointF position = mChart.getPosition(e, AxisDependency.LEFT);
Log.i("bounds", bounds.toString());

View file

@ -3,6 +3,7 @@ package com.xxmassdeveloper.mpchartexample;
import android.annotation.SuppressLint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
@ -246,6 +247,7 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
}
}
protected RectF mOnValueSelectedRectF = new RectF();
@SuppressLint("NewApi")
@Override
public void onValueSelected(Entry e, Highlight h) {
@ -253,7 +255,9 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
if (e == null)
return;
RectF bounds = mChart.getBarBounds((BarEntry) e);
RectF bounds = mOnValueSelectedRectF;
mChart.getBarBounds((BarEntry) e, bounds);
MPPointF position = mChart.getPosition(e, mChart.getData().getDataSetByIndex(h.getDataSetIndex())
.getAxisDependency());

View file

@ -98,17 +98,37 @@ public class BarChart extends BarLineChartBase<BarData> implements BarDataProvid
/**
* Returns the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be
* found in the charts data.
* found in the charts data. Performance-intensive code should use void getBarBounds(BarEntry, RectF) instead.
*
* @param e
* @return
*/
public RectF getBarBounds(BarEntry e) {
RectF bounds = new RectF();
getBarBounds(e, bounds);
return bounds;
}
/**
* The passed outputRect will be assigned the values of the bounding box of the specified Entry in the specified DataSet.
* The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data.
*
* @param e
* @return
*/
public void getBarBounds(BarEntry e, RectF outputRect){
RectF bounds = outputRect;
IBarDataSet set = mData.getDataSetForEntry(e);
if (set == null)
return null;
if (set == null) {
bounds.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
return;
}
float y = e.getY();
float x = e.getX();
@ -120,11 +140,10 @@ public class BarChart extends BarLineChartBase<BarData> implements BarDataProvid
float top = y >= 0 ? y : 0;
float bottom = y <= 0 ? y : 0;
RectF bounds = new RectF(left, top, right, bottom);
bounds.set(left, top, right, bottom);
getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);
getTransformer(set.getAxisDependency()).rectValueToPixel(outputRect);
return bounds;
}
/**

View file

@ -141,12 +141,15 @@ public class HorizontalBarChart extends BarChart {
}
@Override
public RectF getBarBounds(BarEntry e) {
public void getBarBounds(BarEntry e, RectF outputRect) {
RectF bounds = outputRect;
IBarDataSet set = mData.getDataSetForEntry(e);
if (set == null)
return null;
if (set == null){
outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
return;
}
float y = e.getY();
float x = e.getX();
@ -158,11 +161,10 @@ public class HorizontalBarChart extends BarChart {
float left = y >= 0 ? y : 0;
float right = y <= 0 ? y : 0;
RectF bounds = new RectF(left, top, right, bottom);
bounds.set(left, top, right, bottom);
getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);
return bounds;
}
protected float[] mGetPositionBuffer = new float[2];

View file

@ -725,6 +725,7 @@ public class PieChartRenderer extends DataRenderer {
}
}
protected RectF mDrawHighlightedRectF = new RectF();
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
@ -743,7 +744,8 @@ public class PieChartRenderer extends DataRenderer {
? radius * (mChart.getHoleRadius() / 100.f)
: 0.f;
final RectF highlightedCircleBox = new RectF();
final RectF highlightedCircleBox = mDrawHighlightedRectF;
highlightedCircleBox.set(0,0,0,0);
for (int i = 0; i < indices.length; i++) {

View file

@ -147,6 +147,7 @@ public abstract class Utils {
return (int) paint.measureText(demoText);
}
private static Rect mCalcTextHeightRect = new Rect();
/**
* calculates the approximate height of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
@ -157,7 +158,8 @@ public abstract class Utils {
*/
public static int calcTextHeight(Paint paint, String demoText) {
Rect r = new Rect();
Rect r = mCalcTextHeightRect;
r.set(0,0,0,0);
paint.getTextBounds(demoText, 0, demoText.length(), r);
return r.height();
}
@ -188,6 +190,7 @@ public abstract class Utils {
return result;
}
private static Rect mCalcTextSizeRect = new Rect();
/**
* calculates the approximate size of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
@ -198,7 +201,8 @@ public abstract class Utils {
*/
public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) {
Rect r = new Rect();
Rect r = mCalcTextSizeRect;
r.set(0,0,0,0);
paint.getTextBounds(demoText, 0, demoText.length(), r);
outputFSize.width = r.width();
outputFSize.height = r.height();