Back to Scalene

Scalene Profiler Refactoring Plan

refactoring_todo.md

2.2.12.0 KB
Original Source

Scalene Profiler Refactoring Plan

Goal

Refactor scalene/scalene_profiler.py into multiple files with clear separation of concerns.

Status: ✅ COMPLETE

All verification checks pass:

  • pytest tests/ - 147 tests passed
  • mypy scalene - No issues found
  • ruff check scalene - All checks passed

File Sizes

FileLines
scalene_profiler.py1,584 (was 1,885)
scalene_cpu_profiler.py228 (new)
scalene_tracing.py225 (new)
scalene_lifecycle.py198 (new)

Net reduction: ~300 lines from main profiler, with reusable logic extracted

New Modules Created

1. scalene_cpu_profiler.py

  • Class: ScaleneCPUProfiler
  • Purpose: CPU profiling sample processing
  • Key methods:
    • process_cpu_sample - Main CPU sample handler
    • _update_main_thread_stats - Main thread statistics
    • _update_thread_stats - Other thread statistics

2. scalene_tracing.py

  • Class: ScaleneTracing
  • Purpose: Tracing decisions and file filtering with lru_cache
  • Key methods:
    • should_trace - Main entry point (cached)
    • _passes_exclusion_rules - Library exclusions
    • _should_trace_by_location - Path-based filtering
    • _is_system_library - System library detection

3. scalene_lifecycle.py

  • Class: ScaleneLifecycle
  • Purpose: Profiler lifecycle management (prepared for future use)

Architecture

scalene_profiler.py (Scalene class)
    ├── ScaleneCPUProfiler (CPU sample processing)
    ├── ScaleneTracing (file/function filtering)
    ├── ScaleneMemoryProfiler (already existed)
    └── ScaleneSignalManager (already existed)

Completed Tasks

  • Extracted CPU profiling logic (~150 lines)
  • Extracted tracing/filtering logic (~150 lines)
  • Created lifecycle module for future use
  • Updated type signatures to use Filename consistently
  • Applied proper lru_cache usage
  • All tests passing (147/147)
  • Type checking passing (mypy)
  • Linting passing (ruff)