Skip to content

PERF Go faster by caching library detection and loading (WIP) - #249

Draft
itamarst wants to merge 1 commit into
joblib:masterfrom
itamarst:246-go-faster
Draft

itamarst wants to merge 1 commit into
joblib:masterfrom
itamarst:246-go-faster

Conversation

@itamarst

@itamarst itamarst commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #246

Todo:

  • Probably want some tests for caching code.
  • Document semantic changes in the changelog: can no longer have multiple different LibController per file path, and libraries now never get unloaded. Both seem unlikely use cases.

Additional optimizations:

  • Use caching for remaining getattr(CDLL, attr), e.g. OpenBLASController._find_affixes per the profiler.
  • Only run _warn_if_incompatible_openmp() once per newly loaded CDLL, if possible, instead of every time ThreadPoolController is loaded.

Comment thread threadpoolctl.py
def __getattr__(self, symbol: str) -> ctypes._CFuncPtr | None:
result = self._cache.get(symbol, _MISSING)
if result is _MISSING:
result = getattr(self._cdll, symbol, None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, do you have quantitative evidence that caching attribute looks of ctypes.CDLL instances is useful? I would not have expected them to be expensive.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It showed up in the profile, pretty sure, which is what motivated this. When I get to the final PR I will give some numbers without this enabled, and remove it turns out to be unnecessary.

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.

Speed up usage of threadpoolctl

2 participants