1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Report building and formatting for BigOAnalyzer
// This file is include!()'d into big_o_analyzer.rs scope.
// NO use imports or #! inner attributes allowed.
impl BigOAnalyzer {
/// Build analysis report
fn build_report(
&self,
functions: Vec<FunctionComplexity>,
pattern_counts: rustc_hash::FxHashMap<String, usize>,
) -> BigOAnalysisReport {
let mut distribution = ComplexityDistribution {
constant: 0,
logarithmic: 0,
linear: 0,
linearithmic: 0,
quadratic: 0,
cubic: 0,
exponential: 0,
unknown: 0,
};
let total_functions = functions.len();
// Count complexity distribution
for func in &functions {
Self::increment_distribution(&mut distribution, &func.time_complexity.class);
}
// Find high complexity functions
let mut high_complexity: Vec<_> = functions
.into_iter()
.filter(|f| {
matches!(
f.time_complexity.class,
BigOClass::Quadratic
| BigOClass::Cubic
| BigOClass::Exponential
| BigOClass::Factorial
)
})
.collect();
// DETERMINISM: sorting on the class alone leaves every same-class
// function in input order, which is only accidentally stable. The
// (file, line, name) suffix makes the order a function of the code.
high_complexity.sort_by(|a, b| {
(a.time_complexity.class as u8)
.cmp(&(b.time_complexity.class as u8))
.then_with(|| a.file_path.cmp(&b.file_path))
.then_with(|| a.line_number.cmp(&b.line_number))
.then_with(|| a.function_name.cmp(&b.function_name))
});
// Generate pattern matches
//
// DETERMINISM: `pattern_counts` is an `FxHashMap`, so collecting it
// straight into a `Vec` put the same patterns in a different array
// order on every run. Sorted by descending occurrences then by name.
let mut pattern_matches: Vec<_> = pattern_counts
.into_iter()
.map(|(name, count)| PatternMatch {
pattern_name: name,
occurrences: count,
typical_complexity: BigOClass::Linear, // Default
})
.collect();
pattern_matches.sort_by(|a, b| {
b.occurrences
.cmp(&a.occurrences)
.then_with(|| a.pattern_name.cmp(&b.pattern_name))
});
// Generate recommendations
let recommendations =
Self::generate_recommendations(&distribution, total_functions);
BigOAnalysisReport {
analyzed_functions: total_functions,
complexity_distribution: distribution,
high_complexity_functions: high_complexity,
pattern_matches,
recommendations,
}
}
/// How many high-complexity functions are LISTED vs how many were FOUND.
///
/// The distribution is computed over every analysed function and is never
/// touched by the `--top-files` / `--high-complexity-only` filters, so
/// `quadratic + cubic + exponential` (Factorial is folded into
/// `exponential` by `increment_distribution`) is exactly the number of
/// functions that qualified before truncation. `.max(listed)` keeps a part
/// from ever exceeding its whole if a caller hands us a report whose
/// distribution and list disagree.
fn high_complexity_listed_and_found(report: &BigOAnalysisReport) -> (usize, usize) {
let listed = report.high_complexity_functions.len();
let dist = &report.complexity_distribution;
let found = (dist.quadratic + dist.cubic + dist.exponential).max(listed);
(listed, found)
}
/// Increment the appropriate distribution counter for a complexity class
fn increment_distribution(distribution: &mut ComplexityDistribution, class: &BigOClass) {
match class {
BigOClass::Constant => distribution.constant += 1,
BigOClass::Logarithmic => distribution.logarithmic += 1,
BigOClass::Linear => distribution.linear += 1,
BigOClass::Linearithmic => distribution.linearithmic += 1,
BigOClass::Quadratic => distribution.quadratic += 1,
BigOClass::Cubic => distribution.cubic += 1,
BigOClass::Exponential => distribution.exponential += 1,
BigOClass::Factorial => distribution.exponential += 1,
BigOClass::Unknown => distribution.unknown += 1,
}
}
/// Generate recommendations based on complexity distribution
fn generate_recommendations(
distribution: &ComplexityDistribution,
total_functions: usize,
) -> Vec<String> {
let mut recommendations = Vec::new();
if distribution.quadratic > 0 {
recommendations.push(format!(
"Found {} functions with O(n^2) complexity. Consider optimization.",
distribution.quadratic
));
}
if distribution.exponential > 0 {
recommendations.push(format!(
"Found {} functions with exponential complexity! These need immediate attention.",
distribution.exponential
));
}
if distribution.unknown > total_functions / 4 {
recommendations.push(
"Many functions have unknown complexity. Consider adding more explicit patterns."
.to_string(),
);
}
recommendations
}
/// Format report as JSON
///
/// # Examples
///
/// ```
/// use pmat::services::big_o_analyzer::{BigOAnalyzer, BigOAnalysisReport, ComplexityDistribution};
///
/// let analyzer = BigOAnalyzer::new();
/// let report = BigOAnalysisReport {
/// analyzed_functions: 10,
/// complexity_distribution: ComplexityDistribution {
/// constant: 3,
/// logarithmic: 1,
/// linear: 4,
/// linearithmic: 1,
/// quadratic: 1,
/// cubic: 0,
/// exponential: 0,
/// unknown: 0,
/// },
/// high_complexity_functions: vec![],
/// pattern_matches: vec![],
/// recommendations: vec![],
/// };
///
/// let json = analyzer.format_as_json(&report).unwrap();
/// assert!(json.contains("\"analyzed_functions\": 10"));
/// ```
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn format_as_json(&self, report: &BigOAnalysisReport) -> Result<String> {
let (listed, found) = Self::high_complexity_listed_and_found(report);
let json = serde_json::json!({
"summary": {
"analyzed_functions": report.analyzed_functions,
// A TOTAL THAT IS SECRETLY A CAP is a fabrication. This field
// is the length of `high_complexity_functions`, and the default
// `--top-files 10` truncates that list: on pmat's own tree the
// distribution said O(n^2)+O(n^3)+O(2^n) = 106 while
// `high_complexity_count` said 24, with nothing saying so. Both
// numbers are now named, and the flag is explicit.
"high_complexity_count": listed,
"high_complexity_found": found,
"high_complexity_truncated": listed < found,
},
"distribution": {
"O(1)": report.complexity_distribution.constant,
"O(log n)": report.complexity_distribution.logarithmic,
"O(n)": report.complexity_distribution.linear,
"O(n log n)": report.complexity_distribution.linearithmic,
"O(n^2)": report.complexity_distribution.quadratic,
"O(n^3)": report.complexity_distribution.cubic,
"O(2^n)": report.complexity_distribution.exponential,
"O(?)": report.complexity_distribution.unknown,
},
"high_complexity_functions": report.high_complexity_functions.iter().map(|f| {
serde_json::json!({
"file": f.file_path.display().to_string(),
"function": f.function_name,
"line": f.line_number,
"time_complexity": f.time_complexity.notation(),
"space_complexity": f.space_complexity.notation(),
"confidence": f.confidence,
})
}).collect::<Vec<_>>(),
"pattern_matches": report.pattern_matches.iter().map(|p| {
serde_json::json!({
"pattern": p.pattern_name,
"occurrences": p.occurrences,
})
}).collect::<Vec<_>>(),
"recommendations": report.recommendations,
});
Ok(serde_json::to_string_pretty(&json)?)
}
/// Format report as Markdown
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn format_as_markdown(&self, report: &BigOAnalysisReport) -> String {
let mut md = String::with_capacity(1024);
md.push_str("# Big-O Complexity Analysis Report\n\n");
md.push_str("## Summary\n\n");
md.push_str(&format!(
"- **Total Functions Analyzed**: {}\n",
report.analyzed_functions
));
// Same truncation disclosure as `format_as_json`: the listed count is
// capped by `--top-files`, the found count is not.
let (listed, found) = Self::high_complexity_listed_and_found(report);
if listed < found {
md.push_str(&format!(
"- **High Complexity Functions**: {listed} listed of {found} found (truncated by --top-files)\n\n"
));
} else {
md.push_str(&format!("- **High Complexity Functions**: {listed}\n\n"));
}
Self::format_distribution_table(&mut md, report);
if !report.high_complexity_functions.is_empty() {
Self::format_high_complexity_table(&mut md, report);
}
if !report.recommendations.is_empty() {
md.push_str("## Recommendations\n\n");
for rec in &report.recommendations {
md.push_str(&format!("- {rec}\n"));
}
}
md
}
/// Format the complexity distribution table in markdown
fn format_distribution_table(md: &mut String, report: &BigOAnalysisReport) {
md.push_str("## Complexity Distribution\n\n");
md.push_str("| Complexity | Count | Percentage |\n");
md.push_str("|------------|-------|------------|\n");
let total = report.analyzed_functions as f64;
let dist = &report.complexity_distribution;
let rows: &[(&str, usize)] = &[
("O(1)", dist.constant),
("O(log n)", dist.logarithmic),
("O(n)", dist.linear),
("O(n log n)", dist.linearithmic),
("O(n^2)", dist.quadratic),
("O(n^3)", dist.cubic),
("O(2^n)", dist.exponential),
("Unknown", dist.unknown),
];
for (label, count) in rows {
let suffix = if *label == "Unknown" { "\n" } else { "" };
md.push_str(&format!(
"| {} | {} | {:.1}% |{}\n",
label,
count,
(*count as f64 / total) * 100.0,
suffix
));
}
}
/// Format the high-complexity functions table in markdown
fn format_high_complexity_table(md: &mut String, report: &BigOAnalysisReport) {
md.push_str("## High Complexity Functions\n\n");
md.push_str(
"| File | Function | Line | Time Complexity | Space Complexity | Confidence |\n",
);
md.push_str(
"|------|----------|------|-----------------|------------------|------------|\n",
);
for func in &report.high_complexity_functions {
md.push_str(&format!(
"| {} | {} | {} | {} | {} | {}% |\n",
func.file_path
.file_name()
.unwrap_or_default()
.to_string_lossy(),
func.function_name,
func.line_number,
func.time_complexity.notation(),
func.space_complexity.notation(),
func.confidence
));
}
md.push('\n');
}
}