vexy-json-core 1.5.11

Core parser for Vexy JSON's forgiving JSON syntax
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// this_file: crates/core/src/optimization/simd.rs

//! SIMD-accelerated string parsing optimizations.
//!
//! This module provides SIMD-accelerated versions of string parsing functions
//! that can significantly improve performance for large strings. All functions
//! include scalar fallbacks for non-SIMD architectures.

use crate::error::{Error, Result};
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

/// SIMD-accelerated backslash detection for fast string escape analysis.
///
/// This function uses SIMD instructions to quickly scan for backslashes in strings,
/// which is the primary bottleneck in determining if a string needs unescaping.
/// Falls back to scalar implementation on non-x86_64 architectures.
#[inline]
pub fn has_backslash_simd(s: &str) -> bool {
    // Fast path for empty or very short strings
    if s.len() < 16 {
        return s.contains('\\');
    }

    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("sse2") {
            return unsafe { has_backslash_sse2(s.as_bytes()) };
        }
    }

    // Fallback to scalar implementation
    s.contains('\\')
}

/// SIMD-accelerated string validation for JSON compliance.
///
/// This function uses SIMD to quickly validate that a string contains only
/// valid JSON characters, which can speed up the parsing process.
#[inline]
pub fn validate_json_string_simd(s: &str) -> bool {
    // Fast path for empty strings
    if s.is_empty() {
        return true;
    }

    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("sse2") {
            return unsafe { validate_json_string_sse2(s.as_bytes()) };
        }
    }

    // Fallback to scalar validation
    validate_json_string_scalar(s)
}

/// SIMD-accelerated whitespace skipping for faster tokenization.
///
/// This function uses SIMD instructions to quickly skip over whitespace
/// characters in the input, which is a common operation during parsing.
#[inline]
pub fn skip_whitespace_simd(s: &str) -> usize {
    if s.is_empty() {
        return 0;
    }

    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("sse2") {
            return unsafe { skip_whitespace_sse2(s.as_bytes()) };
        }
    }

    // Fallback to scalar implementation
    skip_whitespace_scalar(s)
}

/// SIMD-accelerated number parsing for improved performance.
///
/// This function uses SIMD instructions to accelerate common number parsing
/// operations, particularly for integer detection and validation.
#[inline]
pub fn parse_number_simd(s: &str) -> Result<f64> {
    if s.is_empty() {
        return Err(Error::InvalidNumber(0));
    }

    // Fast path for simple integers using SIMD validation
    if s.len() <= 19 && is_simple_integer_simd(s) {
        // Use optimized integer parsing
        return parse_integer_fast(s);
    }

    // Fallback to standard parsing for complex numbers
    s.parse::<f64>().map_err(|_| Error::InvalidNumber(0))
}

// Private SIMD implementations for x86_64
#[cfg(target_arch = "x86_64")]
unsafe fn has_backslash_sse2(bytes: &[u8]) -> bool {
    let backslash_vec = _mm_set1_epi8(b'\\' as i8);
    let mut i = 0;

    // Process 16 bytes at a time
    while i + 16 <= bytes.len() {
        let chunk = _mm_loadu_si128(bytes.as_ptr().add(i) as *const __m128i);
        let cmp = _mm_cmpeq_epi8(chunk, backslash_vec);
        let mask = _mm_movemask_epi8(cmp);

        if mask != 0 {
            return true;
        }

        i += 16;
    }

    // Handle remaining bytes
    while i < bytes.len() {
        if bytes[i] == b'\\' {
            return true;
        }
        i += 1;
    }

    false
}

#[cfg(target_arch = "x86_64")]
unsafe fn validate_json_string_sse2(bytes: &[u8]) -> bool {
    let mut i = 0;

    // Process 16 bytes at a time
    while i + 16 <= bytes.len() {
        let chunk = _mm_loadu_si128(bytes.as_ptr().add(i) as *const __m128i);

        // Check for control characters (0x00-0x1F)
        let control_mask = _mm_cmpgt_epi8(chunk, _mm_set1_epi8(0x1F));
        let control_result = _mm_movemask_epi8(control_mask);

        // If we found control characters, check if they're valid JSON escapes
        if control_result != 0xFFFF {
            // Fallback to scalar validation for this chunk
            for j in 0..16 {
                if i + j >= bytes.len() {
                    break;
                }
                let byte = bytes[i + j];
                if byte < 0x20 && byte != b'\t' && byte != b'\n' && byte != b'\r' {
                    return false;
                }
            }
        }

        i += 16;
    }

    // Handle remaining bytes
    while i < bytes.len() {
        let byte = bytes[i];
        if byte < 0x20 && byte != b'\t' && byte != b'\n' && byte != b'\r' {
            return false;
        }
        i += 1;
    }

    true
}

