Commit b9df86a40833c02ba57ca66c7e9ed1511e43697b

sammy 2008-05-01T18:31:35

* Optimize FTCharmap::FontIndex() so that font indices < 128 are precomputed during FTCharmap() instantiation to avoid repeated tt_cmap4_char_index() calls. Performance patch by Sean Morrison, taken from bzflag commit r14644.

diff --git a/src/FTCharmap.cpp b/src/FTCharmap.cpp
index 36af57d..c4cd1b8 100644
--- a/src/FTCharmap.cpp
+++ b/src/FTCharmap.cpp
@@ -39,6 +39,11 @@ FTCharmap::FTCharmap(FTFace* face)
     }
 
     ftEncoding = ftFace->charmap->encoding;
+
+    for(int i = 0; i < FTCharmap::MAX_PRECOMPUTED; i++)
+    {
+        charIndexCache[i] = FT_Get_Char_Index(ftFace, i);
+    }
 }
 
 
@@ -76,6 +81,11 @@ unsigned int FTCharmap::GlyphListIndex(const unsigned int characterCode)
 
 unsigned int FTCharmap::FontIndex(const unsigned int characterCode)
 {
+    if(characterCode < FTCharmap::MAX_PRECOMPUTED)
+    {
+        return charIndexCache[characterCode];
+    }
+
     return FT_Get_Char_Index(ftFace, characterCode);
 }
 
diff --git a/src/FTCharmap.h b/src/FTCharmap.h
index 5e3e8fe..5b48213 100644
--- a/src/FTCharmap.h
+++ b/src/FTCharmap.h
@@ -150,10 +150,15 @@ class FTCharmap
         CharacterMap charMap;
 
         /**
+         * Precomputed font indices.
+         */
+        static const int MAX_PRECOMPUTED = 128;
+        unsigned int characterCodeIndices[MAX_PRECOMPUTED];
+
+        /**
          * Current error code.
          */
         FT_Error err;
-
 };