Skip to content

Commit 034c5de

Browse files
committed
SIMD: Match control char and double quote in one pass
`c < 32 || c == 34` is equivalent to `c ^ 2 < 33`. Found in: https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/ The gain seem mostly present on micro-benchmark, and even there aren't very consistent, but it's never slower. ``` == Encoding long string (124001 bytes) ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 5.295k i/100ms Calculating ------------------------------------- after 55.796k (± 3.4%) i/s (17.92 μs/i) - 280.635k in 5.035690s Comparison: before: 49840.7 i/s after: 55795.8 i/s - 1.12x faster ```
1 parent 2739de4 commit 034c5de

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

ext/json/ext/generator/generator.c

+10-15
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,12 @@ static inline FORCE_INLINE uint64_t neon_rules_update(const char *ptr)
320320
{
321321
uint8x16_t chunk = vld1q_u8((const unsigned char *)ptr);
322322

323-
const uint8x16_t lower_bound = vdupq_n_u8(' ');
324-
const uint8x16_t backslash = vdupq_n_u8('\\');
325-
const uint8x16_t dblquote = vdupq_n_u8('\"');
323+
// Trick: c < 32 || c == 34 can be factored as c ^ 2 < 33
324+
// https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/
325+
const uint8x16_t too_low_or_dbl_quote = vcltq_u8(veorq_u8(chunk, vdupq_n_u8(2)), vdupq_n_u8(33));
326326

327-
uint8x16_t too_low = vcltq_u8(chunk, lower_bound);
328-
uint8x16_t has_backslash = vceqq_u8(chunk, backslash);
329-
uint8x16_t has_dblquote = vceqq_u8(chunk, dblquote);
330-
uint8x16_t needs_escape = vorrq_u8(too_low, vorrq_u8(has_backslash, has_dblquote));
327+
uint8x16_t has_backslash = vceqq_u8(chunk, vdupq_n_u8('\\'));
328+
uint8x16_t needs_escape = vorrq_u8(too_low_or_dbl_quote, has_backslash);
331329

332330
return neon_match_mask(needs_escape);
333331
}
@@ -467,14 +465,11 @@ static inline TARGET_SSE2 FORCE_INLINE int sse2_update(const char *ptr)
467465
{
468466
__m128i chunk = _mm_loadu_si128((__m128i const*)ptr);
469467

470-
const __m128i lower_bound = _mm_set1_epi8(' ');
471-
const __m128i backslash = _mm_set1_epi8('\\');
472-
const __m128i dblquote = _mm_set1_epi8('\"');
473-
474-
__m128i too_low = _mm_cmplt_epu8(chunk, lower_bound);
475-
__m128i has_backslash = _mm_cmpeq_epi8(chunk, backslash);
476-
__m128i has_dblquote = _mm_cmpeq_epi8(chunk, dblquote);
477-
__m128i needs_escape = _mm_or_si128(too_low, _mm_or_si128(has_backslash, has_dblquote));
468+
// Trick: c < 32 || c == 34 can be factored as c ^ 2 < 33
469+
// https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/
470+
__m128i too_low_or_dbl_quote = _mm_cmplt_epu8(_mm_xor_si128(chunk, _mm_set1_epi8(2)), _mm_set1_epi8(33));
471+
__m128i has_backslash = _mm_cmpeq_epi8(chunk, _mm_set1_epi8('\\'));
472+
__m128i needs_escape = _mm_or_si128(too_low_or_dbl_quote, has_backslash);
478473
return _mm_movemask_epi8(needs_escape);
479474
}
480475

0 commit comments

Comments
 (0)