From af6ddab25a67c9ec5ab8f5f9be4dd506ff381b18 Mon Sep 17 00:00:00 2001 From: Samanvya Tripathi Date: Wed, 25 Mar 2026 21:30:47 -0400 Subject: [PATCH] perf: cache lazy imports in module globals after first resolution __getattr__ was calling importlib.import_module() + getattr() on every access. Now sets globals()[name] after first resolution so Python finds the attribute in __dict__ directly on subsequent accesses. ~3x faster for repeated module-level attribute access. Fixes #33 --- src/alignrl/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/alignrl/__init__.py b/src/alignrl/__init__.py index cdcbf27..9519e55 100644 --- a/src/alignrl/__init__.py +++ b/src/alignrl/__init__.py @@ -31,7 +31,9 @@ def __getattr__(name: str): if name in _LAZY_IMPORTS: module = importlib.import_module(_LAZY_IMPORTS[name]) - return getattr(module, name) + attr = getattr(module, name) + globals()[name] = attr + return attr raise AttributeError(f"module 'alignrl' has no attribute {name!r}")