Skip to content

Commit 9242eb7

Browse files
Copilotmessense
andauthored
Fix memory allocation issue for large files by capping StaticSparseDAG capacity (#126)
* Initial plan * Fix memory allocation issue for large files by capping StaticSparseDAG capacity Co-authored-by: messense <1556054+messense@users.noreply.github.com> * Remove new tests and restore sparse_dag module privacy Co-authored-by: messense <1556054+messense@users.noreply.github.com> * Update static sparse dag max capacity to ~32MB --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: messense <1556054+messense@users.noreply.github.com> Co-authored-by: messense <messense@icloud.com>
1 parent 2c1670f commit 9242eb7

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/sparse_dag.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ impl Iterator for EdgeIter<'_> {
3333

3434
impl StaticSparseDAG {
3535
pub(crate) fn with_size_hint(hint: usize) -> Self {
36+
// Cap the allocation to prevent memory issues with very large inputs
37+
// The theoretical maximum should be much smaller than hint * 5 for most practical cases
38+
const MAX_CAPACITY: usize = 4_000_000; // 1M elements = ~32MB on 64-bit systems
39+
const MULTIPLIER: usize = 5;
40+
41+
let capacity = std::cmp::min(hint * MULTIPLIER, MAX_CAPACITY);
3642
StaticSparseDAG {
37-
array: Vec::with_capacity(hint * 5),
43+
array: Vec::with_capacity(capacity),
3844
start_pos: HashMap::default(),
3945
size_hint_for_iterator: 0,
4046
curr_insertion_len: 0,

0 commit comments

Comments
 (0)