Eliminate allocs - String Caches now have Prim subtypes (#1892)
By creating versions of the FormattedStringCache that don't require auto boxing, allocations are cut tremendously in this commit.
This commit is contained in:
parent
a04ad28cec
commit
424ee02ce2
3 changed files with 85 additions and 7 deletions
|
@ -165,15 +165,15 @@ public class BarChartPositiveNegative extends DemoBase {
|
|||
|
||||
private class ValueFormatter implements com.github.mikephil.charting.formatter.ValueFormatter {
|
||||
|
||||
private FormattedStringCache<Integer, Float> mFormattedStringCache;
|
||||
private FormattedStringCache.PrimIntFloat mFormattedStringCache;
|
||||
|
||||
public ValueFormatter() {
|
||||
mFormattedStringCache = new FormattedStringCache<>(new DecimalFormat("######.0"));
|
||||
mFormattedStringCache = new FormattedStringCache.PrimIntFloat(new DecimalFormat("######.0"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
|
||||
return mFormattedStringCache.getFormattedString(value, dataSetIndex);
|
||||
return mFormattedStringCache.getFormattedValue(value, dataSetIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ public class DefaultAxisValueFormatter implements AxisValueFormatter {
|
|||
/**
|
||||
* FormattedStringFormat for formatting and caching
|
||||
*/
|
||||
protected FormattedStringCache<Float, Float> mFormattedStringCache;
|
||||
protected FormattedStringCache.PrimFloat mFormattedStringCache;
|
||||
|
||||
/**
|
||||
* the number of decimal digits this formatter uses
|
||||
|
@ -35,14 +35,14 @@ public class DefaultAxisValueFormatter implements AxisValueFormatter {
|
|||
b.append("0");
|
||||
}
|
||||
|
||||
mFormattedStringCache = new FormattedStringCache<>(new DecimalFormat("###,###,###,##0" + b.toString()));
|
||||
mFormattedStringCache = new FormattedStringCache.PrimFloat(new DecimalFormat("###,###,###,##0" + b.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormattedValue(float value, AxisBase axis) {
|
||||
|
||||
// TODO: There should be a better way to do this. Floats are not the best keys...
|
||||
return mFormattedStringCache.getFormattedString(value, value);
|
||||
return mFormattedStringCache.getFormattedValue(value);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.github.mikephil.charting.formatter;
|
||||
|
||||
import java.text.Format;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
|
@ -17,7 +18,7 @@ import java.util.HashMap;
|
|||
*/
|
||||
public class FormattedStringCache<K, V> {
|
||||
|
||||
private Format mFormat;
|
||||
protected Format mFormat;
|
||||
private HashMap<K, String> mCachedStringsHashMap = new HashMap<>();
|
||||
private HashMap<K, V> mCachedValuesHashMap = new HashMap<>();
|
||||
|
||||
|
@ -48,4 +49,81 @@ public class FormattedStringCache<K, V> {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static class PrimIntFloat extends FormattedStringCache{
|
||||
|
||||
public PrimIntFloat(Format format){
|
||||
super(format);
|
||||
}
|
||||
|
||||
private ArrayList<Float> cachedValues = new ArrayList<>();
|
||||
private ArrayList<String> cachedStrings = new ArrayList<>();
|
||||
|
||||
public String getFormattedValue(float value, int key) {
|
||||
|
||||
boolean hasValueAtdataSetIndex = true;
|
||||
if(cachedValues.size() <= key){
|
||||
int p = key;
|
||||
while(p >= 0){
|
||||
if(p == 0){
|
||||
cachedValues.add(value);
|
||||
cachedStrings.add("");
|
||||
}else{
|
||||
cachedValues.add(Float.NaN);
|
||||
cachedStrings.add("");
|
||||
}
|
||||
p--;
|
||||
}
|
||||
hasValueAtdataSetIndex = false;
|
||||
}
|
||||
|
||||
if(hasValueAtdataSetIndex) {
|
||||
Float cachedValue = cachedValues.get(key);
|
||||
hasValueAtdataSetIndex = !(cachedValue == null || cachedValue != value);
|
||||
}
|
||||
|
||||
if(!hasValueAtdataSetIndex){
|
||||
cachedValues.set(key, value);
|
||||
cachedStrings.set(key, mFormat.format(value));
|
||||
}
|
||||
|
||||
return cachedStrings.get(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class PrimFloat extends FormattedStringCache{
|
||||
|
||||
public PrimFloat(Format format){
|
||||
super(format);
|
||||
}
|
||||
|
||||
|
||||
private ArrayList<Float> cachedValues = new ArrayList<>();
|
||||
private ArrayList<String> cachedStrings = new ArrayList<>();
|
||||
|
||||
public String getFormattedValue(float value) {
|
||||
|
||||
boolean alreadyHasValue = false;
|
||||
int vCount = cachedValues.size();
|
||||
int sIndex = -1;
|
||||
for(int i = 0 ; i < vCount ; i++){
|
||||
if(cachedValues.get(i) == value){
|
||||
sIndex = i;
|
||||
alreadyHasValue = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!alreadyHasValue) {
|
||||
cachedValues.add(value);
|
||||
cachedStrings.add(mFormat.format(value));
|
||||
sIndex = cachedValues.size() - 1;
|
||||
}
|
||||
|
||||
return cachedStrings.get(sIndex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue