diff options
author | John Reck <jreck@google.com> | 2015-07-10 09:56:34 -0700 |
---|---|---|
committer | John Reck <jreck@google.com> | 2015-07-10 10:08:31 -0700 |
commit | 476f7158222e49ec24a5fc08f0bbb33fb8ca10bf (patch) | |
tree | 44c88b7a0e71ddeae9be4d326256b31429daf016 /core/jni | |
parent | 9613e9b76410b15b7f2700080a17476bf9f3461c (diff) | |
download | frameworks_base-476f7158222e49ec24a5fc08f0bbb33fb8ca10bf.zip frameworks_base-476f7158222e49ec24a5fc08f0bbb33fb8ca10bf.tar.gz frameworks_base-476f7158222e49ec24a5fc08f0bbb33fb8ca10bf.tar.bz2 |
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
Diffstat (limited to 'core/jni')
-rw-r--r-- | core/jni/android/graphics/Paint.cpp | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp index b9fd65f..bff1885 100644 --- a/core/jni/android/graphics/Paint.cpp +++ b/core/jni/android/graphics/Paint.cpp @@ -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) { |