Optimize Crates2Json::load_from_path: Use from_reader

which avoids reading the entire file into string at once.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-21 22:14:20 +10:00
parent fb3e35624b
commit d7ae1f242b
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -47,8 +47,8 @@ impl Crates2Json {
} }
pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, Crates2JsonParseError> { pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, Crates2JsonParseError> {
let file = fs::read_to_string(path)?; let file = fs::File::open(path.as_ref())?;
Ok(serde_json::from_str(&file)?) Ok(serde_json::from_reader(file)?)
} }
pub fn insert(&mut self, cvs: CrateVersionSource, info: CrateInfo) { pub fn insert(&mut self, cvs: CrateVersionSource, info: CrateInfo) {