perf: pre-allocate vectors in analysis methods

- Use Vec::with_capacity() in param_importance() and fanova_with_config()
  where iteration count is known or bounded
- Fix flaky MultivariateTpeSampler doctest by increasing trials from 30
  to 50
This commit is contained in:
Manuel Raimann
2026-02-12 14:46:46 +01:00
parent ddbbe294bc
commit 1b158e4d1b
2 changed files with 6 additions and 6 deletions
+1 -1
View File
@@ -205,7 +205,7 @@ pub enum ConstantLiarStrategy {
/// let y = FloatParam::new(-5.0, 5.0);
///
/// study
/// .optimize(30, |trial: &mut optimizer::Trial| {
/// .optimize(50, |trial: &mut optimizer::Trial| {
/// let xv = x.suggest(trial)?;
/// let yv = y.suggest(trial)?;
/// Ok::<_, optimizer::Error>(xv * xv + yv * yv)
+5 -5
View File
@@ -1517,12 +1517,12 @@ where
// Collect all parameter IDs across trials.
let all_param_ids: BTreeSet<_> = complete.iter().flat_map(|t| t.params.keys()).collect();
let mut scores: Vec<(String, f64)> = Vec::new();
let mut scores: Vec<(String, f64)> = Vec::with_capacity(all_param_ids.len());
for &param_id in &all_param_ids {
// Collect (param_value_f64, objective_f64) for trials that have this param.
let mut param_vals = Vec::new();
let mut obj_vals = Vec::new();
let mut param_vals = Vec::with_capacity(complete.len());
let mut obj_vals = Vec::with_capacity(complete.len());
for trial in &complete {
if let Some(pv) = trial.params.get(param_id) {
@@ -1645,8 +1645,8 @@ where
}
// Build feature matrix (only trials that have all parameters).
let mut data = Vec::new();
let mut targets = Vec::new();
let mut data = Vec::with_capacity(complete.len());
let mut targets = Vec::with_capacity(complete.len());
for trial in &complete {
let mut row = Vec::with_capacity(all_param_ids.len());