Skip to content

perf: defer tracker initialization and slim import path (1/4)#1251

Merged
inimaz merged 3 commits into
masterfrom
davidberenstein1957/perf-1-lazy-init
Jun 22, 2026
Merged

perf: defer tracker initialization and slim import path (1/4)#1251
inimaz merged 3 commits into
masterfrom
davidberenstein1957/perf-1-lazy-init

Conversation

@davidberenstein1957

@davidberenstein1957 davidberenstein1957 commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

Part 1/4 of the tracker performance stack (replaces #1246).

cc @inimaz — split per your review feedback into smaller, reviewable PRs.

Defers expensive work until it is actually needed so tracker construction and import codecarbon stay fast:

  • Lazy-load static reference data in DataSource (CSV/JSON on first access, not at import)
  • Defer hardware probing, geo validation, and emissions engine until first use
  • Lazy-import output handlers and geography modules
  • Skip the 1 Hz power monitor when output_methods=[]
  • Avoid redundant measurement on stop() when a sample was just taken (_measure_power_and_energy_if_stale)
  • Lazy-import cpuinfo, arrow, and direct output_methods imports

Benchmarks (measured locally, offline Mac ARM, 2026-06-21)

Fresh Python subprocess per sample; baseline = master.

Metric master After this PR Δ
Tracker __init__ p50 190 ms 1.9 ms ~99% faster (~100×)
Cold lifecycle p50 (init→start→stop) 1,685 ms 1,767 ms ~same — hardware still probed on first start
Warm lifecycle best (same process) 1,565 ms 1,561 ms ~same — cache lands in #1252
CLI monitor subprocess p50 850 ms 786 ms ~8% faster (full CLI win in #1254)

What this PR delivers: near-instant tracker construction. The big cold start() win comes in #1252.

Stack

  1. This PR — lazy initialization & import slimming
  2. perf: cache hardware detection and optimize warm-path reuse (2/4) #1252 — hardware detection cache & warm-path reuse
  3. perf: defer API run creation until first emission upload (3/4) #1253 — lazy API run creation
  4. perf: speed up CLI monitor startup and fix wrapped commands (4/4) #1254 — CLI monitor startup

CI / quality

Test plan

  • Reviewer: offline tracker with output_methods=[] still measures on start/stop

Replaces

Split from #1246.

Lazy-load reference data, output handlers, hardware probing, and the
emissions engine so tracker construction stays fast when work is deferred.

Co-authored-by: Cursor <cursoragent@cursor.com>
@davidberenstein1957

Copy link
Copy Markdown
Collaborator Author

@inimaz#1246 has been split into this 4-PR stack for easier review. Benchmarks in the description are measured locally (Mac ARM, 2026-06-21); pre-commit and tests pass on each branch. Please start with #1251.

Deferred hardware setup in PR 1 requires _ensure_hardware_ready() in tests
that inspect tracker._hardware before start().

Co-authored-by: Cursor <cursoragent@cursor.com>
@davidberenstein1957

Copy link
Copy Markdown
Collaborator Author

Test fix pushed: test_cpu_load_detection now calls _ensure_hardware_ready() (required after deferred hardware init in this PR) and mocks PowerMetrics unavailable for portable CI.

Verified locally: 504 passed, pre-commit clean.

@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.39%. Comparing base (150cac8) to head (62d2bcd).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1251      +/-   ##
==========================================
+ Coverage   89.17%   89.39%   +0.21%     
==========================================
  Files          47       47              
  Lines        4510     4565      +55     
==========================================
+ Hits         4022     4081      +59     
+ Misses        488      484       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@inimaz inimaz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice thanks for the split @davidberenstein1957 . I added a comment

Comment thread codecarbon/input.py
Comment thread codecarbon/input.py
Use TYPE_CHECKING import for pd so lazy loading stays fast while keeping
return type annotations on get_cloud_emissions_data and get_cpu_power_data.

Co-authored-by: Cursor <cursoragent@cursor.com>
@davidberenstein1957

Copy link
Copy Markdown
Collaborator Author

@inimaz — addressed your review comments on input.py: restored pd.DataFrame return types on get_cloud_emissions_data and get_cpu_power_data using the TYPE_CHECKING import pattern (commit 62d2bcd).

Verified locally on all 4 PR branches:

@inimaz inimaz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @davidberenstein1957 !

@inimaz inimaz merged commit 11374f4 into master Jun 22, 2026
13 checks passed
@inimaz inimaz deleted the davidberenstein1957/perf-1-lazy-init branch June 22, 2026 08:20
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.

2 participants