iceberg/utils.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::num::NonZeroUsize;
19
20// Use a default value of 1 as the safest option.
21// See https://doc.rust-lang.org/std/thread/fn.available_parallelism.html#limitations
22// for more details.
23const DEFAULT_PARALLELISM: usize = 1;
24
25/// Uses [`std::thread::available_parallelism`] in order to
26/// retrieve an estimate of the default amount of parallelism
27/// that should be used. Note that [`std::thread::available_parallelism`]
28/// returns a `Result` as it can fail, so here we use
29/// a default value instead.
30/// Note: we don't use a OnceCell or LazyCell here as there
31/// are circumstances where the level of available
32/// parallelism can change during the lifetime of an executing
33/// process, but this should not be called in a hot loop.
34pub(crate) fn available_parallelism() -> NonZeroUsize {
35 std::thread::available_parallelism().unwrap_or_else(|_err| {
36 // Failed to get the level of parallelism.
37 // TODO: log/trace when this fallback occurs.
38
39 // Using a default value.
40 NonZeroUsize::new(DEFAULT_PARALLELISM).unwrap()
41 })
42}