Skip to content

Commit

Permalink
constant_memory: fix memory consumption when zipping file
Browse files Browse the repository at this point in the history
Add fix in constant memory mode to properly buffer reading the
worksheet file when writing it to zip archive.

This will ensure memory consumption remain constant.

Fixes #120
  • Loading branch information
Thibault Bedrignans authored and jmcnamara committed Dec 6, 2024
1 parent 401d7c0 commit 261d97f
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/packager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::collections::HashSet;
use std::io::{Seek, Write};

#[cfg(feature = "constant_memory")]
use std::io::Read;
use std::io::{BufRead, BufReader};

use std::sync::{Arc, Mutex};
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -440,15 +440,17 @@ impl<W: Write + Seek + Send> Packager<W> {
// We also need to flush the last remaining row.
worksheet.flush_last_row();

let mut buffer = Vec::new();
worksheet.file_writer.rewind().unwrap();
worksheet
.file_writer
.get_ref()
.read_to_end(&mut buffer)
.unwrap();

self.zip.write_all(&buffer)?;
let mut reader = BufReader::new(worksheet.file_writer.get_ref());
loop {
let buffer = reader.fill_buf().unwrap();
let length = buffer.len();
if length == 0 {
break;
}
self.zip.write_all(&buffer).unwrap();
reader.consume(length);
}
}

// Section 3. In memory metadata at end of the file.
Expand Down

0 comments on commit 261d97f

Please sign in to comment.