From c0016a8581f3124d34dc8cf0a2a5c3374e72356a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 16 Aug 2021 14:28:10 +1000 Subject: BLF: avoid unnecessary lookups in blf_kerning_cache_new blf_kerning_cache_new was performing many unnecessary hash lookups, calling blf_glyph_search 32768 times. Use a lookup table to reduce this to the number of ASCII characters (128 calls). --- source/blender/blenfont/intern/blf_glyph.c | 44 ++++++++++++++++-------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'source/blender/blenfont/intern') diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c index a2571860c94..c5abc5982e8 100644 --- a/source/blender/blenfont/intern/blf_glyph.c +++ b/source/blender/blenfont/intern/blf_glyph.c @@ -72,34 +72,36 @@ KerningCacheBLF *blf_kerning_cache_find(FontBLF *font) /* Create a new glyph cache for the current kerning mode. */ KerningCacheBLF *blf_kerning_cache_new(FontBLF *font, GlyphCacheBLF *gc) { - KerningCacheBLF *kc; - - kc = (KerningCacheBLF *)MEM_callocN(sizeof(KerningCacheBLF), "blf_kerning_cache_new"); + KerningCacheBLF *kc = MEM_mallocN(sizeof(KerningCacheBLF), __func__); kc->next = NULL; kc->prev = NULL; kc->mode = font->kerning_mode; - unsigned int i, j; - for (i = 0; i < KERNING_CACHE_TABLE_SIZE; i++) { - for (j = 0; j < KERNING_CACHE_TABLE_SIZE; j++) { - GlyphBLF *g = blf_glyph_search(gc, i); - if (!g) { - FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); - g = blf_glyph_add(font, gc, glyph_index, i); + GlyphBLF *g_table[KERNING_CACHE_TABLE_SIZE]; + for (uint i = 0; i < KERNING_CACHE_TABLE_SIZE; i++) { + GlyphBLF *g = blf_glyph_search(gc, i); + if (UNLIKELY(g == NULL)) { + FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); + g = blf_glyph_add(font, gc, glyph_index, i); + } + g_table[i] = g; + } + + memset(kc->ascii_table, 0, sizeof(kc->ascii_table)); + for (uint i = 0; i < KERNING_CACHE_TABLE_SIZE; i++) { + GlyphBLF *g = g_table[i]; + if (g == NULL) { + continue; + } + for (uint j = 0; j < KERNING_CACHE_TABLE_SIZE; j++) { + GlyphBLF *g_prev = g_table[j]; + if (g_prev == NULL) { + continue; } - /* Can fail on certain fonts */ - GlyphBLF *g_prev = blf_glyph_search(gc, j); - - FT_Vector delta = { - .x = 0, - .y = 0, - }; - if (g && g_prev && FT_Get_Kerning(font->face, g_prev->idx, g->idx, kc->mode, &delta) == 0) { + FT_Vector delta; + if (FT_Get_Kerning(font->face, g_prev->idx, g->idx, kc->mode, &delta) == 0) { kc->ascii_table[i][j] = (int)delta.x >> 6; } - else { - kc->ascii_table[i][j] = 0; - } } } -- cgit v1.2.3