mirror of
https://github.com/serde-rs/json.git
synced 2025-11-03 15:03:04 +00:00
Mark \u parsing as cold
This counterintuitively speeds up War and Peace 275 -> 290 MB/s (+5%) by enabling inlining of encode_utf8 and extend_from_slice.
This commit is contained in:
parent
cf771a0471
commit
a38dbf3708
24
src/read.rs
24
src/read.rs
@ -882,7 +882,23 @@ fn parse_escape<'de, R: Read<'de>>(
|
|||||||
b'n' => scratch.push(b'\n'),
|
b'n' => scratch.push(b'\n'),
|
||||||
b'r' => scratch.push(b'\r'),
|
b'r' => scratch.push(b'\r'),
|
||||||
b't' => scratch.push(b'\t'),
|
b't' => scratch.push(b'\t'),
|
||||||
b'u' => {
|
b'u' => return parse_unicode_escape(read, validate, scratch),
|
||||||
|
_ => {
|
||||||
|
return error(read, ErrorCode::InvalidEscape);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses a JSON \u escape and appends it into the scratch space. Assumes \u
|
||||||
|
/// has just been read.
|
||||||
|
#[cold]
|
||||||
|
fn parse_unicode_escape<'de, R: Read<'de>>(
|
||||||
|
read: &mut R,
|
||||||
|
validate: bool,
|
||||||
|
scratch: &mut Vec<u8>,
|
||||||
|
) -> Result<()> {
|
||||||
fn encode_surrogate(scratch: &mut Vec<u8>, n: u16) {
|
fn encode_surrogate(scratch: &mut Vec<u8>, n: u16) {
|
||||||
scratch.extend_from_slice(&[
|
scratch.extend_from_slice(&[
|
||||||
(n >> 12 & 0b0000_1111) as u8 | 0b1110_0000,
|
(n >> 12 & 0b0000_1111) as u8 | 0b1110_0000,
|
||||||
@ -957,12 +973,6 @@ fn parse_escape<'de, R: Read<'de>>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
|
scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return error(read, ErrorCode::InvalidEscape);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user