Skip to content

feat: cache loaded fonts#486

Merged
tybulewicz merged 1 commit intomasterfrom
ttybulew/cache-fonts
Feb 26, 2026
Merged

feat: cache loaded fonts#486
tybulewicz merged 1 commit intomasterfrom
ttybulew/cache-fonts

Conversation

@tybulewicz
Copy link
Contributor

@tybulewicz tybulewicz commented Feb 20, 2026

What does this PR do?

This pull request implements font caching to optimize performance when rendering visualizations with many detections on large images. The solution addresses issue #484 by using functools.lru_cache to cache ImageFont objects, preventing repeated expensive font loading operations.

Fixes #484

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

@github-actions github-actions bot added the python python related changes label Feb 20, 2026
@tybulewicz tybulewicz requested a review from Copilot February 20, 2026 12:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request implements font caching to optimize performance when rendering visualizations with many detections on large images. The solution addresses issue #484 by using functools.lru_cache to cache ImageFont objects, preventing repeated expensive font loading operations.

Changes:

  • Added two new cached utility functions (default_font and truetype_font) in src/model_api/visualizer/utils.py using @lru_cache(maxsize=5) decorator
  • Updated all visualizer primitives (BoundingBox, Label, Keypoints, Overlay) to use the new cached font functions instead of directly calling ImageFont.load_default() or ImageFont.truetype()
  • Reorganized imports in scene files to import get_label_color_mapping from model_api.visualizer.utils instead of model_api.visualizer.scene.utils

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/model_api/visualizer/utils.py Added default_font and truetype_font cached utility functions with LRU cache
src/model_api/visualizer/primitive/bounding_box.py Updated to use cached default_font function
src/model_api/visualizer/primitive/label.py Updated to use cached default_font and truetype_font functions
src/model_api/visualizer/primitive/keypoints.py Updated to use cached default_font function
src/model_api/visualizer/primitive/overlay.py Updated to use cached default_font function
src/model_api/visualizer/scene/detection.py Updated import path for get_label_color_mapping
src/model_api/visualizer/scene/segmentation/instance_segmentation.py Updated import path for get_label_color_mapping
Comments suppressed due to low confidence (5)

src/model_api/visualizer/utils.py:8

  • The copyright header should appear before the imports. The codebase convention is to have the module docstring first, then the copyright header, and then the imports.
    src/model_api/visualizer/utils.py:48
  • Missing return type annotation. The function should specify its return type, such as ImageFont.FreeTypeFont or the appropriate type from PIL. The codebase uses return type annotations for similar functions (e.g., get_label_color_mapping on line 34 has -> dict[str, str]).
    src/model_api/visualizer/utils.py:61
  • Missing return type annotation. The function should specify its return type, such as ImageFont.FreeTypeFont or the appropriate type from PIL. The codebase uses return type annotations for similar functions (e.g., get_label_color_mapping on line 34 has -> dict[str, str]).
    src/model_api/visualizer/utils.py:67
  • Missing Returns section in the docstring. The docstring should include a Returns section that describes the return value, similar to the default_font function above (lines 54-55) which documents "A PIL ImageFont instance with the default font and specified size."
    src/model_api/visualizer/utils.py:69
  • The new cached font functions default_font and truetype_font lack test coverage. Since the visualizer module has comprehensive test coverage (see tests/unit/visualizer/test_primitive.py), consider adding tests to verify that the caching mechanism works correctly. The tests should verify that repeated calls with the same parameters return the same cached object, and that different parameters return different objects.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Base automatically changed from mgumowsk/GETI-480-adjusting-visualization-to-image-size to master February 24, 2026 11:39
@tybulewicz tybulewicz marked this pull request as ready for review February 24, 2026 12:13
@tybulewicz tybulewicz requested a review from a team as a code owner February 24, 2026 12:13
@tybulewicz tybulewicz added this pull request to the merge queue Feb 25, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 25, 2026
@tybulewicz tybulewicz added this pull request to the merge queue Feb 26, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 26, 2026
@tybulewicz tybulewicz added this pull request to the merge queue Feb 26, 2026
Merged via the queue into master with commit 4ef786b Feb 26, 2026
28 checks passed
@tybulewicz tybulewicz deleted the ttybulew/cache-fonts branch February 26, 2026 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python python related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache loaded fonts

3 participants