perf: reduce CompletedTrial cloning overhead

- Add Trial::into_completed() and into_multi_objective_trial() to move
  fields instead of cloning 5 HashMaps/Vecs per trial completion
- Fire after_trial callback before pushing to storage, eliminating the
  clone-from-storage pattern at all 4 call sites
- Optimize top_trials(n) to sort indices and clone only N trials instead
  of cloning all completed trials
- Remove unused set_complete/set_pruned methods
This commit is contained in:
Manuel Raimann
2026-02-12 14:39:28 +01:00
parent 74cf0643eb
commit 5da63d8976
5 changed files with 87 additions and 99 deletions
+6 -2
View File
@@ -20,7 +20,9 @@ fn test_callback_early_stopping() {
Ok(x)
}
fn after_trial(&self, study: &Study<f64>, _trial: &CompletedTrial<f64>) -> ControlFlow<()> {
if study.n_trials() >= 5 {
// The current trial has not yet been pushed to storage when
// after_trial fires, so n_trials() == 4 means this is the 5th.
if study.n_trials() >= 4 {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
@@ -98,7 +100,9 @@ fn test_callback_sampler_early_stopping() {
Ok(x)
}
fn after_trial(&self, study: &Study<f64>, _trial: &CompletedTrial<f64>) -> ControlFlow<()> {
if study.n_trials() >= 3 {
// The current trial has not yet been pushed to storage when
// after_trial fires, so n_trials() == 2 means this is the 3rd.
if study.n_trials() >= 2 {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())