Skip to content

Commit

Permalink
chore: replace num_cpus crate with available_parallelism in standard …
Browse files Browse the repository at this point in the history
…library (#2342)
  • Loading branch information
tottoto authored Mar 5, 2025
1 parent 59f5d4c commit 25d8895
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 9 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ log = "0.4"
memchr = "2"
memmap2 = "0.9.4"
mime = "0.3"
num_cpus = "1.16"
number_prefix = "0.4"
object = "0.32"
once_cell = "1.19"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/sccache-dist/build_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl PotBuilder {
image_map: Arc::new(Mutex::new(HashMap::new())),
container_lists: Arc::new(Mutex::new(HashMap::new())),
cleanup_thread_count: Arc::new(AtomicUsize::new(0)),
max_cleanup_thread_count: num_cpus::get() * 3,
max_cleanup_thread_count: sccache::util::num_cpus() * 3,
};
ret.cleanup()?;
Ok(ret)
Expand Down
8 changes: 4 additions & 4 deletions src/dist/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub mod urls {

#[cfg(feature = "dist-server")]
mod server {
use crate::util::new_reqwest_blocking_client;
use crate::util::{new_reqwest_blocking_client, num_cpus};
use byteorder::{BigEndian, ReadBytesExt};
use flate2::read::ZlibDecoder as ZlibReadDecoder;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -843,7 +843,7 @@ mod server {
// This limit is rouille's default for `start_server_with_pool`, which
// we would use, except that interface doesn't permit any sort of
// error handling to be done.
let server = server.pool_size(num_cpus::get() * 8);
let server = server.pool_size(num_cpus() * 8);
server.run();

panic!("Rouille server terminated")
Expand Down Expand Up @@ -925,7 +925,7 @@ mod server {
handler,
} = self;
let heartbeat_req = HeartbeatServerHttpRequest {
num_cpus: num_cpus::get(),
num_cpus: num_cpus(),
jwt_key: jwt_key.clone(),
server_nonce,
cert_digest,
Expand Down Expand Up @@ -1022,7 +1022,7 @@ mod server {
// This limit is rouille's default for `start_server_with_pool`, which
// we would use, except that interface doesn't permit any sort of
// error handling to be done.
let server = server.pool_size(num_cpus::get() * 8);
let server = server.pool_size(num_cpus() * 8);
server.run();

panic!("Rouille server terminated")
Expand Down
2 changes: 1 addition & 1 deletion src/jobserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub struct Acquired {

impl Client {
pub fn new() -> Client {
Client::new_num(num_cpus::get())
Client::new_num(crate::util::num_cpus())
}

pub fn new_num(num: usize) -> Client {
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ pub fn start_server(config: &Config, addr: &crate::net::SocketAddr) -> Result<()
let client = Client::new();
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.worker_threads(std::cmp::max(20, 2 * num_cpus::get()))
.worker_threads(std::cmp::max(20, 2 * util::num_cpus()))
.build()?;
let pool = runtime.handle().clone();
let dist_client = DistClientContainer::new(config, &pool);
Expand Down
4 changes: 4 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,10 @@ pub fn ascii_unescape_default(s: &[u8]) -> std::io::Result<Vec<u8>> {
Ok(out)
}

pub fn num_cpus() -> usize {
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get)
}

#[cfg(test)]
mod tests {
use super::{OsStrExt, TimeMacroFinder};
Expand Down

0 comments on commit 25d8895

Please sign in to comment.