Permalink
Browse files

Profiling: Turned the font glyph cache into a vector.

The fact of using a std::map for the glyph cache made the application
spend 12% of the time do find() calls on my own.

Turning this into a vector made this kind of calls disappear
from the profiling top list and should save a lot cycles.

The application should run a bit smoother now.
  • Loading branch information...
1 parent 060df27 commit 34e031a4f9ef0a8281674b7198c41f8b0a02d5cf Yohann Ferreira committed Sep 22, 2012
Showing with 17 additions and 11 deletions.
  1. +11 −7 src/engine/video/text.cpp
  2. +1 −1 src/engine/video/text.h
  3. +5 −3 src/engine/video/texture_controller.cpp
View
@@ -517,8 +517,8 @@ TextSupervisor::~TextSupervisor() {
TTF_CloseFont(fp->ttf_font);
if (fp->glyph_cache) {
- for (std::map<uint16, FontGlyph*>::iterator j = fp->glyph_cache->begin(); j != fp->glyph_cache->end(); j++) {
- delete (*j).second;
+ for (std::vector<FontGlyph*>::iterator j = fp->glyph_cache->begin(); j != fp->glyph_cache->end(); ++j) {
+ delete *j;
}
delete fp->glyph_cache;
}
@@ -571,7 +571,7 @@ bool TextSupervisor::LoadFont(const std::string& filename, const std::string& fo
fp->descent = TTF_FontDescent(font);
// Create the glyph cache for the font and add it to the font map
- fp->glyph_cache = new std::map<uint16, FontGlyph*>;
+ fp->glyph_cache = new std::vector<FontGlyph*>;
_font_map[font_name] = fp;
return true;
} // bool TextSupervisor::LoadFont(...)
@@ -756,8 +756,12 @@ void TextSupervisor::_CacheGlyphs(const uint16* text, FontProperties* fp) {
// A reference for legibility
const uint16& character = *character_ptr;
- // Check if glyph already cached. If so, move on to the next character
- if (fp->glyph_cache->find(character) != fp->glyph_cache->end())
+ // Update the glyph cache when needed
+ if (character >= fp->glyph_cache->size())
+ fp->glyph_cache->resize(character + 1, 0);
+
+ // Check if the glyph is already cached. If so, move on to the next character
+ if (fp->glyph_cache->at(character) != 0)
continue;
// Attempt to create the initial SDL_Surface that contains the rendered glyph
@@ -836,7 +840,7 @@ void TextSupervisor::_CacheGlyphs(const uint16* text, FontProperties* fp) {
glyph->max_y = static_cast<float>(initial->h + 1) / static_cast<float>(h);
glyph->advance = advance;
- fp->glyph_cache->insert(std::pair<uint16, FontGlyph*>(character, glyph));
+ (*fp->glyph_cache)[character] = glyph;
SDL_FreeSurface(initial);
SDL_FreeSurface(intermediary);
@@ -894,7 +898,7 @@ void TextSupervisor::_DrawTextHelper(const uint16* const text, FontProperties* f
// Iterate through each character in the string and render the character glyphs one at a time
int xpos = 0;
- for (const uint16* glyph = text; *glyph != 0; glyph++) {
+ for (const uint16* glyph = text; *glyph != 0; ++glyph) {
FontGlyph* glyph_info = (*fp->glyph_cache)[*glyph];
int x_hi = glyph_info->width;
View
@@ -105,7 +105,7 @@ class FontProperties {
TTF_Font* ttf_font;
//! \brief A pointer to a cache which holds all of the glyphs used in this font.
- std::map<uint16, FontGlyph*>* glyph_cache;
+ std::vector<FontGlyph*>* glyph_cache;
}; // class FontProperties
@@ -117,9 +117,11 @@ bool TextureController::UnloadTextures() {
FontProperties *fp = j->second;
if (fp->glyph_cache) {
- for (std::map<uint16, FontGlyph*>::iterator k = fp->glyph_cache->begin(); k != fp->glyph_cache->end(); k++) {
- _DeleteTexture((*k).second->texture);
- delete (*k).second;
+ for (std::vector<FontGlyph*>::iterator k = fp->glyph_cache->begin();
+ k != fp->glyph_cache->end(); ++k) {
+ if (*k)
+ _DeleteTexture((*k)->texture);
+ delete *k;
}
fp->glyph_cache->clear();

0 comments on commit 34e031a

Please sign in to comment.