`History<T: Time, ..>` has always been generic over the time axis,
`Untimed` has always been exported, and `Drift<T>` is generic specifically
so that "seasonal or calendar-aware drift is expressible without going
through i64". None of it was reachable from a downstream crate.
Every construction route pinned `T = i64`: `History::builder()`,
`History::builder_with_key()`, and the only `Default` impl on
`HistoryBuilder`. Its fields are private and it had no `new`. So all three
escape routes failed to compile, and a consumer with domain timestamps
had to convert to i64 — which is the exact thing the parameter exists to
avoid. One of `History`'s four type parameters was paid for at every
signature and could never be varied.
`Default` is now generic over `T` and `K`, `HistoryBuilder::new()` exists,
and `time_type::<T2>()` / `key_type::<K2>()` join `drift` and `observer`
as type-changing setters:
History::builder().time_type::<Untimed>().build()
History::builder().key_type::<String>().build()
HistoryBuilder::<Season, _, _, String>::new().build()
`key_type` replaces `builder_with_key`, which could not be turbofished —
`K` sat on the impl rather than the function, so callers had to spell
`History::<i64, _, _, String>::builder_with_key()`. 18 call sites across
15 files migrated.
tests/time_axis.rs is the part that matters. NOTHING in the repository
constructed a non-i64 history, which is precisely why this survived, so
the fix is only half done without a test that exercises the generic. It
defines a `Season(u16)` time type and a `SeasonalDrift` that accumulates
between seasons but not within one — the calendar-aware case the trait's
docs cite — and checks the whole path: fit, converge, and read a learning
curve whose times come back as `Season`, not as integers.
Two of the six tests are controls rather than assertions about output.
`Untimed` must ignore drift entirely, since elapsed is always zero, so
gamma 0.0 and gamma 5.0 must agree bit for bit. And a custom `Drift` must
actually widen a gap across seasons, or the test above would pass whether
or not the drift was consulted at all.
The README's ticked "Generalise a time axis" box is now true.
BREAKING CHANGE: `History::builder_with_key()` is removed. Use
`History::builder().key_type::<K>()`.
Closes #68
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hcFjNDmHXZF8URGLku5zZ
119 lines
4.4 KiB
Rust
119 lines
4.4 KiB
Rust
//! End-to-end History::converge benchmark.
|
||
//!
|
||
//! Workload shapes designed to expose rayon's within-slice color-group
|
||
//! parallelism. Events in the same color group are processed in parallel
|
||
//! via direct-write with disjoint index sets (no data races). Color groups
|
||
//! smaller than a threshold fall back to the sequential path to avoid
|
||
//! rayon overhead on small workloads.
|
||
//!
|
||
//! On Apple M5 Pro, the P-core count (6) is the optimal thread count.
|
||
//! The rayon thread pool is initialised to `min(P-cores, available)` to
|
||
//! avoid scheduling onto the slower E-cores.
|
||
//!
|
||
//! ## Results (Apple M5 Pro, 2026-04-24, after SmallVec revert)
|
||
//!
|
||
//! | Workload | Sequential | Parallel | Speedup |
|
||
//! |---------------------------------------------|------------:|-----------:|--------:|
|
||
//! | History::converge/500x100@10perslice | 4.03 ms | 4.24 ms | 1.0× |
|
||
//! | History::converge/2000x200@20perslice | 20.18 ms | 19.82 ms | 1.0× |
|
||
//! | History::converge/1v1-5000x50000@5000perslice| 11.88 ms | 9.10 ms | 1.3× |
|
||
//!
|
||
//! T3 acceptance gate: ≥2× speedup on at least one workload — NOT achieved after revert.
|
||
//! The SmallVec storage that enabled the 2× gate caused a +28% regression in the
|
||
//! sequential Batch::iteration benchmark and was reverted. Small workloads still fall
|
||
//! below the RAYON_THRESHOLD (64 events/color) and run sequentially with near-zero overhead.
|
||
|
||
use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
|
||
use smallvec::smallvec;
|
||
use trueskill_tt::{
|
||
ConstantDrift, ConvergenceOptions, Event, History, Member, NullObserver, Outcome, Team,
|
||
};
|
||
|
||
fn build_history_1v1(
|
||
n_events: usize,
|
||
n_competitors: usize,
|
||
events_per_slice: usize,
|
||
seed: u64,
|
||
) -> History<i64, ConstantDrift, NullObserver, String> {
|
||
let mut rng = seed;
|
||
let mut next = || {
|
||
rng = rng
|
||
.wrapping_mul(6364136223846793005)
|
||
.wrapping_add(1442695040888963407);
|
||
rng
|
||
};
|
||
|
||
let mut h = History::builder()
|
||
.key_type::<String>()
|
||
.mu(25.0)
|
||
.sigma(25.0 / 3.0)
|
||
.beta(25.0 / 6.0)
|
||
.drift(ConstantDrift::new(25.0 / 300.0))
|
||
.convergence(ConvergenceOptions {
|
||
max_iter: 30,
|
||
epsilon: 1e-6,
|
||
alpha: 1.0,
|
||
})
|
||
.build();
|
||
|
||
let mut events: Vec<Event<i64, String>> = Vec::with_capacity(n_events);
|
||
for ev_i in 0..n_events {
|
||
let a = (next() as usize) % n_competitors;
|
||
let mut b = (next() as usize) % n_competitors;
|
||
while b == a {
|
||
b = (next() as usize) % n_competitors;
|
||
}
|
||
events.push(Event {
|
||
time: (ev_i as i64 / events_per_slice as i64) + 1,
|
||
teams: smallvec![
|
||
Team::with_members([Member::new(format!("p{a}"))]),
|
||
Team::with_members([Member::new(format!("p{b}"))]),
|
||
],
|
||
outcome: Outcome::winner((next() % 2) as u32, 2),
|
||
});
|
||
}
|
||
h.add_events(events).unwrap();
|
||
h
|
||
}
|
||
|
||
fn bench_converge(c: &mut Criterion) {
|
||
// Two original task workloads (small per-slice event count;
|
||
// fall below RAYON_THRESHOLD so sequential path runs — near-zero overhead).
|
||
c.bench_function("History::converge/500x100@10perslice", |b| {
|
||
b.iter_batched(
|
||
|| build_history_1v1(500, 100, 10, 42),
|
||
|mut h| {
|
||
let _ = h.converge().unwrap();
|
||
},
|
||
BatchSize::SmallInput,
|
||
);
|
||
});
|
||
|
||
c.bench_function("History::converge/2000x200@20perslice", |b| {
|
||
b.iter_batched(
|
||
|| build_history_1v1(2000, 200, 20, 42),
|
||
|mut h| {
|
||
let _ = h.converge().unwrap();
|
||
},
|
||
BatchSize::SmallInput,
|
||
);
|
||
});
|
||
|
||
// Large single-slice workload: 5000 events, 50000 competitors.
|
||
// All events in one slice → color-0 gets ~4900 disjoint events, well above
|
||
// the 64-event RAYON_THRESHOLD. 30 iterations × 1 slice = 30 sweeps, each
|
||
// parallelised across P-core threads. Shows ≥2× speedup.
|
||
c.bench_function("History::converge/1v1-5000x50000@5000perslice", |b| {
|
||
b.iter_batched(
|
||
|| build_history_1v1(5000, 50000, 5000, 42),
|
||
|mut h| {
|
||
let _ = h.converge().unwrap();
|
||
},
|
||
BatchSize::SmallInput,
|
||
);
|
||
});
|
||
}
|
||
|
||
criterion_group!(benches, bench_converge);
|
||
criterion_main!(benches);
|