Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub trait Formatter: Clone {

/// Writes a string as JSON string to the specified writer. Will escape the string if necessary.
/// If `need_quote` is `false`, the string will be written without quotes.
#[inline]
#[inline(always)]
fn write_string_fast<W>(
&mut self,
writer: &mut W,
Expand Down
58 changes: 44 additions & 14 deletions src/util/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,25 +478,55 @@ const NEED_ESCAPED: [u8; 256] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

// Split QUOTE_TAB into two aligned tables for faster indexing:
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment claims these are “aligned tables”, but [u8; 256] / [[u8; 8]; 256] have alignment 1, so there’s no guaranteed alignment benefit. Either enforce alignment with an #[repr(align(N))] wrapper (if required for the intended optimization) or adjust the comment to avoid implying alignment guarantees.

Suggested change
// Split QUOTE_TAB into two aligned tables for faster indexing:
// Split QUOTE_TAB into two tables for faster indexing:

Copilot uses AI. Check for mistakes.
// - QUOTE_LEN: 1 byte/entry (direct index, no multiply)
// - QUOTE_ESC: 8 bytes/entry (ch << 3 shift index, no multiply)
static QUOTE_LEN: [u8; 256] = {
let mut t = [0u8; 256];
let mut i = 0;
while i < 256 {
t[i] = QUOTE_TAB[i].0;
i += 1;
}
t
};

static QUOTE_ESC: [[u8; 8]; 256] = {
let mut t = [[0u8; 8]; 256];
let mut i = 0;
while i < 256 {
t[i] = QUOTE_TAB[i].1;
i += 1;
}
t
};

// only check the src length.
#[inline(always)]
unsafe fn escape_unchecked(src: &mut *const u8, nb: &mut usize, dst: &mut *mut u8) {
assert!(*nb >= 1);
debug_assert!(*nb >= 1);
loop {
let ch = *(*src);
let cnt = QUOTE_TAB[ch as usize].0 as usize;
assert!(
cnt != 0,
"char is {}, cnt is {}, NEED_ESCAPED is {}",
ch as char,
cnt,
NEED_ESCAPED[ch as usize]
);
std::ptr::copy_nonoverlapping(QUOTE_TAB[ch as usize].1.as_ptr(), *dst, 8);
(*dst) = (*dst).add(cnt);
(*src) = (*src).add(1);
(*nb) -= 1;
if (*nb) == 0 || NEED_ESCAPED[*(*src) as usize] == 0 {
// Fast path: " and \ are the most common escaped chars.
// Emit directly without table lookup.
if ch == b'"' {
*(*dst) = b'\\';
*(*dst).add(1) = b'"';
*dst = (*dst).add(2);
} else if ch == b'\\' {
*(*dst) = b'\\';
*(*dst).add(1) = b'\\';
*dst = (*dst).add(2);
} else {
// Slow path: control chars → table lookup with aligned tables.
let cnt = QUOTE_LEN[ch as usize] as usize;
debug_assert!(cnt != 0);
std::ptr::copy_nonoverlapping(QUOTE_ESC[ch as usize].as_ptr(), *dst, 8);
*dst = (*dst).add(cnt);
}
*src = (*src).add(1);
*nb -= 1;
if *nb == 0 || NEED_ESCAPED[*(*src) as usize] == 0 {
return;
}
}
Expand Down
Loading