Skip to content

Commit 0c45301

Browse files
committed
Improve cache usage
1 parent dfae0b3 commit 0c45301

File tree

1 file changed

+46
-9
lines changed
  • src/mkdocs_git_revision_date_localized_plugin

1 file changed

+46
-9
lines changed

src/mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self):
5757
super().__init__()
5858
self.last_revision_commits = {}
5959
self.created_commits = {}
60+
self.is_serve_dirty_build = False
6061

6162
def on_startup(self, *, command: str, dirty: bool) -> None:
6263
"""
@@ -70,7 +71,15 @@ def on_startup(self, *, command: str, dirty: bool) -> None:
7071
command (str): The mkdocs command being run.
7172
dirty (bool): Whether the build is dirty.
7273
"""
73-
pass
74+
# Track if this is an incremental rebuild during mkdocs serve
75+
# dirty=True means it's a rebuild triggered by file changes
76+
# dirty=False means it's a clean/initial build
77+
self.is_serve_dirty_build = dirty
78+
79+
# Clear cache on clean builds to ensure fresh data
80+
if not dirty:
81+
self.last_revision_commits = {}
82+
self.created_commits = {}
7483

7584
def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
7685
"""
@@ -179,15 +188,20 @@ def parallel_compute_commit_timestamps(self, files, original_source: Optional[Di
179188
elif exclude(f.src_path, self.config.get("exclude", [])):
180189
continue
181190
else:
191+
temp_abs_src_path = str(Path(f.abs_src_path).absolute())
182192
abs_src_path = f.abs_src_path
193+
183194
# Support plugins like monorepo that might have moved the files from the original source that is under git
184195
if original_source and abs_src_path in original_source:
185196
abs_src_path = original_source[abs_src_path]
186197

187198
assert Path(abs_src_path).exists()
188199
abs_src_path = str(Path(abs_src_path).absolute())
189200
result = pool.apply_async(self.util.get_git_commit_timestamp, args=(abs_src_path, is_first_commit))
201+
# Store both the original path and temp path (if different) so cache lookups work either way
190202
results.append((abs_src_path, result))
203+
if temp_abs_src_path != abs_src_path:
204+
results.append((temp_abs_src_path, result))
191205
pool.close()
192206
pool.join()
193207
if is_first_commit:
@@ -204,6 +218,13 @@ def on_files(self, files: Files, config: MkDocsConfig):
204218
if not self.config.get("enabled") or not self.config.get("enable_parallel_processing"):
205219
return
206220

221+
# Skip parallel processing on incremental rebuilds (dirty builds during mkdocs serve)
222+
# This avoids the overhead of creating a new multiprocessing pool on every file save
223+
# The cache from the initial build will be reused
224+
if self.is_serve_dirty_build:
225+
logging.debug("[git-revision-date-localized] Skipping parallel processing on incremental rebuild, using cache")
226+
return
227+
207228
# Support monorepo/techdocs, which copies the docs_dir to a temporary directory
208229
mono_repo_plugin = config.get("plugins", {}).get("monorepo", None)
209230
if mono_repo_plugin is not None and hasattr(mono_repo_plugin, "merger") and mono_repo_plugin.merger is not None:
@@ -275,10 +296,18 @@ def on_page_markdown(self, markdown: str, page: Page, config: config_options.Con
275296
if getattr(page.file, "generated_by", None):
276297
last_revision_hash, last_revision_timestamp = "", int(time.time())
277298
else:
278-
last_revision_hash, last_revision_timestamp = self.last_revision_commits.get(
279-
str(Path(page.file.abs_src_path).absolute()), (None, None)
280-
)
281-
if last_revision_timestamp is None:
299+
# Use cached results if parallel processing is enabled and cache is populated
300+
if self.config.get("enable_parallel_processing") and self.last_revision_commits:
301+
last_revision_hash, last_revision_timestamp = self.last_revision_commits.get(
302+
str(Path(page.file.abs_src_path).absolute()), (None, None)
303+
)
304+
if last_revision_timestamp is None:
305+
last_revision_hash, last_revision_timestamp = self.util.get_git_commit_timestamp(
306+
path=page.file.abs_src_path,
307+
is_first_commit=False,
308+
)
309+
else:
310+
# Directly call git if parallel processing is disabled or cache is empty
282311
last_revision_hash, last_revision_timestamp = self.util.get_git_commit_timestamp(
283312
path=page.file.abs_src_path,
284313
is_first_commit=False,
@@ -351,10 +380,18 @@ def on_page_markdown(self, markdown: str, page: Page, config: config_options.Con
351380
if getattr(page.file, "generated_by", None):
352381
first_revision_hash, first_revision_timestamp = "", int(time.time())
353382
else:
354-
first_revision_hash, first_revision_timestamp = self.created_commits.get(
355-
str(Path(page.file.abs_src_path).absolute()), (None, None)
356-
)
357-
if first_revision_timestamp is None:
383+
# Use cached results if parallel processing is enabled and cache is populated
384+
if self.config.get("enable_creation_date") and self.config.get("enable_parallel_processing") and self.created_commits:
385+
first_revision_hash, first_revision_timestamp = self.created_commits.get(
386+
str(Path(page.file.abs_src_path).absolute()), (None, None)
387+
)
388+
if first_revision_timestamp is None:
389+
first_revision_hash, first_revision_timestamp = self.util.get_git_commit_timestamp(
390+
path=page.file.abs_src_path,
391+
is_first_commit=True,
392+
)
393+
else:
394+
# Directly call git if parallel processing is disabled or cache is empty
358395
first_revision_hash, first_revision_timestamp = self.util.get_git_commit_timestamp(
359396
path=page.file.abs_src_path,
360397
is_first_commit=True,

0 commit comments

Comments
 (0)