Skip to content

Allow a negative number of jobs to count from maximum number of cores #9217

@orlp

Description

@orlp

If you want your machine to remain usable during a compile session usually you want to set the number of jobs
lower than your maximum number of cores. But this varies heavily from machine to machine, and makes it
annoying to automate in bigger build scripts.

I would suggest extending the --jobs (or -j) parameter to negative numbers, where -j -1 would be 'all but one core',
-j -2 would be 'all but two cores', etc. This makes it really easy for scripts to maintain a usable machine and scale
to the number of cores available without having to look up the actual number of cores.

For sanity and convenience of automatic build scripts I would suggest that if -j was sufficiently negative to specify no or a negative amount of cores, to clamp the number of cores to at least 1, instead of giving an error.

So to be precise I would suggest changing jobs to i32 instead of u32 in BuildConfig::new (and wherever necessary to make that work) and replace this line with

let jobs = match jobs.or(cfg.jobs) {
    None => ::num_cpus::get() as u32,
    Some(j) if j < 0 => (::num_cpus::get() as i32 + j).max(1) as u32,
    Some(j) => j as u32,
};

I'm neutral on whether 0 should still not be allowed or whether that should be mapped to ::num_cpus::get().

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-documenting-cargo-itselfArea: Cargo's documentationA-jobserverArea: jobserver, concurrency, parallelismC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`PerformanceGotta go fast!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions