Replace ScoredCandidate.signal_snapshot Vec<(String,f64)> with
SmallVec<[(SignalKey,f64); 4]> where SignalKey is Static(&'static str)
| Owned(Arc<str>):
- Compile-time-constant labels (sort bases, relevance, co_engagement,
preference_affinity) -> Static: zero allocation, pointer-copy clone.
- Dynamic {signal}_boost/_penalty/_decay labels built once per query in a
RuleLabels hoist (was format! per-candidate-per-rule), shared by Arc.
Cohort rescore hoisted the same way.
- scored accumulator pre-sized to candidates.len().
- Owned Strings rebuilt only at the two response-assembly sites (<= limit).
Byte-identical output: full 1896-test lib suite green. Measured win
(cargo bench --bench ranking, committed-base vs working-tree):
score_200_hot 23.89->21.04us (-11.9%), score_200_trending
27.66->25.85us (-6.5%), score_200_full_pipeline 27.88->26.97us (-3.3%).
smallvec promoted from the lock to a direct dep (no new dependency
surface). perf-sweep doc updated; T2 (per-term DashMap collapse) next.
119 lines
3.6 KiB
Rust
119 lines
3.6 KiB
Rust
#![allow(clippy::unwrap_used)]
|
|
|
|
//! Criterion benchmarks for the diversity enforcement selector.
|
|
//!
|
|
//! Measures greedy selection across five scenarios:
|
|
//! - `max_per_creator` only (common case)
|
|
//! - `format_mix` only
|
|
//! - Both constraints combined
|
|
//! - Worst-case relaxation (all stages triggered)
|
|
//! - No constraints (fast path baseline)
|
|
|
|
use criterion::{Criterion, black_box, criterion_group, criterion_main};
|
|
use tidaldb::{
|
|
ranking::{DiversityConstraints, DiversitySelector, ScoredCandidate},
|
|
schema::EntityId,
|
|
};
|
|
|
|
fn make_200_candidates(n_creators: usize) -> Vec<ScoredCandidate> {
|
|
(0..200_usize)
|
|
.map(|i| ScoredCandidate {
|
|
entity_id: EntityId::new(i as u64 + 1),
|
|
#[allow(clippy::cast_precision_loss)]
|
|
score: (200 - i) as f64 / 200.0,
|
|
signal_snapshot: smallvec::SmallVec::new(),
|
|
creator_id: Some(EntityId::new((i % n_creators) as u64 + 1)),
|
|
format: Some(if i % 3 == 0 {
|
|
"video".into()
|
|
} else {
|
|
"audio".into()
|
|
}),
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn bench_diversity_200_max_per_creator_2(c: &mut Criterion) {
|
|
let candidates = make_200_candidates(50);
|
|
let constraints = DiversityConstraints::new().max_per_creator(2);
|
|
c.bench_function("diversity_200_max_per_creator_2", |b| {
|
|
b.iter(|| {
|
|
DiversitySelector::select(
|
|
black_box(&candidates),
|
|
black_box(&constraints),
|
|
black_box(100),
|
|
)
|
|
});
|
|
});
|
|
}
|
|
|
|
fn bench_diversity_200_format_mix(c: &mut Criterion) {
|
|
let candidates = make_200_candidates(200);
|
|
let constraints = DiversityConstraints::new().format_mix(0.6);
|
|
c.bench_function("diversity_200_format_mix", |b| {
|
|
b.iter(|| {
|
|
DiversitySelector::select(
|
|
black_box(&candidates),
|
|
black_box(&constraints),
|
|
black_box(100),
|
|
)
|
|
});
|
|
});
|
|
}
|
|
|
|
fn bench_diversity_200_combined(c: &mut Criterion) {
|
|
let candidates = make_200_candidates(50);
|
|
let constraints = DiversityConstraints::new()
|
|
.max_per_creator(2)
|
|
.format_mix(0.6);
|
|
c.bench_function("diversity_200_combined", |b| {
|
|
b.iter(|| {
|
|
DiversitySelector::select(
|
|
black_box(&candidates),
|
|
black_box(&constraints),
|
|
black_box(100),
|
|
)
|
|
});
|
|
});
|
|
}
|
|
|
|
fn bench_diversity_200_worst_case_relaxation(c: &mut Criterion) {
|
|
// All same creator -- unsatisfiable constraints trigger all relaxation stages.
|
|
let candidates = make_200_candidates(1);
|
|
let constraints = DiversityConstraints::new()
|
|
.max_per_creator(1)
|
|
.format_mix(0.4);
|
|
c.bench_function("diversity_200_worst_case_relaxation", |b| {
|
|
b.iter(|| {
|
|
DiversitySelector::select(
|
|
black_box(&candidates),
|
|
black_box(&constraints),
|
|
black_box(100),
|
|
)
|
|
});
|
|
});
|
|
}
|
|
|
|
fn bench_diversity_200_no_constraints(c: &mut Criterion) {
|
|
let candidates = make_200_candidates(200);
|
|
let constraints = DiversityConstraints::new();
|
|
c.bench_function("diversity_200_no_constraints", |b| {
|
|
b.iter(|| {
|
|
DiversitySelector::select(
|
|
black_box(&candidates),
|
|
black_box(&constraints),
|
|
black_box(100),
|
|
)
|
|
});
|
|
});
|
|
}
|
|
|
|
criterion_group!(
|
|
benches,
|
|
bench_diversity_200_max_per_creator_2,
|
|
bench_diversity_200_format_mix,
|
|
bench_diversity_200_combined,
|
|
bench_diversity_200_worst_case_relaxation,
|
|
bench_diversity_200_no_constraints,
|
|
);
|
|
criterion_main!(benches);
|