Cache type index with mtime-based invalidation

_load_type_index() now caches the parsed dict at module level and only
re-reads type-index.txt when its mtime changes. This avoids a file read
and parse on every get_class_overview / get_member call. The cache is
automatically invalidated if the docs are regenerated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-27 10:40:19 -05:00
parent 3d075cea20
commit a51102c1eb

View File

@@ -28,15 +28,24 @@ def _engine_root() -> Path:
return Path(p) return Path(p)
_type_index_cache: dict[str, str] = {}
_type_index_mtime: float = 0.0
def _load_type_index() -> dict[str, str]: def _load_type_index() -> dict[str, str]:
"""Returns {TypeName: relative_md_path}.""" """Returns {TypeName: relative_md_path}, reloading only if the file has changed."""
idx = (_docs_root() / "type-index.txt").read_text() global _type_index_cache, _type_index_mtime
result = {} idx_path = _docs_root() / "type-index.txt"
for line in idx.splitlines(): mtime = idx_path.stat().st_mtime
if ": " in line: if mtime != _type_index_mtime:
name, path = line.split(": ", 1) result = {}
result[name.strip()] = path.strip() for line in idx_path.read_text().splitlines():
return result if ": " in line:
name, path = line.split(": ", 1)
result[name.strip()] = path.strip()
_type_index_cache = result
_type_index_mtime = mtime
return _type_index_cache
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------