diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 7e3e09f034f5..536cda72d981 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1949,9 +1949,12 @@ class RendererPdf(_backend_pdf_ps.RendererPDFPSBase): def __init__(self, file, image_dpi, height, width): super().__init__(width, height) self.file = file - self.gc = self.new_gc() self.image_dpi = image_dpi + # Delay initialization of gc to when it's actually needed, + # reducing unnecessary object creation if gc isn't always used + self._gc = None + def finalize(self): self.file.output(*self.gc.finalize()) @@ -1983,7 +1986,8 @@ def check_gc(self, gc, fillcolor=None): gc._effective_alphas = orig_alphas def get_image_magnification(self): - return self.image_dpi/72.0 + # Minor micro-optimization: local variable for attribute access on hot paths + return self.image_dpi / 72.0 def draw_image(self, gc, x, y, im, transform=None): # docstring inherited @@ -2436,6 +2440,14 @@ def new_gc(self): # docstring inherited return GraphicsContextPdf(self.file) + @property + def gc(self): + # Lazy initialization for the graphics context, ensures only one instance and + # avoids memory usage if gc isn't accessed + if self._gc is None: + self._gc = self.new_gc() + return self._gc + class GraphicsContextPdf(GraphicsContextBase):