mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Don't re-read doctrees from disk unnecessarily
Cache the loaded doctree and deepcopy on return
This commit is contained in:
parent
b26b9ba971
commit
085a293357
@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import functools
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from copy import copy
|
from copy import copy, deepcopy
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from os import path
|
from os import path
|
||||||
from typing import TYPE_CHECKING, Any, Callable, Generator, Iterator
|
from typing import TYPE_CHECKING, Any, Callable, Generator, Iterator
|
||||||
@ -32,7 +33,6 @@ if TYPE_CHECKING:
|
|||||||
from sphinx.application import Sphinx
|
from sphinx.application import Sphinx
|
||||||
from sphinx.builders import Builder
|
from sphinx.builders import Builder
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
default_settings: dict[str, Any] = {
|
default_settings: dict[str, Any] = {
|
||||||
@ -558,9 +558,16 @@ class BuildEnvironment:
|
|||||||
|
|
||||||
def get_doctree(self, docname: str) -> nodes.document:
|
def get_doctree(self, docname: str) -> nodes.document:
|
||||||
"""Read the doctree for a file from the pickle and return it."""
|
"""Read the doctree for a file from the pickle and return it."""
|
||||||
filename = path.join(self.doctreedir, docname + '.doctree')
|
doctreedir = self.doctreedir
|
||||||
with open(filename, 'rb') as f:
|
|
||||||
doctree = pickle.load(f)
|
@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.settings.env = self
|
||||||
doctree.reporter = LoggingReporter(self.doc2path(docname))
|
doctree.reporter = LoggingReporter(self.doc2path(docname))
|
||||||
return doctree
|
return doctree
|
||||||
|
Loading…
Reference in New Issue
Block a user