Skip to content

Commit 18b1b2d

Browse files
authored
fix(memory): Limit memory usage for restore when having large pack files (#165)
closes rustic-rs/rustic#1067
1 parent bda6793 commit 18b1b2d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crates/core/src/commands/restore.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ use crate::{
3333
pub(crate) mod constants {
3434
/// The maximum number of reader threads to use for restoring.
3535
pub(crate) const MAX_READER_THREADS_NUM: usize = 20;
36+
/// The maximum size of pack-part which is read at once from the backend.
37+
/// (needed to limit the memory size used for large backends)
38+
pub(crate) const LIMIT_PACK_READ: u32 = 40 * 1024 * 1024; // 40 MiB
3639
}
3740

3841
type RestoreInfo = BTreeMap<(Id, BlobLocation), Vec<FileLocation>>;
@@ -461,8 +464,14 @@ fn restore_contents<P: ProgressBars, S: Open>(
461464
.collect();
462465
(pack, bl.offset, bl.length, from_file, name_dests)
463466
})
467+
// optimize reading from backend by reading many blobs in a row
464468
.coalesce(|mut x, mut y| {
465-
if x.0 == y.0 && x.3.is_none() && y.1 == x.1 + x.2 {
469+
if x.0 == y.0 // if the pack is identical
470+
&& x.3.is_none() // and we don't read from a present file
471+
&& y.1 == x.1 + x.2 // and the blobs are contiguous
472+
// and we don't trespass the limit
473+
&& x.2 + y.2 < constants::LIMIT_PACK_READ
474+
{
466475
x.2 += y.2;
467476
x.4.append(&mut y.4);
468477
Ok(x)

0 commit comments

Comments
 (0)