Skip to content

Commit f98c5b1

Browse files
committed
pass threadlimit down from CLIs
1 parent 7c5d8b8 commit f98c5b1

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

Diff for: gitoxide-core/src/lib.rs

+38-19
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,64 @@ impl FromStr for OutputFormat {
3939
}
4040
}
4141

42+
/// A general purpose context for many operations provided here
4243
pub struct Context<W1: io::Write, W2: io::Write> {
4344
/// If set, provide statistics to `out` in the given format
4445
pub output_statistics: Option<OutputFormat>,
4546
/// A stream to which to output operation results
4647
pub out: W1,
4748
/// A stream to which to errors
4849
pub err: W2,
50+
/// If set, don't use more than this amount of threads.
51+
/// Otherwise, usually use as many threads as there are logical cores.
52+
/// A value of 0 is interpreted as no-limit
53+
pub thread_limit: Option<usize>,
54+
}
55+
56+
impl Default for Context<Vec<u8>, Vec<u8>> {
57+
fn default() -> Self {
58+
Context {
59+
output_statistics: None,
60+
thread_limit: None,
61+
out: Vec::new(),
62+
err: Vec::new(),
63+
}
64+
}
4965
}
5066

5167
pub fn init() -> Result<()> {
5268
git_repository::init::repository().with_context(|| "Repository initialization failed")
5369
}
5470

71+
enum EitherCache {
72+
Left(pack::cache::DecodeEntryNoop),
73+
Right(pack::cache::DecodeEntryLRU),
74+
}
75+
76+
impl pack::cache::DecodeEntry for EitherCache {
77+
fn put(&mut self, offset: u64, data: &[u8], kind: Kind, compressed_size: usize) {
78+
match self {
79+
EitherCache::Left(v) => v.put(offset, data, kind, compressed_size),
80+
EitherCache::Right(v) => v.put(offset, data, kind, compressed_size),
81+
}
82+
}
83+
84+
fn get(&mut self, offset: u64, out: &mut Vec<u8>) -> Option<(Kind, usize)> {
85+
match self {
86+
EitherCache::Left(v) => v.get(offset, out),
87+
EitherCache::Right(v) => v.get(offset, out),
88+
}
89+
}
90+
}
91+
5592
pub fn verify_pack_or_pack_index<P, W1, W2>(
5693
path: impl AsRef<Path>,
5794
progress: Option<P>,
5895
Context {
5996
mut out,
6097
mut err,
6198
output_statistics,
99+
thread_limit,
62100
}: Context<W1, W2>,
63101
) -> Result<(git_object::Id, Option<index::PackFileChecksumResult>)>
64102
where
@@ -94,25 +132,6 @@ where
94132
Err(e)
95133
})
96134
.ok();
97-
enum EitherCache {
98-
Left(pack::cache::DecodeEntryNoop),
99-
Right(pack::cache::DecodeEntryLRU),
100-
};
101-
impl pack::cache::DecodeEntry for EitherCache {
102-
fn put(&mut self, offset: u64, data: &[u8], kind: Kind, compressed_size: usize) {
103-
match self {
104-
EitherCache::Left(v) => v.put(offset, data, kind, compressed_size),
105-
EitherCache::Right(v) => v.put(offset, data, kind, compressed_size),
106-
}
107-
}
108-
109-
fn get(&mut self, offset: u64, out: &mut Vec<u8>) -> Option<(Kind, usize)> {
110-
match self {
111-
EitherCache::Left(v) => v.get(offset, out),
112-
EitherCache::Right(v) => v.get(offset, out),
113-
}
114-
}
115-
}
116135
idx.verify_checksum_of_index(pack.as_ref(), progress, || -> EitherCache {
117136
if output_statistics.is_some() {
118137
// turn off acceleration as we need to see entire chains all the time

Diff for: src/plumbing/lean.rs

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ mod options {
1010
/// print the program version.
1111
pub version: bool,
1212

13+
#[argh(option, short = 't')]
14+
/// the amount of threads to use for some operations.
15+
///
16+
/// If unset, or the value is 0, there is no limit and all logical cores can be used.
17+
pub threads: Option<usize>,
18+
1319
#[argh(subcommand)]
1420
pub subcommand: SubCommands,
1521
}
@@ -69,6 +75,7 @@ fn prepare(verbose: bool, name: &str) -> (Option<prodash::line::JoinHandle>, Opt
6975
pub fn main() -> Result<()> {
7076
pub use options::*;
7177
let cli: Args = crate::shared::from_env();
78+
let thread_limit = cli.threads;
7279
match cli.subcommand {
7380
SubCommands::VerifyPack(VerifyPack {
7481
path,
@@ -85,6 +92,7 @@ pub fn main() -> Result<()> {
8592
} else {
8693
None
8794
},
95+
thread_limit,
8896
out: stdout(),
8997
err: stderr(),
9098
},

Diff for: src/plumbing/pretty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ mod options {
1414
#[structopt(name = "gio-plumbing", about = "The git underworld")]
1515
#[structopt(settings = &[AppSettings::SubcommandRequired, AppSettings::ColoredHelp])]
1616
pub struct Args {
17+
#[structopt(long, short = "t")]
18+
/// The amount of threads to use for some operations.
19+
///
20+
/// If unset, or the value is 0, there is no limit and all logical cores can be used.
21+
pub threads: Option<usize>,
22+
1723
#[structopt(subcommand)]
1824
pub cmd: Subcommands,
1925
}
@@ -147,6 +153,7 @@ fn prepare_and_run<T: Send + 'static>(
147153

148154
pub fn main() -> Result<()> {
149155
let args = Args::from_args();
156+
let thread_limit = args.threads;
150157
match args.cmd {
151158
Subcommands::VerifyPack {
152159
path,
@@ -165,6 +172,7 @@ pub fn main() -> Result<()> {
165172
path,
166173
progress,
167174
core::Context {
175+
thread_limit,
168176
output_statistics: if statistics { Some(format) } else { None },
169177
out,
170178
err,

0 commit comments

Comments
 (0)