From c2ce265afacc416af86918377298d76b53cde79b Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 21 Jul 2022 22:18:08 +1000 Subject: [PATCH] Optimize `Crates2Json::write_to_path`: Use `to_writer` which avoids allocating a `Vec` just to hold serialized data. Signed-off-by: Jiahao XU --- src/metafiles/v2.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/metafiles/v2.rs b/src/metafiles/v2.rs index f4ea5573..c7d0f337 100644 --- a/src/metafiles/v2.rs +++ b/src/metafiles/v2.rs @@ -60,7 +60,8 @@ impl Crates2Json { } pub fn write_to_path(&self, path: impl AsRef) -> Result<(), Crates2JsonParseError> { - fs::write(path, &serde_json::to_vec(&self)?)?; + let file = fs::File::create(path.as_ref())?; + serde_json::to_writer(file, &self)?; Ok(()) }