Skip to content

Set codegen-units = 1 - #193

Open
not522 wants to merge 1 commit into
optuna:mainfrom
not522:compile-option
Open

Set codegen-units = 1#193
not522 wants to merge 1 commit into
optuna:mainfrom
not522:compile-option

Conversation

@not522

@not522 not522 commented Aug 12, 2026

Copy link
Copy Markdown
Member

Setting codegen-units = 1 forces the compiler to process the entire crate as a single compilation unit rather than splitting it up. This unified view allows LLVM to perform aggressive global optimizations, such as cross-function inlining, resulting in faster runtime execution.
I also tried lto = "thin" and lto = "fat", but neither proved effective.

Benchmark

  • master
n_params  n_trials    total [ms]  per trial [us]
      40       500         410.3         820.5
      40      1000        1457.6        1457.6
      40      2000        5691.5        2845.7
  • PR
n_params  n_trials    total [ms]  per trial [us]
      40       500         403.0         806.0
      40      1000        1369.4        1369.4
      40      2000        5298.9        2649.5
Details
use std::hint::black_box;
use std::time::{Duration, Instant};

use rustuna_core::storage::InMemoryStorage;
use rustuna_core::study::{create_study, Direction};
use rustuna_core::Result;
use rustuna_sampler::tpe::TpeSampler;

const N_TRIALS: [usize; 3] = [500, 1000, 2000];
const N_PARAMS: [usize; 1] = [40];

fn run_study(n_trials: usize, n_params: usize) -> Result<Duration> {
    let storage = InMemoryStorage::new();
    let study = create_study(
        "tpe-benchmark",
        storage,
        TpeSampler::seed_from_u64(0),
        vec![Direction::Minimize],
    )?;

    let start = Instant::now();
    study.optimize(
        |mut trial| {
            let mut value = 0.0;
            for i in 0..n_params {
                let x = trial.suggest_float(&format!("x{i}"), -10.0, 10.0)?;
                value += x * x;
            }
            Ok(vec![black_box(value)])
        },
        n_trials,
    )?;
    Ok(start.elapsed())
}

fn main() -> Result<()> {
    println!(
        "{:>8}  {:>8}  {:>12}  {:>12}",
        "n_params", "n_trials", "total [ms]", "per trial [us]"
    );
    for n_params in N_PARAMS {
        for n_trials in N_TRIALS {
            let duration = run_study(n_trials, n_params)?;
            println!(
                "{:>8}  {:>8}  {:>12.1}  {:>12.1}",
                n_params,
                n_trials,
                duration.as_secs_f64() * 1e3,
                duration.as_secs_f64() * 1e6 / n_trials as f64,
            );
        }
    }
    Ok(())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant