summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeigo Nonaka <nona@google.com>2018-02-06 19:31:13 -0800
committerSeigo Nonaka <nona@google.com>2018-02-07 17:57:51 -0800
commit2a7b3974d16048f8c25ba63b640a81d678aa0175 (patch)
tree0ab60cd244f947a68a7828795a8d34487755e3f7
parent4de86391218f9fa2d1ba15d78cd80514fb5ce43d (diff)
downloadminikin-2a7b3974d16048f8c25ba63b640a81d678aa0175.tar.gz
Introduce own mutex for LocaleListCache
To be independent from gMinikinLock, introduce own private mutex for guarding mLocaleLists and mLocaleListLookupTable. Here is a raw performance score of walleye-userdebug. StaticLayout creation time: MeasuredText Balanced Hyphenation : 702,095 -> 707,951: (+0.8%) MeasuredText Balanced NoHyphenation: 534,731 -> 532,958: (-0.3%) MeasuredText Greedy Hyphenation : 483,748 -> 491,997: (+1.7%) MeasuredText Greedy NoHyphenation : 486,470 -> 487,135: (+0.1%) RandomText Balanced Hyphenation : 18,092,188 -> 17,977,875: (-0.6%) RandomText Balanced NoHyphenation : 7,622,978 -> 7,545,116: (-1.0%) RandomText Greedy Hyphenation : 7,535,832 -> 7,489,916: (-0.6%) RandomText Greedy NoHyphenation : 7,541,479 -> 7,546,804: (+0.1%) MeasuredText creation time: NoStyled Hyphenation : 17,957,290 -> 17,925,636: (-0.2%) NoStyled Hyphenation WidthOnly : 17,526,213 -> 17,427,896: (-0.6%) NoStyled NoHyphenation : 7,541,455 -> 7,532,890: (-0.1%) NoStyled NoHyphenation WidthOnly : 7,128,731 -> 7,082,616: (-0.6%) Styled Hyphenation : 14,843,198 -> 14,738,258: (-0.7%) Styled Hyphenation WidthOnly : 13,880,962 -> 13,858,992: (-0.2%) Styled NoHyphenation : 14,393,544 -> 14,365,111: (-0.2%) Styled NoHyphenation WidthOnly : 13,497,007 -> 13,504,562: (+0.1%) StaticLayout draw time: MeasuredText NoStyled : 666,351 -> 657,886: (-1.3%) MeasuredText NoStyled WithoutCache : 655,528 -> 658,249: (+0.4%) MeasuredText Styled : 865,664 -> 869,500: (+0.4%) MeasuredText Styled WithoutCache : 901,887 -> 900,078: (-0.2%) RandomText NoStyled : 576,268 -> 575,145: (-0.2%) RandomText NoStyled WithoutCache : 6,899,642 -> 6,819,789: (-1.2%) RandomText Styled : 2,936,847 -> 2,929,798: (-0.2%) RandomText Styled WithoutCache : 3,395,033 -> 3,411,809: (+0.5%) Bug: 37567215 Test: minikin_tests Change-Id: I7f55be10355fd34a02ea91bf67e0810079f30405
-rw-r--r--libs/minikin/LocaleListCache.cpp47
-rw-r--r--libs/minikin/LocaleListCache.h23
2 files changed, 37 insertions, 33 deletions
diff --git a/libs/minikin/LocaleListCache.cpp b/libs/minikin/LocaleListCache.cpp
index 625b4d2..c191ea6 100644
--- a/libs/minikin/LocaleListCache.cpp
+++ b/libs/minikin/LocaleListCache.cpp
@@ -103,46 +103,35 @@ static std::vector<Locale> parseLocaleList(const std::string& input) {
return result;
}
-// static
-uint32_t LocaleListCache::getId(const std::string& locales) {
- LocaleListCache* inst = LocaleListCache::getInstance();
- std::unordered_map<std::string, uint32_t>::const_iterator it =
- inst->mLocaleListLookupTable.find(locales);
- if (it != inst->mLocaleListLookupTable.end()) {
+LocaleListCache::LocaleListCache() {
+ // Insert an empty locale list for mapping default locale list to kEmptyListId.
+ // The default locale list has only one Locale and it is the unsupported locale.
+ mLocaleLists.emplace_back();
+ mLocaleListLookupTable.insert(std::make_pair("", kEmptyListId));
+}
+
+uint32_t LocaleListCache::getIdInternal(const std::string& locales) {
+ std::lock_guard<std::mutex> lock(mMutex);
+ const auto& it = mLocaleListLookupTable.find(locales);
+ if (it != mLocaleListLookupTable.end()) {
return it->second;
}
// Given locale list is not in cache. Insert it and return newly assigned ID.
- const uint32_t nextId = inst->mLocaleLists.size();
+ const uint32_t nextId = mLocaleLists.size();
LocaleList fontLocales(parseLocaleList(locales));
if (fontLocales.empty()) {
return kEmptyListId;
}
- inst->mLocaleLists.push_back(std::move(fontLocales));
- inst->mLocaleListLookupTable.insert(std::make_pair(locales, nextId));
+ mLocaleLists.push_back(std::move(fontLocales));
+ mLocaleListLookupTable.insert(std::make_pair(locales, nextId));
return nextId;
}
-// static
-const LocaleList& LocaleListCache::getById(uint32_t id) {
- LocaleListCache* inst = LocaleListCache::getInstance();
- MINIKIN_ASSERT(id < inst->mLocaleLists.size(), "Lookup by unknown locale list ID.");
- return inst->mLocaleLists[id];
-}
-
-// static
-LocaleListCache* LocaleListCache::getInstance() {
- assertMinikinLocked();
- static LocaleListCache* instance = nullptr;
- if (instance == nullptr) {
- instance = new LocaleListCache();
-
- // Insert an empty locale list for mapping default locale list to kEmptyListId.
- // The default locale list has only one Locale and it is the unsupported locale.
- instance->mLocaleLists.push_back(LocaleList());
- instance->mLocaleListLookupTable.insert(std::make_pair("", kEmptyListId));
- }
- return instance;
+const LocaleList& LocaleListCache::getByIdInternal(uint32_t id) {
+ std::lock_guard<std::mutex> lock(mMutex);
+ MINIKIN_ASSERT(id < mLocaleLists.size(), "Lookup by unknown locale list ID.");
+ return mLocaleLists[id];
}
} // namespace minikin
diff --git a/libs/minikin/LocaleListCache.h b/libs/minikin/LocaleListCache.h
index 9ebb111..2a007af 100644
--- a/libs/minikin/LocaleListCache.h
+++ b/libs/minikin/LocaleListCache.h
@@ -17,6 +17,7 @@
#ifndef MINIKIN_LOCALE_LIST_CACHE_H
#define MINIKIN_LOCALE_LIST_CACHE_H
+#include <mutex>
#include <unordered_map>
#include "Locale.h"
@@ -35,22 +36,36 @@ public:
// Returns the locale list ID for the given string representation of LocaleList.
// Caller should acquire a lock before calling the method.
- static uint32_t getId(const std::string& locales);
+ static inline uint32_t getId(const std::string& locales) {
+ return getInstance().getIdInternal(locales);
+ }
// Caller should acquire a lock before calling the method.
- static const LocaleList& getById(uint32_t id);
+ static inline const LocaleList& getById(uint32_t id) {
+ return getInstance().getByIdInternal(id);
+ }
private:
- LocaleListCache() {} // Singleton
+ LocaleListCache(); // Singleton
~LocaleListCache() {}
+ uint32_t getIdInternal(const std::string& locales);
+ const LocaleList& getByIdInternal(uint32_t id);
+
// Caller should acquire a lock before calling the method.
- static LocaleListCache* getInstance();
+ static LocaleListCache& getInstance() {
+ static LocaleListCache instance;
+ return instance;
+ }
std::vector<LocaleList> mLocaleLists;
// A map from the string representation of the font locale list to the ID.
std::unordered_map<std::string, uint32_t> mLocaleListLookupTable;
+
+ // To avoid dead-locking, need to acquire gMinikinLock before acquiring mMutex.
+ // TODO: Remove gMinikinLock
+ std::mutex mMutex;
};
} // namespace minikin