Skip to content

gh-138122: Move local imports to module level in sampling profiler - #143257

Merged
pablogsal merged 6 commits into
python:mainfrom
pablogsal:imports
Jan 2, 2026
Merged

gh-138122: Move local imports to module level in sampling profiler#143257
pablogsal merged 6 commits into
python:mainfrom
pablogsal:imports

Conversation

@pablogsal

@pablogsalpablogsal commented Dec 28, 2025

Copy link
Copy Markdown
Member

The sampling profiler code had numerous imports placed inside functions
rather than at module level. While deferred imports can reduce startup
time for rarely-used code paths, these imports were in functions called
during normal profiler operation, adding repeated import overhead.

Moving imports to module level follows Python best practices for code
that runs frequently. This makes import dependencies explicit at file
scope and eliminates per-call import lookup costs. The test files also
had redundant local imports that duplicated module-level imports.

The sampling profiler code had numerous imports placed inside functions
rather than at module level. While deferred imports can reduce startup
time for rarely-used code paths, these imports were in functions called
during normal profiler operation, adding repeated import overhead.
Moving imports to module level follows Python best practices for code
that runs frequently. This makes import dependencies explicit at file
scope and eliminates per-call import lookup costs. The test files also
had redundant local imports that duplicated module-level imports.
@picnixzpicnixz changed the title Move local imports to module level in sampling profilergh-138122: Move local imports to module level in sampling profilerDec 28, 2025
@picnixzpicnixz added the type-refactor Code refactoring (with no changes in behavior) label Dec 28, 2025
Keep curses import as a local import in sample_live() since curses
is not available on Windows. The original code had this as a local
import to avoid breaking Windows tests that import the module.
@pablogsal
pablogsal enabled auto-merge (squash) January 2, 2026 02:05
@pablogsal
pablogsal merged commit 6b9a6c6 into python:mainJan 2, 2026
44 checks passed
@pablogsal
pablogsal deleted the imports branch January 2, 2026 02:31
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip newstype-refactorCode refactoring (with no changes in behavior)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@pablogsal@picnixz