Optimizing fontTools.ttLib.TTFont.getGlyphID
See original GitHub issueI have a project with many thousands of glyphs, which translates to thousands of calls to fontTools.ttLib.TTFont.getGlyphID
when calling fontTools.ttLib.TTFont.save
. I was able to make it faster (17.66 s to 4.236 s of cumulative time) by adding the following to the beginning of the function.
try:
return self._reverseGlyphOrderDict[glyphName]
except (AttributeError, KeyError):
pass
The function is slow because it checks that _reverseGlyphOrderDict
and glyphOrder
are up to date and consistent. My version skips those checks. Is that safe? I think it is, for my use case, because I have finished creating glyphs by the time the function is first called.
Would it be possible to add something like this optimization to fontTools? If getGlyphID
is called because save
was called, it seems like it could assume that _reverseGlyphOrderDict
and glyphOrder
are frozen for the duration of save
.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (11 by maintainers)
Top GitHub Comments
i’m a bit surprised that calling getGlyphID is three times slower than directly looking up self._reverseGlyphOrderDict. For the most common case of a glyphName which is already contained in self._reverseGlyphOrderDict, and the latter being in sync with self.glyphOrder, the extra work is basically:
I wonder why we care about checking the consistency between glyphOrder and _reverseGlyphOrderDict every single time getGlyphID is used. Would it be enough to just have
setGlyphOrder
method be the only supported way of modifying the self.glyphOrder, and have that method invalidate (i.e. remove the attribute) the self._reverseGlyphOrderDict cache? Then thegetGlyphID
method can simply assume that whenever a_reverseGlyphOrderDict
attribute is present, it must be in sync with the glyphOrder, and do try get glyphName from it, except rebuilding the reverse order when missing.If a client messes directly with
self.glyphOrder
or withself._reversedGlyphOrderDict
it’s not fontTools’ fault.@dscorbett Can you test https://github.com/fonttools/fonttools/pull/2398 ?