From a51102c1ebac1385fbf80e6363130a4baca3bb77 Mon Sep 17 00:00:00 2001 From: Pierre-Marie Charavel Date: Fri, 27 Feb 2026 10:40:19 -0500 Subject: [PATCH] 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 --- ue_mcp_server.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ue_mcp_server.py b/ue_mcp_server.py index 881ac45..92c334e 100644 --- a/ue_mcp_server.py +++ b/ue_mcp_server.py @@ -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() - result = {} - for line in idx.splitlines(): - if ": " in line: - name, path = line.split(": ", 1) - result[name.strip()] = path.strip() - return result + """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_path.read_text().splitlines(): + 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 # ---------------------------------------------------------------------------