Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement caching of fonts list to improve runtime performance #3316

Merged
merged 5 commits into from
Dec 10, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions manim/mobject/text/text_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ def construct(self):
Text('The horse does not eat cucumber salad.')

"""
font_list = []

def __init__(
self,
Expand All @@ -431,13 +432,16 @@ def __init__(
width: float = None,
should_center: bool = True,
disable_ligatures: bool = False,
use_svg_cache: bool = False,
**kwargs,
) -> None:
self.line_spacing = line_spacing
if font and warn_missing_font:
fonts_list = manimpango.list_fonts()
if font not in fonts_list:
logger.warning(f"Font {font} not in {fonts_list}.")
Text.font_list = (
Text.font_list if len(Text.font_list) > 0 else manimpango.list_fonts()
)
Copy link
Member

Choose a reason for hiding this comment

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

More of a semantical question: is there any particular argument against implementing this as a cached method of Text? Like

class Text(...):
    ...
    
    @staticmethod
    @functools.lru_cache(maxsize=None)
    def font_list() -> list[str]:
        return manimpango.list_fonts()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can bench it later on, good point but might make it still more inefficient because it checks the cache but it is also in c, so who knows what will happen.

I give report as soon as i benched it

if font not in Text.font_list:
logger.warning(f"Font {font} not in {Text.font_list}.")
self.font = font
self._font_size = float(font_size)
# needs to be a float or else size is inflated when font_size = 24
Expand Down Expand Up @@ -491,7 +495,7 @@ def __init__(
height=height,
width=width,
should_center=should_center,
use_svg_cache=False,
use_svg_cache=use_svg_cache,
**kwargs,
)
self.text = text
Expand Down Expand Up @@ -1157,9 +1161,11 @@ def __init__(
self.text = text
self.line_spacing = line_spacing
if font and warn_missing_font:
fonts_list = manimpango.list_fonts()
if font not in fonts_list:
logger.warning(f"Font {font} not in {fonts_list}.")
Text.font_list = (
Text.font_list if len(Text.font_list) > 0 else manimpango.list_fonts()
)
if font not in Text.font_list:
logger.warning(f"Font {font} not in {Text.font_list}.")
self.font = font
self._font_size = float(font_size)
self.slant = slant
Expand Down
Loading