mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 13:57:58 +00:00
Speed up big chainspec json(~1.5 GB) load (#10137)
* Speed up chainspec json load * Update client/chain-spec/src/chain_spec.rs * Update client/chain-spec/src/chain_spec.rs * Update client/chain-spec/src/chain_spec.rs * Load the chainspec through `mmap` Co-authored-by: icodezjb <icodezjb@users.noreply.github.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: Jan Bujak <jan@parity.io>
This commit is contained in:
@@ -285,10 +285,20 @@ impl<G, E: serde::de::DeserializeOwned> ChainSpec<G, E> {
|
||||
|
||||
/// Parse json file into a `ChainSpec`
|
||||
pub fn from_json_file(path: PathBuf) -> Result<Self, String> {
|
||||
// We mmap the file into memory first, as this is *a lot* faster than using
|
||||
// `serde_json::from_reader`. See https://github.com/serde-rs/json/issues/160
|
||||
let file = File::open(&path)
|
||||
.map_err(|e| format!("Error opening spec file `{}`: {}", path.display(), e))?;
|
||||
|
||||
// SAFETY: `mmap` is fundamentally unsafe since technically the file can change
|
||||
// underneath us while it is mapped; in practice it's unlikely to be a problem
|
||||
let bytes = unsafe {
|
||||
memmap2::Mmap::map(&file)
|
||||
.map_err(|e| format!("Error mmaping spec file `{}`: {}", path.display(), e))?
|
||||
};
|
||||
|
||||
let client_spec =
|
||||
json::from_reader(file).map_err(|e| format!("Error parsing spec file: {}", e))?;
|
||||
json::from_slice(&bytes).map_err(|e| format!("Error parsing spec file: {}", e))?;
|
||||
Ok(ChainSpec { client_spec, genesis: GenesisSource::File(path) })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user