From 0a843d01f148fc594112afd89b845dad9765e587 Mon Sep 17 00:00:00 2001 From: ojeda-e Date: Tue, 14 Dec 2021 20:20:20 -0700 Subject: [PATCH 1/5] moved cache to Text attribute. --- lib/matplotlib/tests/test_text.py | 16 +++++++++++++++- lib/matplotlib/text.py | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index cfa2566173aa..231ca704f676 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -5,7 +5,7 @@ import numpy as np from numpy.testing import assert_almost_equal import pytest - +import time import matplotlib as mpl from matplotlib.backend_bases import MouseEvent from matplotlib.font_manager import FontProperties @@ -756,3 +756,17 @@ def test_pdf_chars_beyond_bmp(): plt.rcParams['mathtext.fontset'] = 'stixsans' plt.figure() plt.figtext(0.1, 0.5, "Mass $m$ \U00010308", size=30) + + +def test_cache_large_labels(): + """Test to verify cache helps when ticks are too large""" + times = [] + for pow in range(1, 5): + labels = [i for i in range(10**pow)] + t0 = time.perf_counter() + plt.figure() + plt.xticks(labels) + plt.yticks(labels) + times.append(time.perf_counter()-t0) + assert times[-1] > times[0] + assert times[-1] > times[-2] diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index c4b3a9184a3a..81310572ac63 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -107,7 +107,6 @@ class Text(Artist): """Handle storing and drawing of text in window or data coordinates.""" zorder = 3 - _cached = cbook.maxdict(50) def __repr__(self): return "Text(%s, %s, %s)" % (self._x, self._y, repr(self._text)) @@ -158,6 +157,7 @@ def __init__(self, self._linespacing = linespacing self.set_rotation_mode(rotation_mode) self.update(kwargs) + self._cached = cbook.maxdict(50) def update(self, kwargs): # docstring inherited From 5af1662e3a9a3f4b5b2c9e4011a58325f6b3e565 Mon Sep 17 00:00:00 2001 From: ojeda-e Date: Wed, 15 Dec 2021 12:39:59 -0700 Subject: [PATCH 2/5] Added __setstate__ in Text to pickle weakref --- lib/matplotlib/text.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 81310572ac63..5948de664029 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -177,8 +177,13 @@ def __getstate__(self): d = super().__getstate__() # remove the cached _renderer (if it exists) d['_renderer'] = None + d['_cached'] = None return d + def __setstate__(self, state): + self.__dict__.update(state) + self._cached = cbook.maxdict(50) + def contains(self, mouseevent): """ Return whether the mouse event occurred inside the axis-aligned From 7399b7f5587ca46e37d82fa6cfd6a707413cbf76 Mon Sep 17 00:00:00 2001 From: ojeda-e Date: Wed, 15 Dec 2021 16:15:22 -0700 Subject: [PATCH 3/5] Replaced maxdict by dict. Added draw_without_rendering in test. --- lib/matplotlib/tests/test_text.py | 3 ++- lib/matplotlib/text.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 231ca704f676..3546532b9725 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -761,12 +761,13 @@ def test_pdf_chars_beyond_bmp(): def test_cache_large_labels(): """Test to verify cache helps when ticks are too large""" times = [] + fig, _ = plt.subplots() for pow in range(1, 5): labels = [i for i in range(10**pow)] t0 = time.perf_counter() - plt.figure() plt.xticks(labels) plt.yticks(labels) + fig.draw_without_rendering() times.append(time.perf_counter()-t0) assert times[-1] > times[0] assert times[-1] > times[-2] diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 5948de664029..29dc1db31b8a 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -157,7 +157,7 @@ def __init__(self, self._linespacing = linespacing self.set_rotation_mode(rotation_mode) self.update(kwargs) - self._cached = cbook.maxdict(50) + self._cached = dict() def update(self, kwargs): # docstring inherited @@ -182,7 +182,7 @@ def __getstate__(self): def __setstate__(self, state): self.__dict__.update(state) - self._cached = cbook.maxdict(50) + self._cached = dict() def contains(self, mouseevent): """ From 15b28c012274db3833fd3b2a52590d79b03e3d1a Mon Sep 17 00:00:00 2001 From: ojeda-e Date: Thu, 16 Dec 2021 12:36:51 -0700 Subject: [PATCH 4/5] Replaced setstate with dict. --- lib/matplotlib/text.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 29dc1db31b8a..987109ba5be2 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -177,12 +177,9 @@ def __getstate__(self): d = super().__getstate__() # remove the cached _renderer (if it exists) d['_renderer'] = None - d['_cached'] = None + d['_cached'] = {} return d - def __setstate__(self, state): - self.__dict__.update(state) - self._cached = dict() def contains(self, mouseevent): """ From cc4307b16aba08a688c3b8b62e408a640ce38853 Mon Sep 17 00:00:00 2001 From: ojeda-e Date: Thu, 16 Dec 2021 12:43:01 -0700 Subject: [PATCH 5/5] fixed Flake8 --- lib/matplotlib/text.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 987109ba5be2..f3acd20f253c 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -180,7 +180,6 @@ def __getstate__(self): d['_cached'] = {} return d - def contains(self, mouseevent): """ Return whether the mouse event occurred inside the axis-aligned