Skip to content

perf(ya): defer heavy imports at startup — Δ=-38ms (p=0.0026, BCa CI [12ms, 58ms])#40

Open
KorsarOfficial wants to merge 1 commit intoyandex:mainfrom
KorsarOfficial:perf/python-bootstrap
Open

perf(ya): defer heavy imports at startup — Δ=-38ms (p=0.0026, BCa CI [12ms, 58ms])#40
KorsarOfficial wants to merge 1 commit intoyandex:mainfrom
KorsarOfficial:perf/python-bootstrap

Conversation

@KorsarOfficial
Copy link
Copy Markdown

Defer tier-1 Python imports for faster bootstrap

Summary

Move heavy tier-1 imports (devtools.ya.core.report, app_config) behind
importlib.import_module() call sites and add PEP 562 __getattr__ lazy
re-exports to four modules in devtools/ya/core. This eliminates eager
import graph traversal during ya --help and reduces bootstrap overhead.

Evidence

Inner-container wall-clock timing (docker exec, n=20, 3 warmup runs):

Metric Before After Delta
Median 1.025s 0.987s -38ms (-3.7%)
Mean 1.027s 0.990s -37ms
Std 41ms 20ms -51% variance

Statistical validation:

  • Mann-Whitney U = 312, p = 0.0026 (two-sided), significant at α = 0.05
  • Effect size r = 0.78 (large)
  • BCa 95% CI for improvement: [12ms, 58ms] (n_bootstrap = 10000)

Disclosure: outer Docker timing (measuring container startup + Python)
shows p = 0.091 (not significant). Container startup overhead dominates
at that granularity (~2.4s total vs 38ms signal). Inner-container timing
isolates Python execution and is the authoritative measurement.

Supplementary: full statistical analysis available in the rendered
optimization evidence report (reporting/output/07-optimization-evidence.tex).

Changes

devtools/ya/core/monitoring/__init__.py
  - Remove eager `import devtools.ya.core.report as report` at module level
  - Change report_type default to None; resolve inside MetricStore.store()
  - Add PEP 562 __getattr__ for lazy re-export of report module symbols

devtools/ya/core/stage_tracer/__init__.py
  - Defer app_config import behind call site

devtools/ya/core/stages_profiler/__init__.py
  - Defer heavy import behind call site

devtools/ya/core/yarg/handler.py
  - Add PEP 562 __getattr__ for lazy re-export of handler symbols

Net change: 4 files modified, ~40 lines changed.

Patches

  • patches/15-01-task1-defer-tier1-imports.patch (apply first)
  • patches/15-01-task2-pep562-lazy-reexports.patch (apply second)

Task 2 must follow task 1. Both patches together constitute this PR.

Testing

# Build
ya make devtools/ya/bin

# Unit tests
ya test -tt devtools/ya/core

See upstream/test-results.log for test execution status and environmental
constraints. Historical validation: patch applied in yatool Docker container
during Phase 15 implementation; ya make devtools/ya/bin succeeded.

CLA

I hereby agree to the terms of the CLA available at:
https://yandex.ru/legal/cla/?lang=en

@KorsarOfficial
Copy link
Copy Markdown
Author

Evidence Report

Full statistical analysis for this optimization:
https://github.com/KorsarOfficial/yatool/releases/download/v1.0-perf-analysis/07-optimization-evidence.pdf — Section 2: Python Bootstrap (p.4)

Also available:

All reports: https://github.com/KorsarOfficial/yatool/releases/tag/v1.0-perf-analysis

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