Skip to content

Commit 2482023

Browse files
committed
Use chunks_exact where possible
1 parent 8a62fb5 commit 2482023

File tree

8 files changed

+30
-20
lines changed

8 files changed

+30
-20
lines changed

gix-commitgraph/src/file/access.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl File {
5757
let start = self.base_graphs_list_offset.unwrap_or(0);
5858
let base_graphs_list = &self.data[start..][..self.hash_len * usize::from(self.base_graph_count)];
5959
base_graphs_list
60-
.chunks(self.hash_len)
60+
.chunks_exact(self.hash_len)
6161
.map(gix_hash::oid::from_bytes_unchecked)
6262
}
6363

gix-commitgraph/src/file/init.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,10 @@ impl TryFrom<&Path> for File {
259259

260260
// Copied from gix-odb/pack/index/init.rs
261261
fn read_fan(d: &[u8]) -> ([u32; FAN_LEN], usize) {
262+
assert!(d.len() >= FAN_LEN * 4);
263+
262264
let mut fan = [0; FAN_LEN];
263-
for (c, f) in d.chunks(4).zip(fan.iter_mut()) {
265+
for (c, f) in d.chunks_exact(4).zip(fan.iter_mut()) {
264266
*f = u32::from_be_bytes(c.try_into().unwrap());
265267
}
266268
(fan, FAN_LEN * 4)

gix-index/src/decode/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ impl State {
9696
let entries_res = match index_offsets_table {
9797
Some(entry_offsets) => {
9898
let chunk_size = (entry_offsets.len() as f32 / num_threads as f32).ceil() as usize;
99-
let num_chunks = entry_offsets.chunks(chunk_size).count();
99+
let entry_offsets_chunked = entry_offsets.chunks(chunk_size);
100+
let num_chunks = entry_offsets_chunked.len();
100101
let mut threads = Vec::with_capacity(num_chunks);
101-
for (id, chunks) in entry_offsets.chunks(chunk_size).enumerate() {
102+
for (id, chunks) in entry_offsets_chunked.enumerate() {
102103
let chunks = chunks.to_vec();
103104
threads.push(
104105
gix_features::parallel::build_thread()

gix-pack/src/index/access.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl index::File {
3131
fn iter_v1(&self) -> impl Iterator<Item = Entry> + '_ {
3232
match self.version {
3333
index::Version::V1 => self.data[V1_HEADER_SIZE..]
34-
.chunks(N32_SIZE + self.hash_len)
34+
.chunks_exact(N32_SIZE + self.hash_len)
3535
.take(self.num_objects as usize)
3636
.map(|c| {
3737
let (ofs, oid) = c.split_at(N32_SIZE);
@@ -47,14 +47,19 @@ impl index::File {
4747

4848
fn iter_v2(&self) -> impl Iterator<Item = Entry> + '_ {
4949
let pack64_offset = self.offset_pack_offset64_v2();
50+
let oids = self.data[V2_HEADER_SIZE..]
51+
.chunks_exact(self.hash_len)
52+
.take(self.num_objects as usize);
53+
let crcs = self.data[self.offset_crc32_v2()..]
54+
.chunks_exact(N32_SIZE)
55+
.take(self.num_objects as usize);
56+
let offsets = self.data[self.offset_pack_offset_v2()..]
57+
.chunks_exact(N32_SIZE)
58+
.take(self.num_objects as usize);
59+
assert_eq!(oids.len(), crcs.len());
60+
assert_eq!(crcs.len(), offsets.len());
5061
match self.version {
51-
index::Version::V2 => izip!(
52-
self.data[V2_HEADER_SIZE..].chunks(self.hash_len),
53-
self.data[self.offset_crc32_v2()..].chunks(N32_SIZE),
54-
self.data[self.offset_pack_offset_v2()..].chunks(N32_SIZE)
55-
)
56-
.take(self.num_objects as usize)
57-
.map(move |(oid, crc32, ofs32)| Entry {
62+
index::Version::V2 => izip!(oids, crcs, offsets).map(move |(oid, crc32, ofs32)| Entry {
5863
oid: gix_hash::ObjectId::from_bytes_or_panic(oid),
5964
pack_offset: self.pack_offset_from_offset_v2(ofs32, pack64_offset),
6065
crc32: Some(crate::read_u32(crc32)),
@@ -162,10 +167,10 @@ impl index::File {
162167
index::Version::V1 => self.iter().map(|e| e.pack_offset).collect(),
163168
index::Version::V2 => {
164169
let offset32_start = &self.data[self.offset_pack_offset_v2()..];
170+
let offsets32 = offset32_start.chunks_exact(N32_SIZE).take(self.num_objects as usize);
171+
assert_eq!(self.num_objects as usize, offsets32.len());
165172
let pack_offset_64_start = self.offset_pack_offset64_v2();
166-
offset32_start
167-
.chunks(N32_SIZE)
168-
.take(self.num_objects as usize)
173+
offsets32
169174
.map(|offset| self.pack_offset_from_offset_v2(offset, pack_offset_64_start))
170175
.collect()
171176
}

gix-pack/src/index/init.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ impl index::File {
8383
}
8484

8585
fn read_fan(d: &[u8]) -> ([u32; FAN_LEN], usize) {
86+
assert!(d.len() >= FAN_LEN * N32_SIZE);
87+
8688
let mut fan = [0; FAN_LEN];
87-
for (c, f) in d.chunks(N32_SIZE).zip(fan.iter_mut()) {
89+
for (c, f) in d.chunks_exact(N32_SIZE).zip(fan.iter_mut()) {
8890
*f = crate::read_u32(c);
8991
}
9092
(fan, FAN_LEN * N32_SIZE)

gix-pack/src/index/traverse/with_lookup.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl index::File {
121121
let (chunk_size, thread_limit, available_cores) =
122122
parallel::optimize_chunk_size_and_thread_limit(1000, Some(index_entries.len()), thread_limit, None);
123123
let there_are_enough_entries_to_process = || index_entries.len() > chunk_size * available_cores;
124-
let input_chunks = index_entries.chunks(chunk_size.max(chunk_size));
124+
let input_chunks = index_entries.chunks(chunk_size);
125125
let reduce_progress = OwnShared::new(Mutable::new({
126126
let mut p = progress.add_child_with_id("Traversing".into(), ProgressId::DecodedObjects.into());
127127
p.init(Some(self.num_objects() as usize), progress::count("objects"));

gix-pack/src/multi_index/chunk.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub mod fanout {
121121
return None;
122122
}
123123
let mut out = [0; 256];
124-
for (c, f) in chunk.chunks(4).zip(out.iter_mut()) {
124+
for (c, f) in chunk.chunks_exact(4).zip(out.iter_mut()) {
125125
*f = u32::from_be_bytes(c.try_into().unwrap());
126126
}
127127
out.into()

src/plumbing/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ pub fn main() -> Result<()> {
226226
add_paths: add_path,
227227
prefix,
228228
files: add_virtual_file
229-
.chunks(2)
230-
.map(|c| (c[0].to_owned(), c[1].clone()))
229+
.chunks_exact(2)
230+
.map(|c| (c[0].clone(), c[1].clone()))
231231
.collect(),
232232
format: format.map(|f| match f {
233233
crate::plumbing::options::archive::Format::Internal => {

0 commit comments

Comments
 (0)