diff options
| author | Adam Turner <9087854+aa-turner@users.noreply.github.com> | 2023-01-02 11:15:31 +0000 |
|---|---|---|
| committer | Adam Turner <9087854+aa-turner@users.noreply.github.com> | 2023-01-02 17:38:19 +0000 |
| commit | 085a29335778bb9143e83e7d751ecce035f1ec32 (patch) | |
| tree | ca048989cccdaaa46fdf07e5482627687782a3a6 /sphinx | |
| parent | b26b9ba9711dbada32051846217fc76d6012fd81 (diff) | |
| download | sphinx-git-085a29335778bb9143e83e7d751ecce035f1ec32.tar.gz | |
Don't re-read doctrees from disk unnecessarily
Cache the loaded doctree and deepcopy on return
Diffstat (limited to 'sphinx')
| -rw-r--r-- | sphinx/environment/__init__.py | 17 |
1 files changed, 12 insertions, 5 deletions
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 |
