From 2ea169a2ecbb7e589fcef78cb1486d007a8fb867 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Mon, 15 May 2017 16:25:11 -0700 Subject: [PATCH] Introduce early exit path for non resource path. Developer can specify android:fontFamily with three ways, pre-defined font family name, e.g. "sans-serif", path to the font file in resource directory, e.g. "res/fonts/Roboto-Regular.ttf", or path to the XML font family file, e.g. "res/fonts/Roboto.xml". Resources.getFont treats font files and XML files but pre-defined family name is handled by TextView. Thus, we can early exit if the passed value is not likely resource path. This improves the inflation performance. The score without this patch: gfx-avg-frame-time-50: 6.9 gfx-avg-frame-time-90: 9.4 gfx-avg-frame-time-95: 10.4 gfx-avg-frame-time-99: 16.7 The score with this patch: gfx-avg-frame-time-50: 7.0 gfx-avg-frame-time-90: 8.9 gfx-avg-frame-time-95: 9.7 gfx-avg-frame-time-99: 16.5 Measured on bullhead-userdebug. The APCT perf test improves from String FontFamily: 200,086 -> 132,561 File FontFamily : 199,256 -> 161,843 XML FontFamily : 203,681 -> 158,553 Measured on angler-userdebug. Bug: 38232467 Test: UiBenchmark Change-Id: Ia601ae7207ae8c60848c9efdbb9396267a57257c --- apct-tests/perftests/core/res/font/samplefont.ttf | Bin 0 -> 696 bytes .../perftests/core/res/font/samplefont_source.ttx | 177 +++++++++++++++++++++ .../perftests/core/res/font/samplexmlfont.xml | 19 +++ .../res/layout/test_textview_font_family_file.xml | 20 +++ .../layout/test_textview_font_family_string.xml | 20 +++ .../res/layout/test_textview_font_family_xml.xml | 20 +++ apct-tests/perftests/core/res/values/strings.xml | 1 + .../widget/TextViewFontFamilyLayoutPerfTest.java | 72 +++++++++ core/java/android/content/res/ResourcesImpl.java | 4 + 9 files changed, 333 insertions(+) create mode 100644 apct-tests/perftests/core/res/font/samplefont.ttf create mode 100644 apct-tests/perftests/core/res/font/samplefont_source.ttx create mode 100644 apct-tests/perftests/core/res/font/samplexmlfont.xml create mode 100644 apct-tests/perftests/core/res/layout/test_textview_font_family_file.xml create mode 100644 apct-tests/perftests/core/res/layout/test_textview_font_family_string.xml create mode 100644 apct-tests/perftests/core/res/layout/test_textview_font_family_xml.xml create mode 100644 apct-tests/perftests/core/src/android/widget/TextViewFontFamilyLayoutPerfTest.java diff --git a/apct-tests/perftests/core/res/font/samplefont.ttf b/apct-tests/perftests/core/res/font/samplefont.ttf new file mode 100644 index 0000000000000000000000000000000000000000..b8302d4ea30f8ed15e235bd8dc3b9e29030a3e22 GIT binary patch literal 696 zcmZWmJxc>Y5PiFM`S1e;3+pRXEc_ych*+cwf}ofXwDDun%cZzW4wFFaEXB&wM(_vN z3Ko_YDXc{QfuPulTAy$4HfnT^otgLM?cVGx1b}`V!Gg0^n$1mcJlHJ`4RER-n zd`dj!`G*xonz+@z>Z<*|f#-Lomt6Bz64tAjVgAH5Rt=hMxsEOBkBQTMeNQpc#0_0K zQ0mL-)#c*O)8s3@?4cvR=sG;VrNCa}Lk^;*6k+YSEQ`|Nr&{eehs> literal 0 HcmV?d00001 diff --git a/apct-tests/perftests/core/res/font/samplefont_source.ttx b/apct-tests/perftests/core/res/font/samplefont_source.ttx new file mode 100644 index 000000000000..40fa26805772 --- /dev/null +++ b/apct-tests/perftests/core/res/font/samplefont_source.ttx @@ -0,0 +1,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Sample Font + + + Regular + + + Sample Font + + + SampleFont-Regular + + + Sample Font + + + Regular + + + Sample Font + + + SampleFont-Regular + + + + + + + + + + + + + + + + diff --git a/apct-tests/perftests/core/res/font/samplexmlfont.xml b/apct-tests/perftests/core/res/font/samplexmlfont.xml new file mode 100644 index 000000000000..b226446c8e2e --- /dev/null +++ b/apct-tests/perftests/core/res/font/samplexmlfont.xml @@ -0,0 +1,19 @@ + + + + + diff --git a/apct-tests/perftests/core/res/layout/test_textview_font_family_file.xml b/apct-tests/perftests/core/res/layout/test_textview_font_family_file.xml new file mode 100644 index 000000000000..3689a640a77a --- /dev/null +++ b/apct-tests/perftests/core/res/layout/test_textview_font_family_file.xml @@ -0,0 +1,20 @@ + + diff --git a/apct-tests/perftests/core/res/layout/test_textview_font_family_string.xml b/apct-tests/perftests/core/res/layout/test_textview_font_family_string.xml new file mode 100644 index 000000000000..5f236f559826 --- /dev/null +++ b/apct-tests/perftests/core/res/layout/test_textview_font_family_string.xml @@ -0,0 +1,20 @@ + + diff --git a/apct-tests/perftests/core/res/layout/test_textview_font_family_xml.xml b/apct-tests/perftests/core/res/layout/test_textview_font_family_xml.xml new file mode 100644 index 000000000000..0331fa579922 --- /dev/null +++ b/apct-tests/perftests/core/res/layout/test_textview_font_family_xml.xml @@ -0,0 +1,20 @@ + + diff --git a/apct-tests/perftests/core/res/values/strings.xml b/apct-tests/perftests/core/res/values/strings.xml index 5d1f6f01dcb1..7ab325f79dc7 100644 --- a/apct-tests/perftests/core/res/values/strings.xml +++ b/apct-tests/perftests/core/res/values/strings.xml @@ -28,4 +28,5 @@ text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text + text text diff --git a/apct-tests/perftests/core/src/android/widget/TextViewFontFamilyLayoutPerfTest.java b/apct-tests/perftests/core/src/android/widget/TextViewFontFamilyLayoutPerfTest.java new file mode 100644 index 000000000000..4b6da6ba345a --- /dev/null +++ b/apct-tests/perftests/core/src/android/widget/TextViewFontFamilyLayoutPerfTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package android.widget; + +import android.content.Context; +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.LargeTest; +import android.support.test.runner.AndroidJUnit4; +import android.view.LayoutInflater; + +import com.android.perftests.core.R; + +import java.util.Collection; +import java.util.Arrays; + +import org.junit.Test; +import org.junit.Rule; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertTrue; + +@LargeTest +@RunWith(Parameterized.class) +public class TextViewFontFamilyLayoutPerfTest { + @Parameters(name = "{0}") + public static Collection layouts() { + return Arrays.asList(new Object[][] { + { "String fontFamily attribute", R.layout.test_textview_font_family_string}, + { "File fontFamily attribute", R.layout.test_textview_font_family_file}, + { "XML fontFamily attribute", R.layout.test_textview_font_family_xml}, + }); + } + + private int mLayoutId; + + public TextViewFontFamilyLayoutPerfTest(String key, int layoutId) { + mLayoutId = layoutId; + } + + @Rule + public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void testConstruction() throws Throwable { + final Context context = InstrumentationRegistry.getTargetContext(); + final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + final LayoutInflater inflator = + (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + + while (state.keepRunning()) { + inflator.inflate(mLayoutId, null, false); + } + } +} diff --git a/core/java/android/content/res/ResourcesImpl.java b/core/java/android/content/res/ResourcesImpl.java index bdfef8326fdf..b3adf82c75a4 100644 --- a/core/java/android/content/res/ResourcesImpl.java +++ b/core/java/android/content/res/ResourcesImpl.java @@ -772,6 +772,10 @@ public class ResourcesImpl { } final String file = value.string.toString(); + if (!file.startsWith("res/")) { + return null; + } + Typeface cached = Typeface.findFromCache(mAssets, file); if (cached != null) { return cached; -- 2.11.0