iceberg/util/mod.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/// Utilities for working with snapshots.
21pub mod snapshot;
22
23// Use a default value of 1 as the safest option.
24// See https://doc.rust-lang.org/std/thread/fn.available_parallelism.html#limitations
25// for more details.
26const DEFAULT_PARALLELISM: usize = 1;
27
28/// Uses [`std::thread::available_parallelism`] in order to
29/// retrieve an estimate of the default amount of parallelism
30/// that should be used. Note that [`std::thread::available_parallelism`]
31/// returns a `Result` as it can fail, so here we use
32/// a default value instead.
33/// Note: we don't use a OnceCell or LazyCell here as there
34/// are circumstances where the level of available
35/// parallelism can change during the lifetime of an executing
36/// process, but this should not be called in a hot loop.
37pub(crate) fn available_parallelism() -> NonZeroUsize {
38 std::thread::available_parallelism().unwrap_or_else(|_err| {
39 // Failed to get the level of parallelism.
40 // TODO: log/trace when this fallback occurs.
41
42 // Using a default value.
43 NonZeroUsize::new(DEFAULT_PARALLELISM).unwrap()
44 })
45}