Skip to content

Commit

Permalink
Cache doctrees between reading and writing phases
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 4, 2023
1 parent a9b0f27 commit 463a696
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sphinx/builders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ def write_doctree(self, docname: str, doctree: nodes.document) -> None:
with open(doctree_filename, 'wb') as f:
pickle.dump(doctree, f, pickle.HIGHEST_PROTOCOL)

self.env._write_doc_doctree_cache[docname] = doctree

def write(
self,
build_docnames: Iterable[str],
Expand Down
10 changes: 9 additions & 1 deletion sphinx/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ def __init__(self, app: Sphinx):
# docname -> pickled doctree
self._pickled_doctree_cache: dict[str, bytes] = {}

# docname -> doctree
self._write_doc_doctree_cache: dict[str, nodes.document] = {}

# File metadata
# docname -> dict of metadata items
self.metadata: dict[str, dict[str, Any]] = defaultdict(dict)
Expand Down Expand Up @@ -608,7 +611,12 @@ def get_and_resolve_doctree(
toctrees and return it.
"""
if doctree is None:
doctree = self.get_doctree(docname)
try:
doctree = self._write_doc_doctree_cache.pop(docname)
doctree.settings.env = self
doctree.reporter = LoggingReporter(self.doc2path(docname))
except KeyError:
doctree = self.get_doctree(docname)

# resolve all pending cross-references
self.apply_post_transforms(doctree, docname)
Expand Down

0 comments on commit 463a696

Please sign in to comment.