Skip to content

perf: remove module-level import inspect from console.py#2

Closed
KRRT7 wants to merge 1 commit intomasterfrom
perf/remove-inspect
Closed

perf: remove module-level import inspect from console.py#2
KRRT7 wants to merge 1 commit intomasterfrom
perf/remove-inspect

Conversation

@KRRT7
Copy link
Copy Markdown
Owner

@KRRT7 KRRT7 commented Apr 9, 2026

Summary

Eliminate import inspect and from inspect import isclass from module level in console.py:

  • Replace isclass(x) with isinstance(x, type) (identical behavior — isclass is literally isinstance(obj, type))
  • Replace inspect.currentframe default arg with sys._getframe (same underlying call, sys already imported)
  • Keep from inspect import stack as a lazy import in the fallback path only (for platforms without sys._getframe)

inspect pulls in dis, tokenize, ast, and annotationlib (~10ms). This is the single largest import-time cost in the Console path.

Benchmarks (Standard_D2s_v5, Python 3.13)

Part of the combined optimization set: master (77.1ms) → codeflash/optimize (51.5ms) for Console import.

Testing

952 passed on CPython 3.9–3.14 and PyPy 3.10.

Replace `import inspect` + `from inspect import isclass` with:
- `isinstance(obj, type)` for isclass (identical behavior)
- `sys._getframe` for inspect.currentframe (same underlying call)
- Lazy `from inspect import stack` in the fallback path only

inspect pulls in dis, tokenize, ast, and annotationlib (~10ms).
This eliminates that cost from Console's import path entirely.
@KRRT7
Copy link
Copy Markdown
Owner Author

KRRT7 commented Apr 9, 2026

Superseded by the consolidated stacked PRs #12 + #13.

@KRRT7 KRRT7 closed this Apr 9, 2026
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.

1 participant