Skip to content

Commit

Permalink
perf: simd-json (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind authored Aug 26, 2024
1 parent a4725cc commit fef4544
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 16 deletions.
191 changes: 185 additions & 6 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ memchr = "2.6.4"
arrayvec = "0.7.4"

codspeed-criterion-compat = { version = "2.3.3", default-features = false, optional = true }
simd-json = "=0.14.0-rc.2"

[dev-dependencies]
twox-hash = "1"
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum Error {
/// Overflow in Vlq handling
VlqOverflow,
/// a JSON parsing related failure
BadJson(serde_json::Error),
BadJson(simd_json::Error),
}

impl fmt::Display for Error {
Expand All @@ -29,8 +29,8 @@ impl fmt::Display for Error {

impl error::Error for Error {}

impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
impl From<simd_json::Error> for Error {
fn from(err: simd_json::Error) -> Error {
Error::BadJson(err)
}
}
16 changes: 9 additions & 7 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,27 +343,29 @@ struct RawSourceMap {

impl RawSourceMap {
pub fn from_reader<R: std::io::Read>(r: R) -> Result<Self> {
let raw: RawSourceMap = serde_json::from_reader(r)?;
let raw: RawSourceMap = simd_json::serde::from_reader(r)?;
Ok(raw)
}

pub fn from_slice(v: &[u8]) -> Result<Self> {
let raw: RawSourceMap = serde_json::from_slice(v)?;
pub fn from_slice(val: &[u8]) -> Result<Self> {
let mut v = val.to_vec();
let raw: RawSourceMap = simd_json::serde::from_slice(&mut v)?;
Ok(raw)
}

pub fn from_json(s: &str) -> Result<Self> {
let raw: RawSourceMap = serde_json::from_str(s)?;
pub fn from_json(val: &str) -> Result<Self> {
let mut v = val.as_bytes().to_vec();
let raw: RawSourceMap = simd_json::serde::from_slice(&mut v)?;
Ok(raw)
}

pub fn to_json(&self) -> Result<String> {
let json = serde_json::to_string(self)?;
let json = simd_json::serde::to_string(self)?;
Ok(json)
}

pub fn to_writer<W: std::io::Write>(&self, w: W) -> Result<()> {
serde_json::to_writer(w, self)?;
simd_json::to_writer(w, self)?;
Ok(())
}
}
Expand Down

0 comments on commit fef4544

Please sign in to comment.