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