Bucket in SQLite, draw the build-time bootstrap once, start sccache's server

Speed: the chart's counts come from GROUP BY over the bucket number, so
about 120 rows per table per range reach Rust instead of every saved row;
a range total is one indexed SUM. History counts the rows it hands out.

Build times: crates/stats gains Resampled, which draws the bootstrap once
for a set of languages and applies it to any number of hit sets; the
server keeps it and redraws only when an observation was added. The
views fill build_times, time saved and the 90% interval from the stats
crate, and the time-saved arithmetic of the server is gone.

Starting sccache: when nothing listens where the client looks
(SCCACHE_SERVER_UDS, or 127.0.0.1 at SCCACHE_SERVER_PORT), sccache
--start-server is run once and the counters read again; a failure is
reported as NotRunning and not retried for a minute.

Uses the api's Range::ALL, parse and label.
This commit is contained in:
swaits committed 2026-11-28 11:57:10 -07:00
1 parent 90a6302149
commit 16ba94517b
7 files changed
+1523 -473

No files matched your search

+166 -74
View File
@@ -140,87 +140,135 @@ pub struct Saved {
/// may not hold (cached compiles may be larger or smaller than the typical
/// miss); nor does it cover errors in `hits` or `hit_read_seconds`, or any
/// drift in build times over time.
///
/// Several sets of hits over the same observations (one per range, say) should
/// go through [`Resampled`] instead: it draws the bootstrap once.
pub fn time_saved(langs: &[LanguageHits], hit_read_seconds: f64) -> Saved {
let hit_read = if hit_read_seconds.is_finite() && hit_read_seconds > 0.0 {
hit_read_seconds
} else {
0.0
};
let mut left_out = 0u64;
// (hits, (compiles, seconds) pairs) for counted languages.
let mut counted: Vec<(f64, Vec<(f64, f64)>)> = Vec::new();
for l in langs {
let data: Vec<(f64, f64)> = l
.obs
.iter()
.filter(|o| usable(o))
.map(|o| (o.compiles as f64, o.seconds))
.collect();
if data.len() < MIN_OBSERVATIONS {
left_out = left_out.saturating_add(l.hits);
let obs: Vec<&[Observation]> = langs.iter().map(|l| l.obs).collect();
Resampled::new(&obs).time_saved(langs, hit_read_seconds)
}
/// The bootstrap drawn once for a set of languages, so that the time saved for
/// several sets of hits can be worked out from the same draws: the build times
/// seen are the same for every set, only the hits differ.
#[derive(Debug)]
pub struct Resampled {
/// Per replicate, per language: the resampled (compiles, seconds). A language
/// with too little data has zeros there and is left out.
replicates: Vec<Vec<(f64, f64)>>,
/// Whether each language has enough observations to be counted.
counted: Vec<bool>,
}
impl Resampled {
/// Resamples every language that has [`MIN_OBSERVATIONS`] usable observations.
pub fn new(obs_per_language: &[&[Observation]]) -> Resampled {
let data: Vec<Vec<(f64, f64)>> = obs_per_language.iter().map(|obs| points(obs)).collect();
let counted: Vec<bool> = data.iter().map(|d| d.len() >= MIN_OBSERVATIONS).collect();
let mut rng = Rng(SEED);
let mut replicates = Vec::with_capacity(REPLICATES);
for _ in 0..REPLICATES {
replicates.push(data.iter().map(|d| resample(d, &mut rng)).collect());
}
Resampled {
replicates,
counted,
}
}
/// [`time_saved`] for one set of hits, from the draws already made. `langs`
/// must give each language in the order its observations were given to
/// [`new`](Resampled::new); the slices need not be the same ones.
pub fn time_saved(&self, langs: &[LanguageHits<'_>], hit_read_seconds: f64) -> Saved {
let hit_read = if hit_read_seconds.is_finite() && hit_read_seconds > 0.0 {
hit_read_seconds
} else {
counted.push((l.hits as f64, data));
}
}
let saving = |hits: f64, c: f64, s: f64| hits * (s / c - hit_read).max(0.0);
let seconds: f64 = counted
.iter()
.map(|(h, d)| {
let (c, s) = sums(d.iter());
saving(*h, c, s)
})
.sum();
if counted.is_empty() {
return Saved {
seconds,
interval_90: None,
hits_left_out: left_out,
0.0
};
}
let mut rng = Rng(SEED);
let mut totals: Vec<f64> = Vec::with_capacity(REPLICATES);
for _ in 0..REPLICATES {
let mut total = 0.0;
for (h, d) in &counted {
if *h == 0.0 {
continue;
let saving = |hits: f64, c: f64, s: f64| hits * (s / c - hit_read).max(0.0);
let counted = |i: usize| langs.get(i).is_some_and(|_| self.counted[i]);
let mut left_out = 0u64;
let seconds: f64 = langs
.iter()
.enumerate()
.filter(|(i, _)| counted(*i))
.map(|(_, l)| {
let d = points(l.obs);
let (c, s) = sums(d.iter());
saving(l.hits as f64, c, s)
})
.sum();
for (i, l) in langs.iter().enumerate() {
if !counted(i) {
left_out = left_out.saturating_add(l.hits);
}
let n = d.len() as u64;
// Two 32-bit draws per 64-bit output; (r * n) >> 32 is in 0..n.
// Two independent accumulator pairs keep the adds from chaining.
let pick = |r: u64| d[((r * n) >> 32) as usize];
let (mut c, mut s) = (0.0, 0.0);
let (mut c2, mut s2) = (0.0, 0.0);
for _ in 0..d.len() / 2 {
let x = rng.next();
let (dc, ds) = pick(x >> 32);
let (ec, es) = pick(x & 0xFFFF_FFFF);
c += dc;
s += ds;
c2 += ec;
s2 += es;
}
if d.len() % 2 == 1 {
let (dc, ds) = pick(rng.next() >> 32);
c += dc;
s += ds;
}
let (c, s) = (c + c2, s + s2);
total += saving(*h, c, s);
}
totals.push(total);
if langs.is_empty() || !self.counted.iter().take(langs.len()).any(|c| *c) {
return Saved {
seconds,
interval_90: None,
hits_left_out: left_out,
};
}
let mut totals: Vec<f64> = self
.replicates
.iter()
.map(|replicate| {
langs
.iter()
.zip(replicate)
.enumerate()
.filter(|(i, _)| counted(*i))
.map(|(_, (l, (c, s)))| saving(l.hits as f64, *c, *s))
.sum()
})
.collect();
totals.sort_unstable_by(|a, b| a.total_cmp(b));
let lo = totals[REPLICATES / 20].min(seconds);
let hi = totals[REPLICATES - REPLICATES / 20 - 1].max(seconds);
Saved {
seconds,
interval_90: Some((lo, hi)),
hits_left_out: left_out,
}
}
totals.sort_unstable_by(|a, b| a.total_cmp(b));
let lo = totals[REPLICATES / 20].min(seconds);
let hi = totals[REPLICATES - REPLICATES / 20 - 1].max(seconds);
Saved {
seconds,
interval_90: Some((lo, hi)),
hits_left_out: left_out,
}
/// The usable observations of a language as (compiles, seconds).
fn points(obs: &[Observation]) -> Vec<(f64, f64)> {
obs.iter()
.filter(|o| usable(o))
.map(|o| (o.compiles as f64, o.seconds))
.collect()
}
/// One bootstrap replicate of a language: its observations drawn with replacement, summed.
/// A language with too little data draws nothing.
fn resample(data: &[(f64, f64)], rng: &mut Rng) -> (f64, f64) {
if data.len() < MIN_OBSERVATIONS {
return (0.0, 0.0);
}
let n = data.len() as u64;
// Two 32-bit draws per 64-bit output; (r * n) >> 32 is in 0..n.
// Two independent accumulator pairs keep the adds from chaining.
let pick = |r: u64| data[((r * n) >> 32) as usize];
let (mut c, mut s) = (0.0, 0.0);
let (mut c2, mut s2) = (0.0, 0.0);
for _ in 0..data.len() / 2 {
let x = rng.next();
let (dc, ds) = pick(x >> 32);
let (ec, es) = pick(x & 0xFFFF_FFFF);
c += dc;
s += ds;
c2 += ec;
s2 += es;
}
if data.len() % 2 == 1 {
let (dc, ds) = pick(rng.next() >> 32);
c += dc;
s += ds;
}
(c + c2, s + s2)
}
fn sums<'a>(it: impl Iterator<Item = &'a (f64, f64)>) -> (f64, f64) {
@@ -466,3 +514,47 @@ mod tests {
assert_eq!(s.interval_90, Some((0.0, 0.0)));
}
}
#[cfg(test)]
mod resampled {
use super::*;
fn o(compiles: u64, seconds: f64) -> Observation {
Observation { compiles, seconds }
}
#[test]
fn several_hit_sets_from_one_draw_each_match_the_one_shot_call() {
let rust: Vec<Observation> = (0..40).map(|i| o(1, 4.0 + (i % 7) as f64)).collect();
let c: Vec<Observation> = (0..30).map(|i| o(1, 0.2 + 0.01 * (i % 5) as f64)).collect();
let tiny: Vec<Observation> = (0..3).map(|_| o(1, 9.0)).collect();
let draws = Resampled::new(&[&rust, &c, &tiny]);
for (rh, ch, th) in [(120, 40, 5), (0, 40, 5), (120, 0, 0), (1, 1, 1)] {
let langs = [
LanguageHits {
hits: rh,
obs: &rust,
},
LanguageHits { hits: ch, obs: &c },
LanguageHits {
hits: th,
obs: &tiny,
},
];
let once = time_saved(&langs, 0.01);
let again = draws.time_saved(&langs, 0.01);
assert_eq!(once.seconds, again.seconds, "{rh}/{ch}/{th}");
assert_eq!(once.hits_left_out, again.hits_left_out, "{rh}/{ch}/{th}");
assert_eq!(
again.hits_left_out, th,
"a language with three observations is left out"
);
let (lo, hi) = again.interval_90.expect("two languages have enough data");
assert!(
lo <= again.seconds && again.seconds <= hi,
"{rh}/{ch}/{th}: {lo} <= {} <= {hi}",
again.seconds
);
}
}
}