Skip to content

Commit

Permalink
Closes sphinx-doc#12883: Performance improvement : Cache pre-pickled …
Browse files Browse the repository at this point in the history
…documents
  • Loading branch information
Arthur ATTOUT committed Sep 11, 2024
1 parent 0438178 commit f2c8b59
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions sphinx/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ def __init__(self, app: Sphinx) -> None:
# docnames to re-read unconditionally on next build
self.reread_always: set[str] = set()

self._pickled_doctree_cache: dict[str, bytes] = {}
"""In-memory cache for reading pickled doctrees from disk.
docname -> pickled doctree
self._pickled_doctree_cache: dict[str, nodes.document] = {}
"""In-memory cache for reading parsed doctrees from disk.
docname -> parsed doctree
This cache is used in the ``get_doctree`` method to avoid reading the
doctree from disk multiple times.
doctree from disk multiple times. Cached objects are pre-pickled using pickle.loads().
"""

self._write_doc_doctree_cache: dict[str, nodes.document] = {}
Expand Down Expand Up @@ -629,13 +629,14 @@ def get_domain(self, domainname: str) -> Domain:
def get_doctree(self, docname: str) -> nodes.document:
"""Read the doctree for a file from the pickle and return it."""
try:
serialised = self._pickled_doctree_cache[docname]
doctree = self._pickled_doctree_cache[docname]

except KeyError:
filename = path.join(self.doctreedir, docname + '.doctree')
with open(filename, 'rb') as f:
serialised = self._pickled_doctree_cache[docname] = f.read()
parsed = pickle.loads(f.read())
doctree = self._pickled_doctree_cache[docname] = parsed

doctree = pickle.loads(serialised)
doctree.settings.env = self
doctree.reporter = LoggingReporter(str(self.doc2path(docname)))
return doctree
Expand Down

0 comments on commit f2c8b59

Please sign in to comment.