Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
perf: avoid serde_json::from_reader
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Aug 11, 2023
1 parent 5145992 commit daeae85
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
23 changes: 8 additions & 15 deletions ethers-etherscan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,21 +399,14 @@ impl Cache {

fn get<T: DeserializeOwned>(&self, prefix: &str, address: Address) -> Option<T> {
let path = self.root.join(prefix).join(format!("{address:?}.json"));
let reader = std::io::BufReader::new(std::fs::File::open(path).ok()?);
if let Ok(inner) = serde_json::from_reader::<_, CacheEnvelope<T>>(reader) {
// If this does not return None then we have passed the expiry
if SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system time is before unix epoch")
.checked_sub(Duration::from_secs(inner.expiry))
.is_some()
{
return None
}

return Some(inner.data)
}
None
let Ok(contents) = std::fs::read_to_string(path) else { return None };
let Ok(inner) = serde_json::from_str::<CacheEnvelope<T>>(&contents) else { return None };
// If this does not return None then we have passed the expiry
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system time is before unix epoch")
.checked_sub(Duration::from_secs(inner.expiry))
.map(|_| inner.data)
}
}

Expand Down
6 changes: 1 addition & 5 deletions ethers-solc/src/artifact_output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::{
ffi::OsString,
fmt, fs,
hash::Hash,
io,
ops::Deref,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -763,10 +762,7 @@ pub trait ArtifactOutput {
/// - The file does not exist
/// - The file's content couldn't be deserialized into the `Artifact` type
fn read_cached_artifact(path: impl AsRef<Path>) -> Result<Self::Artifact> {
let path = path.as_ref();
let file = fs::File::open(path).map_err(|err| SolcError::io(err, path))?;
let file = io::BufReader::new(file);
Ok(serde_json::from_reader(file)?)
crate::utils::read_json_file(path)
}

/// Read the cached artifacts that are located the paths the iterator yields
Expand Down
6 changes: 2 additions & 4 deletions ethers-solc/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,8 @@ pub(crate) fn tempdir(name: &str) -> Result<tempfile::TempDir, SolcIoError> {
/// Reads the json file and deserialize it into the provided type
pub fn read_json_file<T: DeserializeOwned>(path: impl AsRef<Path>) -> Result<T, SolcError> {
let path = path.as_ref();
let file = std::fs::File::open(path).map_err(|err| SolcError::io(err, path))?;
let file = std::io::BufReader::new(file);
let val: T = serde_json::from_reader(file)?;
Ok(val)
let contents = std::fs::read_to_string(path).map_err(|err| SolcError::io(err, path))?;
serde_json::from_str(&contents).map_err(Into::into)
}

/// Creates the parent directory of the `file` and all its ancestors if it does not exist
Expand Down

0 comments on commit daeae85

Please sign in to comment.