Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M1: full saves are allocated at their exact size (#523)

cyfung1031b98505c810b4
diff --git a/crates/cos/src/writer.rs b/crates/cos/src/writer.rs
index 73311d228..418891b3c 100644
--- a/crates/cos/src/writer.rs
+++ b/crates/cos/src/writer.rs
@@ -335,6 +335,9 @@ pub fn write_full(doc: &Document, opts: &SaveOptions) -> Result<Vec<u8>, CosErro
write_dict(&trailer, &mut out);
let _ = write!(out, "\nstartxref\n{xref_at}\n%%EOF\n");
}
+ // The size isn't known until the end, and the buffer grew by doubling. The result is often
+ // kept as a document's working bytes for as long as it's open: drop the unused capacity.
+ out.shrink_to_fit();
Ok(out)
}
diff --git a/crates/cos/tests/writer.rs b/crates/cos/tests/writer.rs
index 41158f10d..6ba653757 100644
--- a/crates/cos/tests/writer.rs
+++ b/crates/cos/tests/writer.rs
@@ -112,6 +112,29 @@ fn incremental_saves_are_allocated_at_their_exact_size() {
}
}
+/// A full save grows its buffer as it writes, so it used to end with up to as much again unused,
+/// kept alive with the document's working bytes. It now ends at its exact size.
+#[test]
+fn full_saves_are_allocated_at_their_exact_size() {
+ let mut doc = Document::open(Arc::new(fixture())).unwrap();
+ let data: Vec<u8> = (0..100_000u32).map(|i| (i % 251) as u8).collect();
+ let added = doc.add(Object::Stream(Stream { dict: Dict::new(), raw: Arc::new(data.clone()) }));
+ let root = doc.root().unwrap();
+ doc.update_dict(root, |d| d.set(b"Extra".to_vec(), Object::Ref(added))).unwrap();
+ for object_streams in [true, false] {
+ let full = write_full(&doc, &SaveOptions { object_streams, ..SaveOptions::default() }).unwrap();
+ assert_eq!(full.capacity(), full.len(), "object streams: {object_streams}");
+ let back = Document::open(Arc::new(full.clone())).unwrap();
+ assert!(back.repair_log().is_empty(), "{:?}", back.repair_log());
+ let extra = back.dict(&Object::Ref(back.root().unwrap())).and_then(|d| d.get(b"Extra").cloned()).unwrap();
+ match &*back.resolve(&extra) {
+ Object::Stream(s) => assert_eq!(*s.raw, data),
+ other => panic!("{other:?}"),
+ }
+ assert_eq!(hayro_pages(&full), 4);
+ }
+}
+
#[test]
fn qpdf_accepts_object_stream_output() {
let Ok(out) = std::process::Command::new("qpdf").arg("--version").output() else {