Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Speed up big chainspec json(~1.5 GB) load #10137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ serde_json = "1.0.68"
sp-runtime = { version = "4.0.0-dev", path = "../../primitives/runtime" }
sc-telemetry = { version = "4.0.0-dev", path = "../telemetry" }
codec = { package = "parity-scale-codec", version = "2.0.0" }
memmap2 = "0.5.0"
12 changes: 11 additions & 1 deletion client/chain-spec/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
}
}
Expand Down