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

Replace maxdict with lru_cache #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hoodmane
Copy link
Member

@hoodmane hoodmane commented Feb 10, 2024

hoodmane added a commit to hoodmane/pyodide that referenced this pull request Feb 10, 2024
Requires an update to matplotlib-pyodide before it will work:
pyodide/matplotlib-pyodide#52
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (main@61935f7). Click here to learn what that means.

Additional details and impacted files
@@          Coverage Diff           @@
##             main     #52   +/-   ##
======================================
  Coverage        ?   0.00%           
======================================
  Files           ?       4           
  Lines           ?     648           
  Branches        ?       0           
======================================
  Hits            ?       0           
  Misses          ?     648           
  Partials        ?       0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@ryanking13 ryanking13 left a comment

Choose a reason for hiding this comment

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

Thanks!

self.mathtext_parser = MathTextParser("bitmap")
self._get_font_helper = lru_cache(maxsize=50)(self._get_font_helper)
Copy link
Member

Choose a reason for hiding this comment

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

What's the difference between adding a lru_cache decorator on top of self._get_font_helper, or re-defining a variable like this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Two things:

  1. Decorating the method makes one shared cache across all class instances, this makes a per-instance cache. It actually might make more sense to have a shared cache here but I don't understand what's going on well enough to know.
  2. The cache holds a strong reference to the arguments of the function including theself argument so if you decorate the method directly then instances can be kept alive by the cache. This instead makes a reference loop but the gc will collect it.

It would probably be better to shift this to a top level or static method and use a shared cache.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess the main concern is that the font objects appear to be mutable. I wonder if there is a good way to copy them so we can avoid mutating cache entries...

Copy link
Member

Choose a reason for hiding this comment

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

I see. Thanks for the explanation!

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.

None yet

3 participants