Skip to content

Commit 54ea266

Browse files
authored
Merge pull request #1700 from GitoxideLabs/reduce-memory
reduce memory consumption during clone
2 parents 0b7abfb + 664e28c commit 54ea266

File tree

6 files changed

+17
-8
lines changed

6 files changed

+17
-8
lines changed

gix-pack/src/cache/delta/tree.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{traverse, Error};
2+
use crate::exact_vec;
23
/// An item stored within the [`Tree`] whose data is stored in a pack file, identified by
34
/// the offset of its first (`offset`) and last (`next_offset`) bytes.
45
///
@@ -61,8 +62,8 @@ impl<T> Tree<T> {
6162
/// Instantiate a empty tree capable of storing `num_objects` amounts of items.
6263
pub fn with_capacity(num_objects: usize) -> Result<Self, Error> {
6364
Ok(Tree {
64-
root_items: Vec::with_capacity(num_objects / 2),
65-
child_items: Vec::with_capacity(num_objects / 2),
65+
root_items: exact_vec(num_objects / 2),
66+
child_items: exact_vec(num_objects / 2),
6667
last_seen: None,
6768
future_child_offsets: Vec::new(),
6869
})

gix-pack/src/data/output/bytes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::io::Write;
33
use gix_features::hash;
44

55
use crate::data::output;
6+
use crate::exact_vec;
67

78
/// The error returned by `next()` in the [`FromEntriesIter`] iterator.
89
#[allow(missing_docs)]
@@ -73,7 +74,7 @@ where
7374
output: hash::Write::new(output, object_hash),
7475
trailer: None,
7576
entry_version: version,
76-
pack_offsets_and_validity: Vec::with_capacity(num_entries as usize),
77+
pack_offsets_and_validity: exact_vec(num_entries as usize),
7778
written: 0,
7879
header_info: Some((version, num_entries)),
7980
is_done: false,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use gix_features::{
99

1010
use super::{Error, Reducer};
1111
use crate::{
12-
data, index,
12+
data, exact_vec, index,
1313
index::{traverse::Outcome, util},
1414
};
1515

@@ -152,7 +152,7 @@ impl index::File {
152152
Some(entries.len()),
153153
gix_features::progress::count_with_decimals("objects", 2),
154154
);
155-
let mut stats = Vec::with_capacity(entries.len());
155+
let mut stats = exact_vec(entries.len());
156156
progress.set(0);
157157
for index_entry in entries.iter() {
158158
let result = self.decode_and_process_entry(

gix-pack/src/index/util.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::time::Instant;
22

3+
use crate::exact_vec;
34
use gix_features::progress::{self, Progress};
45

56
pub(crate) fn index_entries_sorted_by_offset_ascending(
@@ -9,7 +10,7 @@ pub(crate) fn index_entries_sorted_by_offset_ascending(
910
progress.init(Some(idx.num_objects as usize), progress::count("entries"));
1011
let start = Instant::now();
1112

12-
let mut v = Vec::with_capacity(idx.num_objects as usize);
13+
let mut v = exact_vec(idx.num_objects as usize);
1314
for entry in idx.iter() {
1415
v.push(entry);
1516
progress.inc();

gix-pack/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,9 @@ fn read_u32(b: &[u8]) -> u32 {
6969
fn read_u64(b: &[u8]) -> u64 {
7070
u64::from_be_bytes(b.try_into().unwrap())
7171
}
72+
73+
fn exact_vec<T>(capacity: usize) -> Vec<T> {
74+
let mut v = Vec::new();
75+
v.reserve_exact(capacity);
76+
v
77+
}

gix-pack/src/multi_index/verify.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{cmp::Ordering, sync::atomic::AtomicBool, time::Instant};
22

33
use gix_features::progress::{Count, DynNestedProgress, Progress};
44

5-
use crate::{index, multi_index::File};
5+
use crate::{exact_vec, index, multi_index::File};
66

77
///
88
pub mod integrity {
@@ -166,7 +166,7 @@ impl File {
166166

167167
let operation_start = Instant::now();
168168
let mut total_objects_checked = 0;
169-
let mut pack_ids_and_offsets = Vec::with_capacity(self.num_objects as usize);
169+
let mut pack_ids_and_offsets = exact_vec(self.num_objects as usize);
170170
{
171171
let order_start = Instant::now();
172172
let mut progress = progress.add_child_with_id("checking oid order".into(), gix_features::progress::UNKNOWN);

0 commit comments

Comments
 (0)