Skip to content

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

@sacredvoid

Description

@sacredvoid

Description

`init.py`'s `getattr` calls `importlib.import_module()` + `getattr()` on every attribute access. While Python caches modules in `sys.modules`, the `getattr` -> dict lookup -> module lookup chain adds 1.8us per access vs 0.02us for a direct reference (111x overhead).

This matters in hot loops that reference `alignrl.SFTConfig` or similar.

Fix

After resolving, set the attribute on the module itself with `setattr()` so Python finds it via normal attribute lookup on subsequent accesses, bypassing `getattr` entirely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions