From 671e93816a3c638389d5f601c1ea8b1ce0e6d73e Mon Sep 17 00:00:00 2001 From: Andrew Tulloch Date: Tue, 7 Jan 2025 05:02:33 -0800 Subject: [PATCH] Buffer file reads in `serde_json::from_reader` (#10341) ## Summary https://docs.rs/serde_json/latest/serde_json/fn.from_reader.html suggests that > When reading from a source against which short reads are not efficient, such as a [File](https://doc.rust-lang.org/std/fs/struct.File.html), you will want to apply your own buffering because serde_json will not buffer the input. See [std::io::BufReader](https://doc.rust-lang.org/std/io/struct.BufReader.html). Without this buffering, we observe a sequence of single byte reads which can be quite inefficient depending on the underlying filesystem. This adds buffering with `std::io::BufReader` to resolve this. ## Test Plan Unit tests cover this code. --- crates/uv-distribution-types/src/installed.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/uv-distribution-types/src/installed.rs b/crates/uv-distribution-types/src/installed.rs index 1d4cf9662..4bd528643 100644 --- a/crates/uv-distribution-types/src/installed.rs +++ b/crates/uv-distribution-types/src/installed.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::io::BufReader; use std::path::{Path, PathBuf}; use std::str::FromStr; @@ -309,7 +310,8 @@ impl InstalledDist { Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None), Err(err) => return Err(err.into()), }; - let direct_url = serde_json::from_reader::(file)?; + let direct_url = + serde_json::from_reader::, DirectUrl>(BufReader::new(file))?; Ok(Some(direct_url)) } @@ -321,7 +323,8 @@ impl InstalledDist { Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None), Err(err) => return Err(err.into()), }; - let cache_info = serde_json::from_reader::(file)?; + let cache_info = + serde_json::from_reader::, CacheInfo>(BufReader::new(file))?; Ok(Some(cache_info)) }