Don't create an instance of FontStylingSpan until any of the properties changed

Summary: Minor optimization/cleanup: instead of creating an instance of FontStylingSpan for every RCTVirtualText, start with a global immutable instance of use copy-on-write when any of the properties change.

Reviewed By: ahmedre

Differential Revision: D2817156
This commit is contained in:
Denis Koroskin 2016-01-11 20:03:24 -08:00 committed by Ahmed El-Helw
parent c43d8409c0
commit f98a288c2a
3 changed files with 23 additions and 13 deletions

View File

@ -16,18 +16,28 @@ import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
/* package */ final class FontStylingSpan extends MetricAffectingSpan {
/* package */ static final FontStylingSpan INSTANCE = new FontStylingSpan(
Double.NaN /* mTextColor */,
0 /* mBackgroundColor */,
-1 /* mFontSize */,
-1 /* mFontStyle */,
-1 /* mFontWeight */,
null /* mFontFamily */,
true /* mFrozen */);
// text property
private double mTextColor = Double.NaN;
private double mTextColor;
private int mBackgroundColor;
// font properties
private int mFontSize = -1;
private int mFontStyle = -1;
private int mFontWeight = -1;
private int mFontSize;
private int mFontStyle;
private int mFontWeight;
private @Nullable String mFontFamily;
// whether or not mutation is allowed.
private boolean mFrozen = false;
private boolean mFrozen;
FontStylingSpan() {
}
@ -38,13 +48,15 @@ import android.text.style.MetricAffectingSpan;
int fontSize,
int fontStyle,
int fontWeight,
@Nullable String fontFamily) {
@Nullable String fontFamily,
boolean frozen) {
mTextColor = textColor;
mBackgroundColor = backgroundColor;
mFontSize = fontSize;
mFontStyle = fontStyle;
mFontWeight = fontWeight;
mFontFamily = fontFamily;
mFrozen = frozen;
}
/* package */ FontStylingSpan mutableCopy() {
@ -54,7 +66,8 @@ import android.text.style.MetricAffectingSpan;
mFontSize,
mFontStyle,
mFontWeight,
mFontFamily);
mFontFamily,
false);
}
/* package */ boolean isFrozen() {

View File

@ -50,6 +50,7 @@ import com.facebook.react.uimanager.ViewProps;
public RCTText() {
setMeasureFunction(this);
getSpan().setFontSize(getDefaultFontSize());
}
@Override

View File

@ -29,11 +29,7 @@ import com.facebook.react.uimanager.ViewProps;
private static final String ITALIC = "italic";
private static final String NORMAL = "normal";
private FontStylingSpan mFontStylingSpan = new FontStylingSpan();
RCTVirtualText() {
mFontStylingSpan.setFontSize(getDefaultFontSize());
}
private FontStylingSpan mFontStylingSpan = FontStylingSpan.INSTANCE;
@Override
protected void performCollectText(SpannableStringBuilder builder) {
@ -176,7 +172,7 @@ import com.facebook.react.uimanager.ViewProps;
return (int) Math.ceil(PixelUtil.toPixelFromSP(sp));
}
private FontStylingSpan getSpan() {
protected final FontStylingSpan getSpan() {
if (mFontStylingSpan.isFrozen()) {
mFontStylingSpan = mFontStylingSpan.mutableCopy();
}