Skip to content

Commit 818ec98

Browse files
committed
Stop instantiating parse_value in MapKey::deserialize_any
The parse_value method is the most expensive method in serde_json in terms of compile time. Before this change, we were instantiating it twice for every derive(Deserialize) struct because every struct has two different Visitor impls -- one for the key type and one for the struct overall -- see https://serde.rs/deserialize-struct.html. This improves compile time of json-benchmark by 20%.
1 parent a882cd4 commit 818ec98

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/de.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,16 @@ where
12361236
where
12371237
V: de::Visitor<'de>,
12381238
{
1239-
self.de.parse_value(visitor)
1239+
self.de.eat_char();
1240+
self.de.str_buf.clear();
1241+
let value = match try!(self.de.read.parse_str(&mut self.de.str_buf)) {
1242+
Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
1243+
Reference::Copied(s) => visitor.visit_str(s),
1244+
};
1245+
match value {
1246+
Ok(value) => Ok(value),
1247+
Err(err) => Err(self.de.fix_position(err)),
1248+
}
12401249
}
12411250

12421251
deserialize_integer_key!(deserialize_i8 => visit_i8);

0 commit comments

Comments
 (0)