#[cfg(target_arch = "x86_64")]
unsafe fn skip_whitespace_sse2(bytes: &[u8]) -> usize {
    let space_vec = _mm_set1_epi8(b' ' as i8);
    let tab_vec = _mm_set1_epi8(b'\t' as i8);
    let newline_vec = _mm_set1_epi8(b'\n' as i8);
    let carriage_vec = _mm_set1_epi8(b'\r' as i8);

    let mut i = 0;

    // Process 16 bytes at a time
    while i + 16 <= bytes.len() {
        let chunk = _mm_loadu_si128(bytes.as_ptr().add(i) as *const __m128i);

        // Check for whitespace characters
        let space_cmp = _mm_cmpeq_epi8(chunk, space_vec);
        let tab_cmp = _mm_cmpeq_epi8(chunk, tab_vec);
        let newline_cmp = _mm_cmpeq_epi8(chunk, newline_vec);
        let carriage_cmp = _mm_cmpeq_epi8(chunk, carriage_vec);

        // Combine all whitespace comparisons
        let whitespace_mask = _mm_or_si128(
            _mm_or_si128(space_cmp, tab_cmp),
            _mm_or_si128(newline_cmp, carriage_cmp),
        );

        let mask = _mm_movemask_epi8(whitespace_mask);

        // If all bytes are whitespace, continue to next chunk
        if mask == 0xFFFF {
            i += 16;
            continue;
        }

        // Find first non-whitespace byte in this chunk
        let first_non_whitespace = mask.trailing_ones() as usize;
        return i + first_non_whitespace;
    }

    // Handle remaining bytes
    while i < bytes.len() {
        match bytes[i] {
            b' ' | b'\t' | b'\n' | b'\r' => i += 1,
            _ => break,
        }
    }

    i
}

// Scalar fallback implementations
#[inline(always)]
fn validate_json_string_scalar(s: &str) -> bool {
    for byte in s.bytes() {
        if byte < 0x20 && byte != b'\t' && byte != b'\n' && byte != b'\r' {
            return false;
        }
    }
    true
}

#[inline(always)]
fn skip_whitespace_scalar(s: &str) -> usize {
    let mut i = 0;
    for byte in s.bytes() {
        match byte {
            b' ' | b'\t' | b'\n' | b'\r' => i += 1,
            _ => break,
        }
    }
    i
}

fn is_simple_integer_simd(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }

    let bytes = s.as_bytes();
    let mut start = 0;

    // Check for optional minus sign
    if bytes[0] == b'-' {
        if bytes.len() == 1 {
            return false;
        }
        start = 1;
    }

    // Check if all remaining characters are digits
    for &byte in &bytes[start..] {
        if !byte.is_ascii_digit() {
            return false;
        }
    }

    true
}

fn parse_integer_fast(s: &str) -> Result<f64> {
    // Fast path for integers without overflow checking
    let mut result = 0i64;
    let mut negative = false;
    let mut chars = s.chars();

    // Handle sign
    if let Some(first_char) = chars.next() {
        if first_char == '-' {
            negative = true;
        } else if first_char.is_ascii_digit() {
            result = (first_char as u8 - b'0') as i64;
        } else {
            return Err(Error::InvalidNumber(0));
        }
    }

    // Process remaining digits
    for ch in chars {
        if ch.is_ascii_digit() {
            result = result * 10 + (ch as u8 - b'0') as i64;
        } else {
            return Err(Error::InvalidNumber(0));
        }
    }

    if negative {
        result = -result;
    }

    Ok(result as f64)
}

/// SIMD-accelerated string unescaping with optimized common cases.
///
/// This function uses SIMD instructions to accelerate string unescaping
/// by quickly identifying regions that need processing and optimizing
/// the handling of common escape sequences.
#[inline]
pub fn unescape_string_simd(s: &str) -> Result<String> {
    // Fast path: if no backslashes, return as-is
    if !has_backslash_simd(s) {
        return Ok(s.to_string());
    }

    // Use SIMD-accelerated escape processing
    unescape_with_simd_scan(s)
}

