diff --git a/sphinx/environment/__init__.py b/sphinx/environment/__init__.py index a345971b5..1720dc161 100644 --- a/sphinx/environment/__init__.py +++ b/sphinx/environment/__init__.py @@ -2,10 +2,11 @@ from __future__ import annotations +import functools import os import pickle from collections import defaultdict -from copy import copy +from copy import copy, deepcopy from datetime import datetime from os import path from typing import TYPE_CHECKING, Any, Callable, Generator, Iterator @@ -32,7 +33,6 @@ if TYPE_CHECKING: from sphinx.application import Sphinx from sphinx.builders import Builder - logger = logging.getLogger(__name__) default_settings: dict[str, Any] = { @@ -558,9 +558,16 @@ class BuildEnvironment: def get_doctree(self, docname: str) -> nodes.document: """Read the doctree for a file from the pickle and return it.""" - filename = path.join(self.doctreedir, docname + '.doctree') - with open(filename, 'rb') as f: - doctree = pickle.load(f) + doctreedir = self.doctreedir + + @functools.lru_cache(maxsize=None) + def _load_doctree_from_disk(docname: str) -> nodes.document: + """Read the doctree for a file from the pickle and return it.""" + filename = path.join(doctreedir, docname + '.doctree') + with open(filename, 'rb') as f: + return pickle.load(f) + + doctree = deepcopy(_load_doctree_from_disk(docname)) doctree.settings.env = self doctree.reporter = LoggingReporter(self.doc2path(docname)) return doctree