History

Make sure we don't pre-allocate beyond some sane amount

This should already have the desired effect.

Sebastian Thiel committed 7 months ago

Commit 16bb9051 parent 888feb71 file · +8 −3Browse files

Unified · Split

gix-object/src/tree/ref_iter.rs+8−3
@@ -206,11 +206,16 @@ mod decode { · 205 unmodified lines
206206
207207 // Calculate an estimate of the amount of entries to reduce
208208 // the amount of allocations necessary.
209+ // Note that this assumes that we want speed over fitting Vecs, this is a trade-off.
209210 // TODO(SHA256): know actual/desired length for reduced overallocation
210211 const HASH_LEN_FIXME: usize = 20;
211− let lower_bound_single_entry = 2 + HASH_LEN_FIXME; // 2 = space + trailing zero
212− let upper_bound_entries = i.len() / lower_bound_single_entry;
213− let mut out = Vec::with_capacity(upper_bound_entries);
212+ const AVERAGE_FILENAME_LEN: usize = 24;
213+ const AVERAGE_MODE_LEN: usize = 6;
214+ const ENTRY_DELIMITER_LEN: usize = 2; // space + trailing zero
215+ const AVERAGE_TREE_ENTRIES: usize = 16 * 2; // prevent overallocation beyond what's meaningful or what could be dangerous
216+ let average_entry_len = ENTRY_DELIMITER_LEN + HASH_LEN_FIXME + AVERAGE_MODE_LEN + AVERAGE_FILENAME_LEN;
217+ let upper_bound = i.len() / average_entry_len;
218+ let mut out = Vec::with_capacity(upper_bound.min(AVERAGE_TREE_ENTRIES));
214219
215220 while !i.is_empty() {
216221 let Some((rest, entry)) = fast_entry(i) else {