Fix performance regression by fixing keys used in Typeface.Builder

The key used in Typeface.Builder is different from the key used in
findFromCache method. The problem is key generation in Typeface.Builder
since the key should be created from requested parameters not the actual
font styles.

Here is the raw performance differences on walleye-userdebug

android.graphics.perftests.TypefaceCreatePerfTest(us):
  createFromResources: 248 -> 23: (-225, -90.7%)

Bug: 131167183
Test: manually collected perf test result.
Change-Id: Idea25095979707ac84b7f4bc1ede0c2daefd6127
This commit is contained in:
Seigo Nonaka
2019-04-23 13:21:27 -07:00
parent ac8913be19
commit 37d181673b
2 changed files with 16 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ package android.graphics.perftests;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.perftests.utils.BenchmarkState;
import android.perftests.utils.PerfStatusReporter;
@@ -26,6 +27,8 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.perftests.core.R;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -104,4 +107,15 @@ public class TypefaceCreatePerfTest {
outFile.delete();
}
@Test
public void testCreate_fromResources() {
BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
final Resources r = InstrumentationRegistry.getContext().getResources();
while (state.keepRunning()) {
Typeface face = r.getFont(R.font.samplefont);
}
}
}

View File

@@ -596,8 +596,8 @@ public class Typeface {
final Font font = mFontBuilder.build();
final String key = mAssetManager == null ? null : createAssetUid(
mAssetManager, mPath, font.getTtcIndex(), font.getAxes(),
font.getStyle().getWeight(), font.getStyle().getSlant(),
mFallbackFamilyName);
mWeight, mItalic,
mFallbackFamilyName == null ? DEFAULT_FAMILY : mFallbackFamilyName);
if (key != null) {
// Dynamic cache lookup is only for assets.
synchronized (sDynamicCacheLock) {