Skip to content

Commit 134c094

Browse files
committed
Make the sync module release memory aggressively
Shrink HashMaps and Vectors of downloaders and Verification queue to reduce the memory usage when syncing.
1 parent ab9fdcb commit 134c094

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

core/src/verification/queue/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ impl<K: Kind> VerificationQueue<K> {
408408
*td -= score;
409409
}
410410
}
411+
processing.shrink_to_fit();
411412
processing.is_empty()
412413
}
413414

@@ -429,6 +430,7 @@ impl<K: Kind> VerificationQueue<K> {
429430
*td -= score;
430431
}
431432
}
433+
processing.shrink_to_fit();
432434

433435
let mut new_verified = VecDeque::new();
434436
let mut removed_size = 0;
@@ -444,6 +446,7 @@ impl<K: Kind> VerificationQueue<K> {
444446
new_verified.push_back(output);
445447
}
446448
}
449+
processing.shrink_to_fit();
447450

448451
self.verification.sizes.verified.fetch_sub(removed_size, AtomicOrdering::SeqCst);
449452
*verified = new_verified;

sync/src/block/downloader/body.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl BodyDownloader {
6969
self.downloaded.insert(hash, body);
7070
}
7171
}
72+
self.downloading.shrink_to_fit();
7273
}
7374

7475
pub fn add_target(&mut self, header: &Header, parent: &Header) {
@@ -93,13 +94,17 @@ impl BodyDownloader {
9394
self.downloading.remove(hash);
9495
self.downloaded.remove(hash);
9596
}
97+
self.targets.shrink_to_fit();
98+
self.downloading.shrink_to_fit();
99+
self.downloaded.shrink_to_fit();
96100
}
97101

98102
pub fn reset_downloading(&mut self, hashes: &[H256]) {
99103
cdebug!(SYNC, "Remove downloading by timeout {:?}", hashes);
100104
for hash in hashes {
101105
self.downloading.remove(&hash);
102106
}
107+
self.downloading.shrink_to_fit();
103108
}
104109

105110
pub fn drain(&mut self) -> Vec<(H256, Vec<UnverifiedTransaction>)> {
@@ -111,7 +116,9 @@ impl BodyDownloader {
111116
break
112117
}
113118
}
119+
self.downloaded.shrink_to_fit();
114120
self.targets.drain(0..result.len());
121+
self.targets.shrink_to_fit();
115122
result
116123
}
117124
}

sync/src/block/downloader/header.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl HeaderDownloader {
187187
}
188188
}
189189
}
190+
self.queued.shrink_to_fit();
190191
}
191192

192193
pub fn mark_as_queued(&mut self, hashes: Vec<H256>) {
@@ -195,5 +196,6 @@ impl HeaderDownloader {
195196
self.queued.insert(hash, header);
196197
}
197198
}
199+
self.downloaded.shrink_to_fit();
198200
}
199201
}

0 commit comments

Comments
 (0)