Skip to content

perf: cache lazy imports in module globals after first resolution#34

Merged
sacredvoid merged 1 commit intomainfrom
perf/cache-lazy-imports
Mar 26, 2026
Merged

perf: cache lazy imports in module globals after first resolution#34
sacredvoid merged 1 commit intomainfrom
perf/cache-lazy-imports

Conversation

@sacredvoid
Copy link
Owner

Summary

  • After first __getattr__ resolution, caches the result in globals() via globals()[name] = attr
  • Subsequent accesses find the attribute in __dict__ directly, bypassing __getattr__
  • ~3x faster for repeated module attribute access (111x overhead -> 40x, where 40x is the baseline Python LOAD_ATTR cost)

Fixes #33

Test plan

  • All 179 tests pass, lint/format clean

__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
@sacredvoid sacredvoid merged commit b722f13 into main Mar 26, 2026
@sacredvoid sacredvoid deleted the perf/cache-lazy-imports branch March 26, 2026 01:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Perf: __getattr__ lazy imports re-resolve on every access (111x overhead)

1 participant