Skip to content

Commit

Permalink
Patch usage of maxdict with functools.lru_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
agriyakhetarpal committed Nov 28, 2024
1 parent 00ae48d commit 1b57803
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/matplotlib-pyodide/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package:
source:
url: https://files.pythonhosted.org/packages/36/8e/f28cdc02d2eb5d9a711876c39be72701b09ec80ce9ea3a6b67a7f08d8212/matplotlib_pyodide-0.2.2-py3-none-any.whl
sha256: 712a1bd8a06875393f25b0dc7a35cff1ecde2df36446d1290aaa846fd225a5df
patches:
- patches/0001-Replace-maxdict-with-lru_cache.patch # remove with 0.2.3
about:
home: https://github.com/pyodide/matplotlib-pyodide
PyPI: https://pypi.org/project/matplotlib-pyodide
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
From 18cd638fb0b70078c0e6bedc4a98331ac7652f33 Mon Sep 17 00:00:00 2001
From: Hood Chatham <roberthoodchatham@gmail.com>
Date: Tue, 22 Oct 2024 14:27:43 +0200
Subject: [PATCH 01/19] Replace maxdict with lru_cache (#52)

Matplotlib removed maxdict:
https://matplotlib.org/stable/api/prev_api_changes/api_changes_3.6.0.html#miscellaneous-internals
---
matplotlib_pyodide/html5_canvas_backend.py | 30 +++++++++++-----------
1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/matplotlib_pyodide/html5_canvas_backend.py b/matplotlib_pyodide/html5_canvas_backend.py
index 740e679..ac3afd5 100644
--- a/matplotlib_pyodide/html5_canvas_backend.py
+++ b/matplotlib_pyodide/html5_canvas_backend.py
@@ -1,6 +1,7 @@
import base64
import io
import math
+from functools import lru_cache

import numpy as np
from matplotlib import __version__, interactive
@@ -10,7 +11,6 @@ from matplotlib.backend_bases import (
RendererBase,
_Backend,
)
-from matplotlib.cbook import maxdict
from matplotlib.colors import colorConverter, rgb2hex
from matplotlib.font_manager import findfont
from matplotlib.ft2font import LOAD_NO_HINTING, FT2Font
@@ -204,8 +204,8 @@ class RendererHTMLCanvas(RendererBase):
self.ctx.width = self.width
self.ctx.height = self.height
self.dpi = dpi
- self.fontd = maxdict(50)
self.mathtext_parser = MathTextParser("bitmap")
+ self._get_font_helper = lru_cache(maxsize=50)(self._get_font_helper)

# Keep the state of fontfaces that are loading
self.fonts_loading = {}
@@ -309,22 +309,22 @@ class RendererHTMLCanvas(RendererBase):
pixels_proxy.destroy()
pixels_buf.release()

+ def _get_font_helper(self, prop):
+ """Cached font lookup
+
+ We wrap this in an lru-cache in the constructor.
+ """
+ fname = findfont(prop)
+ font = FT2Font(str(fname))
+ font_file_name = fname.rpartition("/")[-1]
+ return (font, font_file_name)
+
def _get_font(self, prop):
- key = hash(prop)
- font_value = self.fontd.get(key)
- if font_value is None:
- fname = findfont(prop)
- font_value = self.fontd.get(fname)
- if font_value is None:
- font = FT2Font(str(fname))
- font_file_name = fname[fname.rfind("/") + 1 :]
- font_value = font, font_file_name
- self.fontd[fname] = font_value
- self.fontd[key] = font_value
- font, font_file_name = font_value
+ result = self._get_font_helper(prop)
+ font = result[0]
font.clear()
font.set_size(prop.get_size_in_points(), self.dpi)
- return font, font_file_name
+ return result

def get_text_width_height_descent(self, s, prop, ismath):
w: float
--
2.39.5 (Apple Git-154)

0 comments on commit 1b57803

Please sign in to comment.