Skip to content

Commit

Permalink
perf: don't prettify json when not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 14, 2023
1 parent 0fc9af7 commit 54e73dc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ tempfile = { version = "3.8.0", optional = true }
fs_extra = { version = "1.3.0", optional = true }
rand = { version = "0.8", optional = true }
futures-util = { version = "0.3.28", optional = true }
memmap2 = "0.9.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
home = "0.5.5"
Expand Down
4 changes: 1 addition & 3 deletions src/artifact_output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ impl<T: Serialize> ArtifactFile<T> {
pub fn write(&self) -> Result<()> {
trace!("writing artifact file {:?} {}", self.file, self.version);
utils::create_parent_dir_all(&self.file)?;
fs::write(&self.file, serde_json::to_vec_pretty(&self.artifact)?)
.map_err(|err| SolcError::io(err, &self.file))?;
Ok(())
utils::write_json_file(&self.artifact, &self.file, 64 * 1024)
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use std::{
btree_map::{BTreeMap, Entry},
hash_map, BTreeSet, HashMap, HashSet,
},
fs::{self},
io::Write,
fs,
path::{Path, PathBuf},
time::{Duration, UNIX_EPOCH},
};
Expand Down Expand Up @@ -133,16 +132,13 @@ impl SolFilesCache {
/// Write the cache as json file to the given path
pub fn write(&self, path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
utils::create_parent_dir_all(path)?;
let file = fs::File::create(path).map_err(|err| SolcError::io(err, path))?;
tracing::trace!(
"writing cache with {} entries to json file: \"{}\"",
self.len(),
path.display()
);
let mut writer = std::io::BufWriter::with_capacity(1024 * 256, file);
serde_json::to_writer_pretty(&mut writer, self)?;
writer.flush().map_err(|e| SolcError::io(e, path))?;
utils::create_parent_dir_all(path)?;
utils::write_json_file(self, path, 128 * 1024)?;
tracing::trace!("cache file located: \"{}\"", path.display());
Ok(())
}
Expand Down Expand Up @@ -378,7 +374,7 @@ impl SolFilesCache {

pub async fn async_write(&self, path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
let content = serde_json::to_vec_pretty(self)?;
let content = serde_json::to_vec(self)?;
tokio::fs::write(path, content).await.map_err(|err| SolcError::io(err, path))
}
}
Expand Down
47 changes: 32 additions & 15 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//! Utility functions
use crate::{error::SolcError, SolcIoError};
use cfg_if::cfg_if;
use once_cell::sync::Lazy;
use regex::{Match, Regex};
use semver::Version;
use serde::{de::DeserializeOwned, Serialize};
use std::{
collections::HashSet,
fs,
io::Write,
ops::Range,
path::{Component, Path, PathBuf},
};

use crate::{error::SolcError, SolcIoError};
use once_cell::sync::Lazy;
use regex::{Match, Regex};
use semver::Version;
use serde::de::DeserializeOwned;
use tiny_keccak::{Hasher, Keccak};
use walkdir::WalkDir;

Expand Down Expand Up @@ -441,25 +442,41 @@ impl RuntimeOrHandle {
}
}

/// Creates a new named tempdir
/// Creates a new named tempdir.
#[cfg(any(test, feature = "project-util"))]
pub(crate) fn tempdir(name: &str) -> Result<tempfile::TempDir, SolcIoError> {
tempfile::Builder::new().prefix(name).tempdir().map_err(|err| SolcIoError::new(err, name))
}

/// Reads the json file and deserialize it into the provided type
/// 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 contents = std::fs::read_to_string(path).map_err(|err| SolcError::io(err, path))?;
serde_json::from_str(&contents).map_err(Into::into)
// See: https://github.com/serde-rs/json/issues/160
let file = fs::File::open(path).map_err(|err| SolcError::io(err, path))?;
let bytes = unsafe { memmap2::Mmap::map(&file).map_err(|err| SolcError::io(err, path))? };
serde_json::from_slice(&bytes).map_err(Into::into)
}

/// Writes serializes the provided value to JSON and writes it to a file.
pub fn write_json_file<T: Serialize>(
value: &T,
path: impl AsRef<Path>,
capacity: usize,
) -> Result<(), SolcError> {
let path = path.as_ref();
let file = fs::File::create(path).map_err(|err| SolcError::io(err, path))?;
let mut writer = std::io::BufWriter::with_capacity(capacity, file);
serde_json::to_writer(&mut writer, value)?;
writer.flush().map_err(|e| SolcError::io(e, path))
}

/// Creates the parent directory of the `file` and all its ancestors if it does not exist
/// See [`std::fs::create_dir_all()`]
/// Creates the parent directory of the `file` and all its ancestors if it does not exist.
///
/// See [`fs::create_dir_all()`].
pub fn create_parent_dir_all(file: impl AsRef<Path>) -> Result<(), SolcError> {
let file = file.as_ref();
if let Some(parent) = file.parent() {
std::fs::create_dir_all(parent).map_err(|err| {
fs::create_dir_all(parent).map_err(|err| {
SolcError::msg(format!(
"Failed to create artifact parent folder \"{}\": {}",
parent.display(),
Expand Down Expand Up @@ -488,7 +505,7 @@ mod tests {
create_dir_all(&path).unwrap();
let existing = path.join("Test.sol");
let non_existing = path.join("test.sol");
std::fs::write(&existing, b"").unwrap();
fs::write(&existing, b"").unwrap();

#[cfg(target_os = "linux")]
assert!(!non_existing.exists());
Expand All @@ -505,7 +522,7 @@ mod tests {
create_dir_all(&path).unwrap();
let existing = path.join("Test.sol");
let non_existing = path.join("test.sol");
std::fs::write(
fs::write(
existing,
"
pragma solidity ^0.8.10;
Expand Down

0 comments on commit 54e73dc

Please sign in to comment.