Skip to content

Commit 03ceee9

Browse files
committed
Replace ESCAPE array with is_escape fn
This is not backed by benchmarks, but it seems reasonable that we'd be more starved for cache than CPU in IO-bound tasks. It also simplifies code a bit and frees up some memory, which is probably a good thing.
1 parent 3faae03 commit 03ceee9

File tree

1 file changed

+7
-29
lines changed

1 file changed

+7
-29
lines changed

src/read.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ where
222222
{
223223
loop {
224224
let ch = tri!(next_or_eof(self));
225-
if !ESCAPE[ch as usize] {
225+
if !is_escape(ch) {
226226
scratch.push(ch);
227227
continue;
228228
}
@@ -343,7 +343,7 @@ where
343343
fn ignore_str(&mut self) -> Result<()> {
344344
loop {
345345
let ch = tri!(next_or_eof(self));
346-
if !ESCAPE[ch as usize] {
346+
if !is_escape(ch) {
347347
continue;
348348
}
349349
match ch {
@@ -819,33 +819,11 @@ pub trait Fused: private::Sealed {}
819819
impl<'a> Fused for SliceRead<'a> {}
820820
impl<'a> Fused for StrRead<'a> {}
821821

822-
// Lookup table of bytes that must be escaped. A value of true at index i means
823-
// that byte i requires an escape sequence in the input.
824-
static ESCAPE: [bool; 256] = {
825-
const CT: bool = true; // control character \x00..=\x1F
826-
const QU: bool = true; // quote \x22
827-
const BS: bool = true; // backslash \x5C
828-
const __: bool = false; // allow unescaped
829-
[
830-
// 1 2 3 4 5 6 7 8 9 A B C D E F
831-
CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 0
832-
CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1
833-
__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
834-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
835-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
836-
__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
837-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
838-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
839-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
840-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
841-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
842-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
843-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
844-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
845-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
846-
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
847-
]
848-
};
822+
// This is only used in IoRead. SliceRead hardcodes the arguments to memchr.
823+
#[cfg(feature = "std")]
824+
fn is_escape(ch: u8) -> bool {
825+
ch == b'"' || ch == b'\\' || ch < 0x20
826+
}
849827

850828
fn next_or_eof<'de, R>(read: &mut R) -> Result<u8>
851829
where

0 commit comments

Comments
 (0)