perf: remove module-level import inspect from console.py#2
Closed
perf: remove module-level import inspect from console.py#2
Conversation
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.
Owner
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Eliminate
import inspectandfrom inspect import isclassfrom module level inconsole.py:isclass(x)withisinstance(x, type)(identical behavior —isclassis literallyisinstance(obj, type))inspect.currentframedefault arg withsys._getframe(same underlying call,sysalready imported)from inspect import stackas a lazy import in the fallback path only (for platforms withoutsys._getframe)inspectpulls indis,tokenize,ast, andannotationlib(~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.