Create a thread_local cache for textLocale
Bug:22378829 toLanguageTag is significantly more expensive than previously thought (note ULOC_FULLNAME_CAPACITY is 157) and weighs in at around 40us. Given that this is called on every Paint and that there are typically thousands of Paint objects created this adds up very quickly. Given that the locale is almost always Locale.getDefault(), a very simple thread_local cache of size 1 fixes this trivially Change-Id: I819e60cac7a4b27e9dd5538332c22ce5bbd0851c
This commit is contained in:
@@ -74,6 +74,13 @@ static void defaultSettingsForAndroid(Paint* paint) {
|
||||
paint->setTextEncoding(Paint::kGlyphID_TextEncoding);
|
||||
}
|
||||
|
||||
struct LocaleCacheEntry {
|
||||
std::string javaLocale;
|
||||
std::string languageTag;
|
||||
};
|
||||
|
||||
static thread_local LocaleCacheEntry sSingleEntryLocaleCache;
|
||||
|
||||
class PaintGlue {
|
||||
public:
|
||||
enum MoveOpt {
|
||||
@@ -399,10 +406,14 @@ public:
|
||||
static void setTextLocale(JNIEnv* env, jobject clazz, jlong objHandle, jstring locale) {
|
||||
Paint* obj = reinterpret_cast<Paint*>(objHandle);
|
||||
ScopedUtfChars localeChars(env, locale);
|
||||
char langTag[ULOC_FULLNAME_CAPACITY];
|
||||
toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeChars.c_str());
|
||||
if (sSingleEntryLocaleCache.javaLocale != localeChars.c_str()) {
|
||||
sSingleEntryLocaleCache.javaLocale = localeChars.c_str();
|
||||
char langTag[ULOC_FULLNAME_CAPACITY];
|
||||
toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeChars.c_str());
|
||||
sSingleEntryLocaleCache.languageTag = langTag;
|
||||
}
|
||||
|
||||
obj->setTextLocale(langTag);
|
||||
obj->setTextLocale(sSingleEntryLocaleCache.languageTag);
|
||||
}
|
||||
|
||||
static jboolean isElegantTextHeight(JNIEnv* env, jobject paint) {
|
||||
|
||||
Reference in New Issue
Block a user