Page MenuHome

BLF: Do Not Cache Unused Rendered Glyphs
ClosedPublic

Authored by Harley Acheson (harley) on Aug 11 2021, 2:39 AM.

Details

Summary

The loading of a font size or style renders bitmaps of the characters
0-255 and stores them in a cache. But glyphs 128-255 in this cache are
not accessible. What used to be ansi high-bit characters are now multi-
byte UTF-8 sequences.

Therefore this patch reduces the glyph_ascii_table size to 128 and
only caches characters 32-127, the visible portion of ASCII, which
greatly reduces the time to load a font.


GlyphCacheBLF has a general ListBase of glyphs but also has an array of 255 GlyphBLF called "glyph_ascii_table", used for "fast ascii lookup." Whenever we use a font size and style for the first time we call blf_font_ensure_ascii_table() which populates this array with rendered glyphs, in a loop 0...255.

But since adding support for UTF-8, we only use this table for glyphs under 128. macro BLF_UTF8_NEXT_FAST checks to see if the character is less than 128. If so it grabs the glyph from the glyph_ascii_table, otherwise loads it using a 32-bit unicode value and saves it to the regular cache. blf_font_draw_ascii_ex() assumes character under 128

I'm just not seeing a code path that will use glyphs 128-255. In one test, printing out a short word using 300 different fonts, that took 7.16 seconds, it took just 4.41 with this patch applied.

Diff Detail

Repository
rB Blender

Event Timeline

Harley Acheson (harley) requested review of this revision.Aug 11 2021, 2:39 AM
Harley Acheson (harley) created this revision.

LGTM, patch description should be updated fit the formatting of a commit log.

Include discussion about the patch below --- separator, see: https://wiki.blender.org/wiki/Process/Patch_Review


NOTE: a second cleanup commit could be made here that uses defines for ascii min/max range, which would have avoided this problem happening in the first place.
source/blender/blenfont/intern/blf_font.c
320–321

Include comment that this is the visible range of ASCII.

This revision is now accepted and ready to land.Aug 11 2021, 3:05 AM
Harley Acheson (harley) retitled this revision from BLF Ascii Table Too Large to BLF: Do Not Cache Unused Rendered Glyphs.
Harley Acheson (harley) edited the summary of this revision. (Show Details)

Updating comment as requested by @Campbell Barton (campbellbarton)

This revision was automatically updated to reflect the committed changes.