[Bf-blender-cvs] [c0016a8581f] master: BLF: avoid unnecessary lookups in blf_kerning_cache_new

Campbell Barton noreply at git.blender.org
Mon Aug 16 06:36:05 CEST 2021


Commit: c0016a8581f3124d34dc8cf0a2a5c3374e72356a
Author: Campbell Barton
Date:   Mon Aug 16 14:28:10 2021 +1000
Branches: master
https://developer.blender.org/rBc0016a8581f3124d34dc8cf0a2a5c3374e72356a

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).

===================================================================

M	source/blender/blenfont/intern/blf_glyph.c

===================================================================

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;
-      }
     }
   }



More information about the Bf-blender-cvs mailing list