question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimizing fontTools.ttLib.TTFont.getGlyphID

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
anthrotypecommented, Mar 13, 2019

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:

  1. one hasattr check (line 566),
  2. one call to getGlyphOrder, which in turn simply returns self.glyphOrder (line 568),
  3. one dict contains check (line 570)
  4. one dict getitem call (line 602),
  5. and one string comparison (line 603).

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 the getGlyphID 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 with self._reversedGlyphOrderDict it’s not fontTools’ fault.

0reactions
behdadcommented, Aug 21, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

ttLib: Read/write OpenType and TrueType fonts ttFont - fontTools
This means that simple operations can be extremely fast. Example usage: >> from fontTools import ttLib >> tt = ttLib.TTFont("afont.ttf") # Load an...
Read more >
fonttools · PyPI
fontTools is a library for manipulating fonts, written in Python. The project includes the TTX tool, that can convert TrueType and OpenType fonts...
Read more >
fonttools [python-library] - Occam :: Details
| fontTools is a library for manipulating fonts, written in Python. The project includes the TTX tool, that can convert TrueType and OpenType...
Read more >
Font Eng 101: The Basics
TTX, a commandline tool to convert fonts to/from xml; TTFont, ... fonts for this example python >>> from fontTools import ttLib >>> font...
Read more >
Is there some python tool or software tool through which i can ...
from fontTools.ttLib import TTFont from collections import defaultdict font = TTFont('path/to/fontfile.ttf') unicode_map = font.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found