From 717060b076350ea811153290281075396a554fed Mon Sep 17 00:00:00 2001 From: Fabrice Di Meglio Date: Tue, 27 Sep 2011 15:53:42 -0700 Subject: [PATCH] Improve TextLayoutCache performances a bit - the gain is about 5% and the timing is more stable - use compare_type() and strictly_order_type() Change-Id: Iab81869a8ba461ce786a468b6c59b8f34e8db838 --- core/jni/android/graphics/TextLayoutCache.cpp | 46 +++++++++++-------- core/jni/android/graphics/TextLayoutCache.h | 17 +++++-- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/core/jni/android/graphics/TextLayoutCache.cpp b/core/jni/android/graphics/TextLayoutCache.cpp index 86ae7d2ae05f9..3c70c301844fd 100644 --- a/core/jni/android/graphics/TextLayoutCache.cpp +++ b/core/jni/android/graphics/TextLayoutCache.cpp @@ -251,26 +251,32 @@ TextLayoutCacheKey::TextLayoutCacheKey(const TextLayoutCacheKey& other) : } } -bool TextLayoutCacheKey::operator<(const TextLayoutCacheKey& rhs) const { - LTE_INT(count) { - LTE_INT(typeface) { - LTE_FLOAT(textSize) { - LTE_FLOAT(textSkewX) { - LTE_FLOAT(textScaleX) { - LTE_INT(flags) { - LTE_INT(hinting) { - LTE_INT(dirFlags) { - return memcmp(getText(), rhs.getText(), - count * sizeof(UChar)) < 0; - } - } - } - } - } - } - } - } - return false; +int TextLayoutCacheKey::compare(const TextLayoutCacheKey& lhs, const TextLayoutCacheKey& rhs) { + int deltaInt = lhs.count - rhs.count; + if (deltaInt != 0) return (deltaInt); + + if (lhs.typeface < rhs.typeface) return -1; + if (lhs.typeface > rhs.typeface) return +1; + + if (lhs.textSize < rhs.textSize) return -1; + if (lhs.textSize > rhs.textSize) return +1; + + if (lhs.textSkewX < rhs.textSkewX) return -1; + if (lhs.textSkewX > rhs.textSkewX) return +1; + + if (lhs.textScaleX < rhs.textScaleX) return -1; + if (lhs.textScaleX > rhs.textScaleX) return +1; + + deltaInt = lhs.flags - rhs.flags; + if (deltaInt != 0) return (deltaInt); + + deltaInt = lhs.hinting - rhs.hinting; + if (deltaInt != 0) return (deltaInt); + + deltaInt = lhs.dirFlags - rhs.dirFlags; + if (deltaInt) return (deltaInt); + + return memcmp(lhs.getText(), rhs.getText(), lhs.count * sizeof(UChar)); } void TextLayoutCacheKey::internalTextCopy() { diff --git a/core/jni/android/graphics/TextLayoutCache.h b/core/jni/android/graphics/TextLayoutCache.h index 35dd6fd8ed175..6dda1e58b71d1 100644 --- a/core/jni/android/graphics/TextLayoutCache.h +++ b/core/jni/android/graphics/TextLayoutCache.h @@ -72,8 +72,6 @@ public: TextLayoutCacheKey(const TextLayoutCacheKey& other); - bool operator<(const TextLayoutCacheKey& rhs) const; - /** * We need to copy the text when we insert the key into the cache itself. * We don't need to copy the text when we are only comparing keys. @@ -85,6 +83,8 @@ public: */ size_t getSize(); + static int compare(const TextLayoutCacheKey& lhs, const TextLayoutCacheKey& rhs); + private: const UChar* text; // if text is NULL, use textCopy String16 textCopy; @@ -97,11 +97,18 @@ private: uint32_t flags; SkPaint::Hinting hinting; - inline const UChar* getText() const { - return text ? text : textCopy.string(); - } + inline const UChar* getText() const { return text ? text : textCopy.string(); } + }; // TextLayoutCacheKey +inline int strictly_order_type(const TextLayoutCacheKey& lhs, const TextLayoutCacheKey& rhs) { + return TextLayoutCacheKey::compare(lhs, rhs) < 0; +} + +inline int compare_type(const TextLayoutCacheKey& lhs, const TextLayoutCacheKey& rhs) { + return TextLayoutCacheKey::compare(lhs, rhs); +} + /* * TextLayoutCacheValue is the Cache value */