Skip to content

Replace inspect usage with common CPython stdlib pattern #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Sachaa-Thanasius
Copy link

@Sachaa-Thanasius Sachaa-Thanasius commented Apr 6, 2025

Part of #326.

This is the biggest source of import time and hopefully the least controversial to replace; based on local testing, replacing this improves it by ~30% on supported CPython versions and PyPy3.10.

This replaces the expensive inspect import and inspect.stack() usage with an expanded version of a frame inspection pattern used in the CPython standard library.

It's a bit more code, but it's a fairly reasonable amount considering it does the following:

  1. Has various fast paths.
  2. Chooses the fastest working method of inspection at first call to avoid needing to choose again in the future.
  3. Provides robust support for several alternate Python implementations that don't have many, if any, accessible stack frames.
  4. Obviously, avoids an expensive import.

References:

@Sachaa-Thanasius
Copy link
Author

This can be slimmed down (as evidenced by the stdlib's version), but I figured I'd start with the most robust version.

@Sachaa-Thanasius Sachaa-Thanasius changed the title Replace inspect usage with common CPython stdlib pattern. Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant