From 631b286c7a31b595eb926194e117f2ff1e6a9f72 Mon Sep 17 00:00:00 2001 From: John Paul Jepko Date: Mon, 12 Dec 2022 02:22:28 -0500 Subject: [PATCH] Implement nested four-level cache Prevents putting too many files in a single folder --- lib/matplotlib/texmanager.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index 4bd113767c23..54d724b328ec 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -176,8 +176,15 @@ def get_basefile(cls, tex, fontsize, dpi=None): Return a filename based on a hash of the string, fontsize, and dpi. """ src = cls._get_tex_source(tex, fontsize) + str(dpi) - return os.path.join( - cls.texcache, hashlib.md5(src.encode('utf-8')).hexdigest()) + filehash = hashlib.md5(src.encode('utf-8')).hexdigest() + filepath = Path(cls.texcache) + + num_letters, num_levels = 2, 2 + for i in range(0, num_letters*num_levels, num_letters): + filepath = filepath / Path(filehash[i:i+2]) + + filepath.mkdir(parents=True, exist_ok=True) + return os.path.join(filepath, filehash) @classmethod def get_font_preamble(cls):