Skip to content

Cache None for code points not found in font to prevent unnecessary file scans - #76

Merged
tannewt merged 4 commits into
adafruit:mainfrom
mtraver:glyph-cache-and-tests
May 21, 2026
Merged

tannewt merged 4 commits into
adafruit:mainfrom
mtraver:glyph-cache-and-tests

Conversation

@mtraver

@mtraver mtraver commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

The current implementation treats None as meaning not present in the font OR never before loaded rather than simply not present in the font. This means that a subsequent call to load_glyphs for a code point that had been loaded before and not found will trigger another scan of the file, only to not be found again.

This PR:

  1. Changes the meaning of None in the cache to mean simply not present in the font.
  2. In the BDF and PCF implementations, caches None for code points not found in the font. The lvfontbin implementation already does this. The TTF implementation doesn't load glyphs so this is not yet relevant.
  3. Removes the preemptive caching of None in get_glyph.
  4. Adds tests. There were already mocks written for displayio.Bitmap and fontio.Glyph so the tests use those. The test/ dir is also renamed to tests/ because CI only runs pytest if the dir tests/ is present.

This PR also cleans up and clarifies type checking imports so that tests run using the mocked displayio.Bitmap.

Finally, there were changes required by the ruff check pre-commit check, so some type annotations are updated from e.g. Optional[int] to int | None etc.

Closes #75

@mtraver
mtraver force-pushed the glyph-cache-and-tests branch 3 times, most recently from 519a1df to 3bfc80a Compare May 21, 2026 17:51
mtraver added 3 commits May 21, 2026 11:40
…ile scans

The current implementation treats None as meaning not present in the
font OR never before loaded rather than simply not present in the font.
This means that a subsequent call to load_glyphs for a code point that
had been loaded before and not found will trigger another scan of the
file, only to not be found again.

This commit:
1. Changes the meaning of None in the cache to mean simply not
present in the font.
2. In the BDF and PCF implementations, caches None for code points not
found in the font. The lvfontbin implementation already does this. The
TTF implementation doesn't load glyphs so this is not yet relevant.
3. Removes the preemptive caching of None in get_glyph.
4. Adds tests. There were already mocks written for displayio.Bitmap and
fontio.Glyph so the tests use those.

This commit also cleans up and clarifies type checking imports so that
tests run using the mocked displayio.Bitmap.

Closes adafruit#75
CI only runs tests if the dir tests/ is present.
@mtraver
mtraver force-pushed the glyph-cache-and-tests branch from 45534d5 to 3a8c0a4 Compare May 21, 2026 18:40
Comment thread tests/test_bitmap_font.py Resolved
@dhalbert
dhalbert requested review from FoamyGuy and tannewt and removed request for FoamyGuy May 21, 2026 19:10

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Excited for the speedup 🚀

@tannewt
tannewt merged commit 94bec4b into adafruit:main May 21, 2026
1 check passed
@mtraver
mtraver deleted the glyph-cache-and-tests branch May 21, 2026 22:29
adafruit-adabot pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Aug 15, 2026
Updating https://github.com/adafruit/Adafruit_CircuitPython_EPD to 2.20.0 from 2.19.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#112 from mikeysklar/feather-epd-grayscale4-1in54-breakout
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#108 from mikeysklar/feather-epd-grayscale4-2in66-6392
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#110 from mikeysklar/feather-epd-grayscale4-2in13-6383
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#111 from mikeysklar/feather-epd-grayscale4-4in2-6381
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#105 from mikeysklar/feather-epd-grayscale4-2in13-breakout
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#106 from mikeysklar/feather-epd-grayscale4-2in9
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#107 from mikeysklar/feather-epd-grayscale4-2in13-featherwing
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#104 from mikeysklar/ssd1680-grayscale4-gdey0213b74

Updating https://github.com/adafruit/Adafruit_CircuitPython_GPS to 3.11.10 from 3.11.9:
  > Merge pull request adafruit/Adafruit_CircuitPython_GPS#128 from grgrant/fix-gtop_i2c_edge_case

Updating https://github.com/adafruit/Adafruit_CircuitPython_LIS331 to 1.1.0 from 1.0.25:
  > Merge pull request adafruit/Adafruit_CircuitPython_LIS331#10 from FoamyGuy/alternate_chip_id

Updating https://github.com/adafruit/Adafruit_CircuitPython_NAU88L21 to 1.0.1 from 1.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_NAU88L21#1 from FoamyGuy/left_justified_change

Updating https://github.com/adafruit/Adafruit_CircuitPython_SSD1680 to 2.2.0 from 2.1.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1680#46 from mikeysklar/ssd1683-4.2-6381
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1680#45 from mikeysklar/grayscale4-2in13-bare-6383
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1680#44 from mikeysklar/grayscale4-2in66-6392
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1680#43 from mikeysklar/grayscale4-2in13-featherwing
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1680#42 from mikeysklar/grayscale4-2in9-featherwing
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1680#41 from mikeysklar/grayscale4-breakout-2in13

Updating https://github.com/adafruit/Adafruit_CircuitPython_SSD1681 to 2.1.0 from 2.0.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1681#17 from mikeysklar/grayscale4-1in54-breakout

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font to 2.4.3 from 2.4.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_Bitmap_Font#76 from mtraver/glyph-cache-and-tests

Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Emoji_Text to 1.0.5 from 1.0.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Emoji_Text#7 from FoamyGuy/fix_xy_props

Updating https://github.com/adafruit/Adafruit_CircuitPython_Pixelbuf to 2.1.0 from 2.0.12:
  > Merge pull request adafruit/Adafruit_CircuitPython_Pixelbuf#15 from giri256/feat/slice-single-color

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyBadger to 4.0.10 from 4.0.9:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyBadger#74 from FoamyGuy/fix_docs_build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Caching of unknown glyphs is inconsistent

3 participants