Skip to content

Commit 7d611f0

Browse files
jqnatividadclaude
andauthored
perf(moarstats): hint rare branches with core::hint::cold_path() (#3823)
* perf(moarstats): hint rare branches with `core::hint::cold_path()` Mark exceptional branches inside the per-record hot loops of `count_chunk_outliers` and `compute_chunk_bivariate` as cold so the compiler can keep the common path tight: - UTF-8 decode failures on byte cells (CSV cells are virtually always valid UTF-8) - `chunk_stats.get_mut` "this can't happen" safety guards already paired with `debug_assert!(false, ...)` Leaf `compute_*` finalizer helpers, `is_empty()` checks in the parsers, and the outlier-bucket if/else chain were intentionally not hinted — they are not on a tight inner loop or their branch frequency depends on the dataset. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * style(moarstats): group `cold_path` import with `core`/`std` block Address roborev review #1956 (Low): move `use core::hint::cold_path;` to the top of the import block, matching the convention in `src/cmd/stats.rs`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a177270 commit 7d611f0

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/cmd/moarstats.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ Common options:
338338
-o, --output <file> Write output to <file> instead of overwriting the stats CSV file.
339339
"#;
340340

341+
use core::hint::cold_path;
341342
use std::{
342343
env, fs,
343344
path::{Path, PathBuf},
@@ -1970,10 +1971,12 @@ where
19701971

19711972
// Parse the value based on field type
19721973
numeric_value = if field_info.field_type.is_date_or_datetime() {
1973-
// Convert bytes to string for date parsing
1974+
// Convert bytes to string for date parsing. UTF-8 decode failure
1975+
// is exceptional in CSV data — mark as cold for branch prediction.
19741976
if let Ok(value_str) = from_utf8(value_bytes) {
19751977
parse_date_to_days(value_str, prefer_dmy)
19761978
} else {
1979+
cold_path();
19771980
None
19781981
}
19791982
} else {
@@ -1987,6 +1990,7 @@ where
19871990
// Get mutable reference to stats for this field
19881991
// safety: chunk_stats is pre-populated with all field names
19891992
let Some(stats) = chunk_stats.get_mut(field_name) else {
1993+
cold_path();
19901994
debug_assert!(false, "chunk_stats missing expected key: {field_name}");
19911995
continue;
19921996
};
@@ -2303,6 +2307,7 @@ where
23032307

23042308
// safety: chunk_stats is pre-populated with all field pair indices
23052309
let Some(stats) = chunk_stats.get_mut(&(*idx1, *idx2)) else {
2310+
cold_path();
23062311
debug_assert!(false, "chunk_stats missing expected key: ({idx1}, {idx2})");
23072312
continue;
23082313
};
@@ -2313,6 +2318,7 @@ where
23132318
// columns into a per-record HashMap.
23142319
let numeric_value_x = if field1_info.field_type.is_date_or_datetime() {
23152320
let Ok(x_str) = from_utf8(value_bytes_x) else {
2321+
cold_path();
23162322
continue;
23172323
};
23182324
if let Some(cached) = date_cache.get(x_str) {
@@ -2328,6 +2334,7 @@ where
23282334

23292335
let numeric_value_y = if field2_info.field_type.is_date_or_datetime() {
23302336
let Ok(y_str) = from_utf8(value_bytes_y) else {
2337+
cold_path();
23312338
continue;
23322339
};
23332340
if let Some(cached) = date_cache.get(y_str) {
@@ -2357,6 +2364,7 @@ where
23572364
let (Ok(x_str_ref), Ok(y_str_ref)) =
23582365
(from_utf8(value_bytes_x), from_utf8(value_bytes_y))
23592366
else {
2367+
cold_path();
23602368
continue;
23612369
};
23622370
// NOTE: this is not true string interning — each lookup still yields

0 commit comments

Comments
 (0)