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
diff --git a/core/tests/coretests/src/android/graphics/TypefaceSystemFallbackTest.java b/core/tests/coretests/src/android/graphics/TypefaceSystemFallbackTest.java
index 1c4039b..3ce2589 100644
--- a/core/tests/coretests/src/android/graphics/TypefaceSystemFallbackTest.java
+++ b/core/tests/coretests/src/android/graphics/TypefaceSystemFallbackTest.java
@@ -44,7 +44,7 @@
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 @@
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 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);
diff --git a/core/tests/coretests/src/android/text/FontFallbackSetup.java b/core/tests/coretests/src/android/text/FontFallbackSetup.java
index 355be61..898e78c 100644
--- a/core/tests/coretests/src/android/text/FontFallbackSetup.java
+++ b/core/tests/coretests/src/android/text/FontFallbackSetup.java
@@ -33,7 +33,7 @@
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 @@
}
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);
diff --git a/graphics/java/android/graphics/fonts/Font.java b/graphics/java/android/graphics/fonts/Font.java
index 8aa4845..1458c66 100644
--- a/graphics/java/android/graphics/fonts/Font.java
+++ b/graphics/java/android/graphics/fonts/Font.java
@@ -119,7 +119,7 @@
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 @@
* @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 @@
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 @@
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 @@
* @return a locale list
*/
public @NonNull LocaleList getLocaleList() {
- return mLocaleList;
+ return LocaleList.forLanguageTags(mLocaleList);
}
/** @hide */
@@ -580,7 +580,7 @@
+ ", italic=" + mItalic
+ ", ttcIndex=" + mTtcIndex
+ ", axes=" + FontVariationAxis.toFontVariationSettings(mAxes)
- + ", localeList=" + mLocaleList.toLanguageTags()
+ + ", localeList=" + mLocaleList
+ ", buffer=" + mBuffer
+ "}";
}
diff --git a/graphics/java/android/graphics/fonts/SystemFonts.java b/graphics/java/android/graphics/fonts/SystemFonts.java
index 1c957b8..f4a2199 100644
--- a/graphics/java/android/graphics/fonts/SystemFonts.java
+++ b/graphics/java/android/graphics/fonts/SystemFonts.java
@@ -19,7 +19,6 @@
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 @@
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 @@
* @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 @@
@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 @@
@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 @@
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 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 @@
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);
}
}