From 10f6b6bb093e585eeec25458b35d0818f82494ab Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:37:38 +0100 Subject: [PATCH] Cache the result of ``_file_checksum()`` --- sphinx/builders/html/_assets.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sphinx/builders/html/_assets.py b/sphinx/builders/html/_assets.py index b2b3b9c2b..85e5e2dc2 100644 --- a/sphinx/builders/html/_assets.py +++ b/sphinx/builders/html/_assets.py @@ -3,6 +3,7 @@ from __future__ import annotations import os import warnings import zlib +from functools import cache from typing import TYPE_CHECKING, Any, NoReturn from sphinx.deprecation import RemovedInSphinx90Warning @@ -172,9 +173,14 @@ def _file_checksum(outdir: Path, filename: str | os.PathLike[str]) -> str: if '?' in filename: msg = f'Local asset file paths must not contain query strings: {filename!r}' raise ThemeError(msg) + return _file_checksum_inner(outdir.joinpath(filename).resolve()) + + +@cache +def _file_checksum_inner(file: Path) -> str: try: # Remove all carriage returns to avoid checksum differences - content = outdir.joinpath(filename).read_bytes().translate(None, b'\r') + content = file.read_bytes().translate(None, b'\r') except FileNotFoundError: return '' if not content: