Skip to content

Commit d851bed

Browse files
committed
change!: remove unnecessary Arc around should_interrupt flag (#279)
1 parent c2679a0 commit d851bed

File tree

11 files changed

+64
-79
lines changed

11 files changed

+64
-79
lines changed

Diff for: git-pack/src/bundle/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod find;
66
pub mod write;
77

88
mod verify {
9-
use std::sync::{atomic::AtomicBool, Arc};
9+
use std::sync::atomic::AtomicBool;
1010

1111
use git_features::progress::Progress;
1212

@@ -22,7 +22,7 @@ mod verify {
2222
make_pack_lookup_cache: impl Fn() -> C + Send + Clone,
2323
thread_limit: Option<usize>,
2424
progress: Option<P>,
25-
should_interrupt: Arc<AtomicBool>,
25+
should_interrupt: &AtomicBool,
2626
) -> Result<
2727
(git_hash::ObjectId, Option<crate::index::traverse::Outcome>, Option<P>),
2828
crate::index::traverse::Error<crate::index::verify::integrity::Error>,

Diff for: git-pack/src/index/traverse/indexed.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::{
22
collections::VecDeque,
3-
sync::{
4-
atomic::{AtomicBool, Ordering},
5-
Arc,
6-
},
3+
sync::atomic::{AtomicBool, Ordering},
74
};
85

96
use git_features::{parallel, progress::Progress};
@@ -27,7 +24,7 @@ impl index::File {
2724
new_processor: impl Fn() -> Processor + Send + Clone,
2825
mut progress: P,
2926
pack: &crate::data::File,
30-
should_interrupt: Arc<AtomicBool>,
27+
should_interrupt: &AtomicBool,
3128
) -> Result<(git_hash::ObjectId, index::traverse::Outcome, P), Error<E>>
3229
where
3330
P: Progress,
@@ -43,15 +40,8 @@ impl index::File {
4340
{
4441
let pack_progress = progress.add_child("SHA1 of pack");
4542
let index_progress = progress.add_child("SHA1 of index");
46-
let should_interrupt = Arc::clone(&should_interrupt);
4743
move || {
48-
let res = self.possibly_verify(
49-
pack,
50-
check,
51-
pack_progress,
52-
index_progress,
53-
Arc::clone(&should_interrupt),
54-
);
44+
let res = self.possibly_verify(pack, check, pack_progress, index_progress, &should_interrupt);
5545
if res.is_err() {
5646
should_interrupt.store(true, Ordering::SeqCst);
5747
}

Diff for: git-pack/src/index/traverse/mod.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{atomic::AtomicBool, Arc};
1+
use std::sync::atomic::AtomicBool;
22

33
use git_features::{
44
parallel,
@@ -21,13 +21,13 @@ mod types;
2121
pub use types::{Algorithm, Outcome, SafetyCheck};
2222

2323
mod options {
24-
use std::sync::{atomic::AtomicBool, Arc};
24+
use std::sync::atomic::AtomicBool;
2525

2626
use crate::index::traverse::{Algorithm, SafetyCheck};
2727

2828
/// Traversal options for [`traverse()`][crate::index::File::traverse()]
2929
#[derive(Debug, Clone)]
30-
pub struct Options {
30+
pub struct Options<'a> {
3131
/// The algorithm to employ.
3232
pub algorithm: Algorithm,
3333
/// If `Some`, only use the given amount of threads. Otherwise, the amount of threads to use will be selected based on
@@ -37,18 +37,7 @@ mod options {
3737
pub check: SafetyCheck,
3838
/// A flag to indicate whether the algorithm should be interrupted. Will be checked occasionally allow stopping a running
3939
/// computation.
40-
pub should_interrupt: Arc<AtomicBool>,
41-
}
42-
43-
impl Default for Options {
44-
fn default() -> Self {
45-
Self {
46-
algorithm: Algorithm::Lookup,
47-
thread_limit: Default::default(),
48-
check: Default::default(),
49-
should_interrupt: Default::default(),
50-
}
51-
}
40+
pub should_interrupt: &'a AtomicBool,
5241
}
5342
}
5443
pub use options::Options;
@@ -86,7 +75,7 @@ impl index::File {
8675
thread_limit,
8776
check,
8877
should_interrupt,
89-
}: Options,
78+
}: Options<'_>,
9079
) -> Result<(git_hash::ObjectId, Outcome, Option<P>), Error<E>>
9180
where
9281
P: Progress,
@@ -125,7 +114,7 @@ impl index::File {
125114
check: SafetyCheck,
126115
pack_progress: impl Progress,
127116
index_progress: impl Progress,
128-
should_interrupt: Arc<AtomicBool>,
117+
should_interrupt: &AtomicBool,
129118
) -> Result<git_hash::ObjectId, Error<E>>
130119
where
131120
E: std::error::Error + Send + Sync + 'static,
@@ -138,11 +127,8 @@ impl index::File {
138127
});
139128
}
140129
let (pack_res, id) = parallel::join(
141-
{
142-
let should_interrupt = Arc::clone(&should_interrupt);
143-
move || pack.verify_checksum(pack_progress, &should_interrupt)
144-
},
145-
move || self.verify_checksum(index_progress, &should_interrupt),
130+
move || pack.verify_checksum(pack_progress, should_interrupt),
131+
move || self.verify_checksum(index_progress, should_interrupt),
146132
);
147133
pack_res?;
148134
id?

Diff for: git-pack/src/index/traverse/with_lookup.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::sync::Arc;
2-
31
use git_features::{
42
parallel::{self, in_parallel_if},
53
progress::{self, unit, Progress},
@@ -9,21 +7,21 @@ use super::{Error, Reducer};
97
use crate::{data, index, index::util};
108

119
mod options {
12-
use std::sync::{atomic::AtomicBool, Arc};
10+
use std::sync::atomic::AtomicBool;
1311

1412
use crate::index::traverse::SafetyCheck;
1513

1614
/// Traversal options for [`traverse()`][crate::index::File::traverse_with_lookup()]
17-
#[derive(Default, Debug, Clone)]
18-
pub struct Options {
15+
#[derive(Debug, Clone)]
16+
pub struct Options<'a> {
1917
/// If `Some`, only use the given amount of threads. Otherwise, the amount of threads to use will be selected based on
2018
/// the amount of available logical cores.
2119
pub thread_limit: Option<usize>,
2220
/// The kinds of safety checks to perform.
2321
pub check: SafetyCheck,
2422
/// A flag to indicate whether the algorithm should be interrupted. Will be checked occasionally allow stopping a running
2523
/// computation.
26-
pub should_interrupt: Arc<AtomicBool>,
24+
pub should_interrupt: &'a AtomicBool,
2725
}
2826
}
2927
use std::sync::atomic::Ordering;
@@ -47,7 +45,7 @@ impl index::File {
4745
thread_limit,
4846
check,
4947
should_interrupt,
50-
}: Options,
48+
}: Options<'_>,
5149
) -> Result<(git_hash::ObjectId, index::traverse::Outcome, P), Error<E>>
5250
where
5351
P: Progress,
@@ -64,15 +62,8 @@ impl index::File {
6462
{
6563
let pack_progress = progress.add_child("SHA1 of pack");
6664
let index_progress = progress.add_child("SHA1 of index");
67-
let should_interrupt = Arc::clone(&should_interrupt);
6865
move || {
69-
let res = self.possibly_verify(
70-
pack,
71-
check,
72-
pack_progress,
73-
index_progress,
74-
Arc::clone(&should_interrupt),
75-
);
66+
let res = self.possibly_verify(pack, check, pack_progress, index_progress, should_interrupt);
7667
if res.is_err() {
7768
should_interrupt.store(true, Ordering::SeqCst);
7869
}

Diff for: git-pack/src/index/verify.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{atomic::AtomicBool, Arc};
1+
use std::sync::atomic::AtomicBool;
22

33
use git_features::progress::{self, Progress};
44
use git_object::{bstr::ByteSlice, WriteTo};
@@ -120,7 +120,7 @@ impl index::File {
120120
pack: Option<PackContext<'_, C, F>>,
121121
thread_limit: Option<usize>,
122122
progress: Option<P>,
123-
should_interrupt: Arc<AtomicBool>,
123+
should_interrupt: &AtomicBool,
124124
) -> Result<
125125
(git_hash::ObjectId, Option<index::traverse::Outcome>, Option<P>),
126126
index::traverse::Error<crate::index::verify::integrity::Error>,
@@ -157,7 +157,7 @@ impl index::File {
157157
)
158158
.map(|(id, outcome, root)| (id, Some(outcome), root)),
159159
None => self
160-
.verify_checksum(root.add_child("Sha1 of index"), &should_interrupt)
160+
.verify_checksum(root.add_child("Sha1 of index"), should_interrupt)
161161
.map_err(Into::into)
162162
.map(|id| (id, None, root.into_inner())),
163163
}

Diff for: git-pack/src/multi_index/verify.rs

+23
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,27 @@ impl File {
2525
should_interrupt,
2626
)
2727
}
28+
29+
/// Similar to [`crate::Bundle::verify_integrity()`] but checks all contained indices and their packs.
30+
///
31+
/// Note that it's considered a failure if an index doesn't have a corresponding pack.
32+
#[allow(unused)]
33+
pub fn verify_integrity<C, P>(
34+
&self,
35+
verify_mode: crate::index::verify::Mode,
36+
traversal: crate::index::traverse::Algorithm,
37+
make_pack_lookup_cache: impl Fn() -> C + Send + Clone,
38+
thread_limit: Option<usize>,
39+
progress: Option<P>,
40+
should_interrupt: &AtomicBool,
41+
) -> Result<
42+
(git_hash::ObjectId, Option<crate::index::traverse::Outcome>, Option<P>),
43+
crate::index::traverse::Error<crate::index::verify::integrity::Error>,
44+
>
45+
where
46+
P: Progress,
47+
C: crate::cache::DecodeEntry,
48+
{
49+
todo!()
50+
}
2851
}

Diff for: git-pack/tests/pack/data/output/count_and_entries.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
convert::Infallible,
3-
sync::{atomic::AtomicBool, Arc},
4-
};
1+
use std::{convert::Infallible, sync::atomic::AtomicBool};
52

63
use git_features::{parallel::reduce::Finalize, progress};
74
use git_odb::{compound, pack, pack::FindExt};
@@ -396,7 +393,7 @@ fn write_and_verify(
396393
|| pack::cache::Never,
397394
None,
398395
progress::Discard.into(),
399-
Arc::new(should_interrupt),
396+
&should_interrupt,
400397
)?;
401398

402399
Ok(())

Diff for: git-pack/tests/pack/index.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
mod file {
22
const SHA1_SIZE: usize = git_hash::Kind::Sha1.len_in_bytes();
3+
34
use git_object::{self as object};
45
use git_odb::pack;
6+
use std::sync::atomic::AtomicBool;
57

68
use crate::{
79
fixture_path, hex_to_id,
@@ -307,7 +309,7 @@ mod file {
307309
}),
308310
None,
309311
progress::Discard.into(),
310-
Default::default()
312+
&AtomicBool::new(false)
311313
)
312314
.map(|(a, b, _)| (a, b))?,
313315
(idx.index_checksum(), Some(stats.to_owned())),
@@ -407,7 +409,7 @@ mod file {
407409
None::<git_pack::index::verify::PackContext<'_, _, fn() -> cache::Never>>,
408410
None,
409411
progress::Discard.into(),
410-
Default::default()
412+
&AtomicBool::new(false)
411413
)
412414
.map(|(a, b, _)| (a, b))?,
413415
(idx.index_checksum(), None)

Diff for: gitoxide-core/src/pack/explode.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub fn pack_or_pack_index(
200200
let object_path = object_path.map(|p| p.as_ref().to_owned());
201201
move || {
202202
let out = OutputWriter::new(object_path.clone(), sink_compress, object_hash);
203-
let object_verifier = if verify { object_path.as_ref().map(|path| loose::Store::at(path, object_hash)) } else { None };
203+
let loose_odb = verify.then(|| object_path.as_ref().map(|path| loose::Store::at(path, object_hash))).flatten();
204204
let mut read_buf = Vec::new();
205205
move |object_kind, buf, index_entry, progress| {
206206
let written_id = out.write_buf(object_kind, buf).map_err(|err| {
@@ -220,7 +220,7 @@ pub fn pack_or_pack_index(
220220
return Err(Error::ObjectEncodeMismatch(object_kind, index_entry.oid, written_id));
221221
}
222222
}
223-
if let Some(verifier) = object_verifier.as_ref() {
223+
if let Some(verifier) = loose_odb.as_ref() {
224224
let obj = verifier
225225
.try_find(written_id, &mut read_buf)
226226
.map_err(|err| Error::WrittenFileCorrupt(err, written_id))?
@@ -236,7 +236,7 @@ pub fn pack_or_pack_index(
236236
algorithm,
237237
thread_limit,
238238
check: check.into(),
239-
should_interrupt,
239+
should_interrupt: &should_interrupt,
240240
},
241241
)
242242
.map(|(_, _, c)| progress::DoOrDiscard::from(c))

Diff for: gitoxide-core/src/pack/verify.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use std::{
2-
io,
3-
path::Path,
4-
str::FromStr,
5-
sync::{atomic::AtomicBool, Arc},
6-
};
1+
use std::{io, path::Path, str::FromStr, sync::atomic::AtomicBool};
72

83
use anyhow::{anyhow, Context as AnyhowContext, Result};
94
use bytesize::ByteSize;
@@ -54,7 +49,7 @@ impl From<Algorithm> for index::traverse::Algorithm {
5449
}
5550

5651
/// A general purpose context for many operations provided here
57-
pub struct Context<W1: io::Write, W2: io::Write> {
52+
pub struct Context<'a, W1: io::Write, W2: io::Write> {
5853
/// If set, provide statistics to `out` in the given format
5954
pub output_statistics: Option<OutputFormat>,
6055
/// A stream to which to output operation results
@@ -67,19 +62,20 @@ pub struct Context<W1: io::Write, W2: io::Write> {
6762
pub thread_limit: Option<usize>,
6863
pub mode: index::verify::Mode,
6964
pub algorithm: Algorithm,
70-
pub should_interrupt: Arc<AtomicBool>,
65+
pub should_interrupt: &'a AtomicBool,
7166
}
7267

73-
impl Default for Context<Vec<u8>, Vec<u8>> {
74-
fn default() -> Self {
68+
impl<'a> Context<'a, Vec<u8>, Vec<u8>> {
69+
/// Create a new default context with all fields being the default.
70+
pub fn new(should_interrupt: &'a AtomicBool) -> Self {
7571
Context {
7672
output_statistics: None,
7773
thread_limit: None,
7874
mode: index::verify::Mode::HashCrc32,
7975
algorithm: Algorithm::LessMemory,
8076
out: Vec::new(),
8177
err: Vec::new(),
82-
should_interrupt: Default::default(),
78+
should_interrupt,
8379
}
8480
}
8581
}
@@ -116,7 +112,7 @@ pub fn pack_or_pack_index<W1, W2>(
116112
thread_limit,
117113
algorithm,
118114
should_interrupt,
119-
}: Context<W1, W2>,
115+
}: Context<'_, W1, W2>,
120116
) -> Result<(ObjectId, Option<index::traverse::Outcome>)>
121117
where
122118
W1: io::Write,
@@ -178,7 +174,7 @@ where
178174
}),
179175
thread_limit,
180176
progress,
181-
should_interrupt,
177+
&should_interrupt,
182178
)
183179
.map(|(a, b, _)| (a, b))
184180
.with_context(|| "Verification failure")?

Diff for: src/plumbing/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ pub fn main() -> Result<()> {
302302
thread_limit,
303303
mode,
304304
algorithm,
305-
should_interrupt,
305+
should_interrupt: &should_interrupt,
306306
},
307307
)
308308
},

0 commit comments

Comments
 (0)