Skip to main content

rustify_ml/
analyzer.rs

1use rustpython_parser::ast::{Stmt, Suite};
2use rustpython_parser::Parse;
3use tracing::info;
4
5use crate::utils::{extract_code, InputSource, ProfileSummary, TargetSpec};
6
7/// Select target functions to generate based on hotspot percentages and ML mode heuristics (placeholder heuristics).
8pub fn select_targets(
9    profile: &ProfileSummary,
10    source: &InputSource,
11    threshold: f32,
12    ml_mode: bool,
13) -> Vec<TargetSpec> {
14    let mut targets = Vec::new();
15    for hs in &profile.hotspots {
16        if hs.percent < threshold {
17            continue;
18        }
19        let reason = if ml_mode {
20            format!("{}% hotspot (ml-mode)", hs.percent)
21        } else {
22            format!("{}% hotspot", hs.percent)
23        };
24        targets.push(TargetSpec {
25            func: hs.func.clone(),
26            line: hs.line,
27            percent: hs.percent,
28            reason,
29        });
30    }
31
32    if threshold <= 0.0 {
33        if let Ok(code) = extract_code(source) {
34            if let Ok(suite) = Suite::parse(&code, "<input>") {
35                for stmt in suite.iter() {
36                    if let Stmt::FunctionDef(func_def) = stmt {
37                        let name = func_def.name.to_string();
38                        if targets.iter().any(|t| t.func == name) {
39                            continue;
40                        }
41                        // Source line is optional here; default to 1 if unavailable.
42                        targets.push(TargetSpec {
43                            func: name,
44                            line: 1,
45                            percent: 0.0,
46                            reason: "threshold<=0: include all defs".to_string(),
47                        });
48                    }
49                }
50            }
51        }
52    }
53
54    info!(count = targets.len(), "selected targets for generation");
55    targets
56}