/// Internal function that uses SIMD to scan for escape sequences
/// and processes them efficiently.
fn unescape_with_simd_scan(s: &str) -> Result<String> {
    let mut result = String::with_capacity(s.len());
    let bytes = s.as_bytes();
    let mut i = 0;

    while i < bytes.len() {
        // Use SIMD to find the next backslash
        let backslash_pos = find_next_backslash_simd(&bytes[i..]);

        if let Some(pos) = backslash_pos {
            // Copy the unescaped portion
            result.push_str(std::str::from_utf8(&bytes[i..i + pos]).unwrap());
            i += pos;

            // Process the escape sequence
            if i + 1 < bytes.len() {
                match bytes[i + 1] {
                    b'n' => result.push('\n'),
                    b't' => result.push('\t'),
                    b'r' => result.push('\r'),
                    b'\\' => result.push('\\'),
                    b'"' => result.push('"'),
                    b'\'' => result.push('\''),
                    b'/' => result.push('/'),
                    b'b' => result.push('\x08'),
                    b'f' => result.push('\x0C'),
                    b'u' => {
                        // Unicode escape sequence
                        if i + 5 < bytes.len() {
                            let hex_str = std::str::from_utf8(&bytes[i + 2..i + 6]).unwrap();
                            if let Ok(code) = u32::from_str_radix(hex_str, 16) {
                                if let Some(unicode_char) = std::char::from_u32(code) {
                                    result.push(unicode_char);
                                    i += 6;
                                    continue;
                                }
                            }
                        }
                        return Err(Error::InvalidEscape(i));
                    }
                    other => {
                        result.push('\\');
                        result.push(other as char);
                    }
                }
                i += 2;
            } else {
                result.push('\\');
                i += 1;
            }
        } else {
            // No more backslashes, copy the rest
            result.push_str(std::str::from_utf8(&bytes[i..]).unwrap());
            break;
        }
    }

    Ok(result)
}

/// Find the next backslash in a byte slice using SIMD when available.
fn find_next_backslash_simd(bytes: &[u8]) -> Option<usize> {
    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("sse2") {
            return unsafe { find_next_backslash_sse2(bytes) };
        }
    }

    // Fallback to scalar search
    bytes.iter().position(|&b| b == b'\\')
}

#[cfg(target_arch = "x86_64")]
unsafe fn find_next_backslash_sse2(bytes: &[u8]) -> Option<usize> {
    let backslash_vec = _mm_set1_epi8(b'\\' as i8);
    let mut i = 0;

    // Process 16 bytes at a time
    while i + 16 <= bytes.len() {
        let chunk = _mm_loadu_si128(bytes.as_ptr().add(i) as *const __m128i);
        let cmp = _mm_cmpeq_epi8(chunk, backslash_vec);
        let mask = _mm_movemask_epi8(cmp);

        if mask != 0 {
            return Some(i + mask.trailing_zeros() as usize);
        }

        i += 16;
    }

    // Handle remaining bytes
    while i < bytes.len() {
        if bytes[i] == b'\\' {
            return Some(i);
        }
        i += 1;
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_has_backslash_simd() {
        assert!(!has_backslash_simd("hello world"));
        assert!(has_backslash_simd("hello\\nworld"));
        assert!(has_backslash_simd("path\\to\\file"));
        assert!(!has_backslash_simd(""));

        // Test with long strings
        let long_string = "a".repeat(1000);
        assert!(!has_backslash_simd(&long_string));

        let long_string_with_backslash = format!("{}\\n{}", "a".repeat(500), "b".repeat(500));
        assert!(has_backslash_simd(&long_string_with_backslash));
    }

    #[test]
    fn test_validate_json_string_simd() {
        assert!(validate_json_string_simd("hello world"));
        assert!(validate_json_string_simd("with\ttabs"));
        assert!(validate_json_string_simd("with\nnewlines"));
        assert!(!validate_json_string_simd("with\x00null"));
        assert!(!validate_json_string_simd("with\x01control"));
        assert!(validate_json_string_simd(""));
    }

    #[test]
    fn test_skip_whitespace_simd() {
        assert_eq!(skip_whitespace_simd("hello"), 0);
        assert_eq!(skip_whitespace_simd("  hello"), 2);
        assert_eq!(skip_whitespace_simd("\t\n\r hello"), 4);
        assert_eq!(skip_whitespace_simd("   "), 3);
        assert_eq!(skip_whitespace_simd(""), 0);
    }

    #[test]
    fn test_parse_number_simd() {
        assert_eq!(parse_number_simd("42").unwrap(), 42.0);
        assert_eq!(parse_number_simd("-123").unwrap(), -123.0);
        assert_eq!(parse_number_simd("0").unwrap(), 0.0);
        assert!(parse_number_simd("").is_err());
        assert!(parse_number_simd("abc").is_err());
    }

    #[test]
    fn test_unescape_string_simd() {
        assert_eq!(unescape_string_simd("hello").unwrap(), "hello");
        assert_eq!(
            unescape_string_simd("hello\\nworld").unwrap(),
            "hello\nworld"
        );
        assert_eq!(
            unescape_string_simd("path\\\\to\\\\file").unwrap(),
            "path\\to\\file"
        );
        assert_eq!(
            unescape_string_simd("quote\\\"test").unwrap(),
            "quote\"test"
        );

        // Test unicode escapes
        assert_eq!(
            unescape_string_simd("hello\\u0041world").unwrap(),
            "helloAworld"
        );

        // Test with long strings
        let long_string = "a".repeat(1000);
        assert_eq!(unescape_string_simd(&long_string).unwrap(), long_string);
    }
}