My program parse big enough json document (30MB),
on machine with slow CPU it takes 70ms, I want to speedup the process,
and I find out that 27% of parsing take place into my foo_document_type_deserialize,
is it possible to improve this function, may be there is way to skip String allocation here: let s = String::deserialize(deserializer)?;?
I completly sure that strings that represent enum values doesn't contain special json characters like \b \f \n \r \t \" \\, so it should be safe to work with unescaped string.
use serde::{Deserialize, Deserializer};
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FooDocument {
// other fields...
#[serde(rename = "type")]
#[serde(deserialize_with = "foo_document_type_deserialize")]
doc_type: FooDocumentType,
}
fn foo_document_type_deserialize<'de, D>(deserializer: D) -> Result<FooDocumentType, D::Error>
where
D: Deserializer<'de>,
{
use self::FooDocumentType::*;
let s = String::deserialize(deserializer)?;
match s.as_str() {
"tir lim bom bom" => Ok(Var1),
"hgga;hghau" => Ok(Var2),
"hgueoqtyhit4t" => Ok(Var3),
"Text" | "Type not detected" | "---" => Ok(Unknown),
_ => Err(serde::de::Error::custom(format!(
"Unsupported foo document type '{}'",
s
))),
}
}
#[derive(Debug, Clone, Copy)]
pub enum FooDocumentType {
Unknown,
Var1,
Var2,
Var3,
}
--release? - hellowFilewithfs::read_to_string+serde_json::from_str, because of github.com/serde-rs/json/issues/160 . Still too slow. - user1244932Stringfor each deserialization is likely contributiing to the performance issue here. Serde provides animpl Deserialize for &str. Can you use that instead of theStringimpl? - Wesley Wiserlet s = String::deserialize(deserializer)?;, try this:let s = <&str as Deserialize<'de>>::deserialize(deserializer)?;- Wesley Wiser