* 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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;
-
};