Repository navigation
Description
There are some inconsistencies and inefficiencies with glyph caching.
GlyphCache.get_glyph caches the code point with a value of None before calling GlyphCache.load_glyphs [1]. If the glyph is not found by load_glyphs then its cache entry will remain None. This has the side effect of preventing cache misses on future calls to get_glyph for that code point, which is good [2].
However, because of get_glyph's preemptive caching, load_glyphs must treat None as meaning not present in the font OR never before loaded rather than simply not present in the font [3] [4]. This means that it's impossible for load_glyphs to short-circuit on code points that it should be able to know are not present in the font. This is different behavior than get_glyph and it undermines the good side effect mentioned above.
It's understandable, then, that the BDF and PCF implementations of load_glyphs do not set cache values of None for code points that are not found in the font. Doing so would be futile. The lvfontbin implementation does cache unknown code points as None but given the current implementation of get_glyph that fact can't be leveraged [5].
All of this leads to unnecessary and expensive file scans when a glyph that we know doesn't exist is loaded again.
If we reframe None to mean only that the code point is not present in the font then we can 1) prevent unnecessary file scans and 2) better separate the concerns of get_glyph and load_glyphs. The current implementation requires get_glyph and load_glyphs to be aware of the implementation details of the other.
With that done we can change the BDF and PCF implementations to cache None for code points that are not found during loading. get_glyph of course won't need to (in fact, absolutely should not) preemptively cache None.
[1]
| self._glyphs[code_point] = None |
[2]
| if code_point in self._glyphs: |
[3]
| if code_point in self._glyphs and self._glyphs[code_point]: |
[4]
| code_points = sorted(c for c in code_points if self._glyphs.get(c, None) is None) |
[5]
| self._glyphs[code_point] = None |
Activity
Cache None for code points not found in font to prevent unnecessary f…
Cache None for code points not found in font to prevent unnecessary f…