Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0eb713d1 authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Create LocaleList and HashSet lazily to save OS boot time

Creating HashSet and LocaleList in Zygote make performance impact
to the boot time. Since this API is not called so frequently and less
application is expected to call this API. So let's make Set and
LocaleList only when it is requested.

Here is a result of boot time perf test:
Before: TOTAL_BOOT_TIME : 16229.5
After : TOTAL_BOOT_TIME : 11704.0

Bug: 114774424
Test: atest android.graphics.fonts
Test: atest TypefaceSystemFallbackTest
Change-Id: I07247a78f6091c8adfaac748fe4a77a4597b1ae2
parent 7ee06167
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Locale;

@SmallTest
@@ -112,7 +112,7 @@ public class TypefaceSystemFallbackTest {

    private static void buildSystemFallback(String xml,
            ArrayMap<String, Typeface> fontMap, ArrayMap<String, FontFamily[]> fallbackMap) {
        final HashSet<Font> availableFonts = new HashSet<>();
        final ArrayList<Font> availableFonts = new ArrayList<>();
        try (FileOutputStream fos = new FileOutputStream(TEST_FONTS_XML)) {
            fos.write(xml.getBytes(Charset.forName("UTF-8")));
        } catch (IOException e) {
@@ -127,7 +127,7 @@ public class TypefaceSystemFallbackTest {
    public void testBuildSystemFallback() {
        final ArrayMap<String, Typeface> fontMap = new ArrayMap<>();
        final ArrayMap<String, FontFamily[]> fallbackMap = new ArrayMap<>();
        final HashSet<Font> availableFonts = new HashSet<>();
        final ArrayList<Font> availableFonts = new ArrayList<>();

        final FontConfig.Alias[] aliases = SystemFonts.buildSystemFallback(SYSTEM_FONTS_XML,
                SYSTEM_FONT_DIR, fallbackMap, availableFonts);
+2 −2
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.ArrayList;

public class FontFallbackSetup implements AutoCloseable {
    private final String[] mTestFontFiles;
@@ -76,7 +76,7 @@ public class FontFallbackSetup implements AutoCloseable {
        }

        final ArrayMap<String, FontFamily[]> fallbackMap = new ArrayMap<>();
        final HashSet<Font> availableFonts = new HashSet<>();
        final ArrayList<Font> availableFonts = new ArrayList<>();
        final FontConfig.Alias[] aliases = SystemFonts.buildSystemFallback(testFontsXml,
                mTestFontsDir, fallbackMap, availableFonts);
        Typeface.initSystemDefaultTypefaces(mFontMap, fallbackMap, aliases);
+6 −6
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ public final class Font {

        private @Nullable ByteBuffer mBuffer;
        private @Nullable File mFile;
        private @NonNull LocaleList mLocaleList = LocaleList.getEmptyLocaleList();
        private @NonNull String mLocaleList = "";
        private @IntRange(from = -1, to = 1000) int mWeight = NOT_SPECIFIED;
        private @IntRange(from = -1, to = 1) int mItalic = NOT_SPECIFIED;
        private @IntRange(from = 0) int mTtcIndex = 0;
@@ -150,7 +150,7 @@ public final class Font {
         * @hide
         */
        public Builder(@NonNull ByteBuffer buffer, @NonNull File path,
                @NonNull LocaleList localeList) {
                @NonNull String localeList) {
            this(buffer);
            mFile = path;
            mLocaleList = localeList;
@@ -457,7 +457,7 @@ public final class Font {
    private final boolean mItalic;
    private final @IntRange(from = 0) int mTtcIndex;
    private final @Nullable FontVariationAxis[] mAxes;
    private final @NonNull LocaleList mLocaleList;
    private final @NonNull String mLocaleList;

    /**
     * Use Builder instead
@@ -465,7 +465,7 @@ public final class Font {
    private Font(long nativePtr, @NonNull ByteBuffer buffer, @Nullable File file,
            @IntRange(from = 0, to = 1000) int weight, boolean italic,
            @IntRange(from = 0) int ttcIndex, @Nullable FontVariationAxis[] axes,
            @NonNull LocaleList localeList) {
            @NonNull String localeList) {
        mBuffer = buffer;
        mFile = file;
        mWeight = weight;
@@ -546,7 +546,7 @@ public final class Font {
     * @return a locale list
     */
    public @NonNull LocaleList getLocaleList() {
        return mLocaleList;
        return LocaleList.forLanguageTags(mLocaleList);
    }

    /** @hide */
@@ -580,7 +580,7 @@ public final class Font {
            + ", italic=" + mItalic
            + ", ttcIndex=" + mTtcIndex
            + ", axes=" + FontVariationAxis.toFontVariationSettings(mAxes)
            + ", localeList=" + mLocaleList.toLanguageTags()
            + ", localeList=" + mLocaleList
            + ", buffer=" + mBuffer
            + "}";
    }
+10 −10
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package android.graphics.fonts;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.graphics.FontListParser;
import android.os.LocaleList;
import android.text.FontConfig;
import android.util.ArrayMap;
import android.util.Log;
@@ -54,7 +53,7 @@ public class SystemFonts {

    private static final Map<String, FontFamily[]> sSystemFallbackMap;
    private static final FontConfig.Alias[] sAliases;
    private static final Set<Font> sAvailableFonts;
    private static final List<Font> sAvailableFonts;

    /**
     * Returns all available font files in the system.
@@ -63,7 +62,9 @@ public class SystemFonts {
     * @return an array of system fonts
     */
    public static @NonNull Set<Font> getAvailableFonts() {
        return sAvailableFonts;
        HashSet<Font> set = new HashSet<>();
        set.addAll(sAvailableFonts);
        return set;
    }

    /**
@@ -114,7 +115,7 @@ public class SystemFonts {
            @NonNull ArrayMap<String, ArrayList<FontFamily>> fallbackMap,
            @NonNull Map<String, ByteBuffer> cache,
            @NonNull String fontDir,
            @NonNull HashSet<Font> availableFonts) {
            @NonNull ArrayList<Font> availableFonts) {

        final String languageTags = xmlFamily.getLanguages();
        final int variant = xmlFamily.getVariant();
@@ -170,13 +171,12 @@ public class SystemFonts {
            @FontConfig.Family.Variant int variant,
            @NonNull Map<String, ByteBuffer> cache,
            @NonNull String fontDir,
            @NonNull HashSet<Font> availableFonts) {
            @NonNull ArrayList<Font> availableFonts) {
        if (fonts.size() == 0) {
            return null;
        }

        FontFamily.Builder b = null;
        final LocaleList localeList = LocaleList.forLanguageTags(languageTags);
        for (int i = 0; i < fonts.size(); i++) {
            final FontConfig.Font fontConfig = fonts.get(i);
            final String fullPath = fontDir + fontConfig.getFontName();
@@ -194,7 +194,7 @@ public class SystemFonts {

            final Font font;
            try {
                font = new Font.Builder(buffer, new File(fullPath), localeList)
                font = new Font.Builder(buffer, new File(fullPath), languageTags)
                        .setWeight(fontConfig.getWeight())
                        .setItalic(fontConfig.isItalic())
                        .setTtcIndex(fontConfig.getTtcIndex())
@@ -228,7 +228,7 @@ public class SystemFonts {
    public static FontConfig.Alias[] buildSystemFallback(@NonNull String xmlPath,
            @NonNull String fontDir,
            @NonNull ArrayMap<String, FontFamily[]> fallbackMap,
            @NonNull HashSet<Font> availableFonts) {
            @NonNull ArrayList<Font> availableFonts) {
        try {
            final FileInputStream fontsIn = new FileInputStream(xmlPath);
            final FontConfig fontConfig = FontListParser.parse(fontsIn);
@@ -284,11 +284,11 @@ public class SystemFonts {

    static {
        final ArrayMap<String, FontFamily[]> systemFallbackMap = new ArrayMap<>();
        final HashSet<Font> availableFonts = new HashSet<>();
        final ArrayList<Font> availableFonts = new ArrayList<>();
        sAliases = buildSystemFallback("/system/etc/fonts.xml", "/system/fonts/",
                systemFallbackMap, availableFonts);
        sSystemFallbackMap = Collections.unmodifiableMap(systemFallbackMap);
        sAvailableFonts = Collections.unmodifiableSet(availableFonts);
        sAvailableFonts = Collections.unmodifiableList(availableFonts);
    }

}