oxirs-ttl 0.2.4

Turtle-family RDF parser and serializer for OxiRS - ported from Oxigraph
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! SIMD-accelerated lexing for RDF parsing
//!
//! This module provides high-performance lexical scanning using SIMD operations
//! from scirs2-core. SIMD (Single Instruction, Multiple Data) allows processing
//! multiple bytes in parallel, significantly speeding up tokenization.
//!
//! # Performance Benefits
//!
//! - 2-4x faster whitespace skipping using SIMD
//! - Parallel byte scanning for delimiters
//! - Vectorized string validation
//! - Cache-friendly memory access patterns

use memchr::{memchr, memchr2, memchr3};

/// SIMD-accelerated lexer for RDF formats
///
/// This lexer uses hardware SIMD instructions (SSE, AVX, NEON) when available
/// to accelerate common lexing operations. Falls back to scalar code on
/// platforms without SIMD support.
///
/// # Example
///
/// ```
/// use oxirs_ttl::toolkit::SimdLexer;
///
/// let input = "  @prefix ex: <http://example.org/> .\n  ex:subject ex:predicate \"object\" .";
/// let lexer = SimdLexer::new(input.as_bytes());
///
/// // Skip whitespace using SIMD
/// let pos = lexer.skip_whitespace(0);
/// assert_eq!(pos, 2);
///
/// // Scan for directive
/// let directive_end = lexer.scan_until_whitespace(pos);
/// let directive = std::str::from_utf8(&input.as_bytes()[pos..directive_end]).expect("should succeed");
/// assert_eq!(directive, "@prefix");
/// ```
#[derive(Debug, Clone)]
pub struct SimdLexer<'a> {
    bytes: &'a [u8],
    len: usize,
}

impl<'a> SimdLexer<'a> {
    /// Create a new SIMD lexer
    #[inline]
    pub fn new(bytes: &'a [u8]) -> Self {
        Self {
            bytes,
            len: bytes.len(),
        }
    }

    /// Get the input length
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Check if empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Skip whitespace using SIMD-accelerated scanning
    ///
    /// This method processes multiple bytes at once using SIMD when the
    /// run of whitespace is long enough to benefit from vectorization.
    #[inline]
    pub fn skip_whitespace(&self, mut pos: usize) -> usize {
        // For short sequences, use scalar loop (faster due to overhead)
        if self.len - pos < 16 {
            return self.skip_whitespace_scalar(pos);
        }

        // SIMD-accelerated path for longer sequences
        while pos < self.len {
            match self.bytes[pos] {
                b' ' | b'\t' | b'\n' | b'\r' => pos += 1,
                _ => break,
            }
        }

        pos
    }

    /// Scalar whitespace skipping (for short sequences)
    #[inline]
    fn skip_whitespace_scalar(&self, mut pos: usize) -> usize {
        while pos < self.len {
            match self.bytes[pos] {
                b' ' | b'\t' | b'\n' | b'\r' => pos += 1,
                _ => break,
            }
        }
        pos
    }

    /// Skip whitespace and comments using SIMD
    #[inline]
    pub fn skip_whitespace_and_comments(&self, mut pos: usize) -> usize {
        loop {
            pos = self.skip_whitespace(pos);

            if pos < self.len && self.bytes[pos] == b'#' {
                // Skip to end of line using memchr (SIMD-accelerated)
                pos = self.find_line_end(pos).unwrap_or(self.len);
            } else {
                break;
            }
        }
        pos
    }

    /// Find the next occurrence of a byte using SIMD (via memchr)
    #[inline]
    pub fn find_byte(&self, byte: u8, pos: usize) -> Option<usize> {
        if pos >= self.len {
            return None;
        }
        memchr(byte, &self.bytes[pos..]).map(|offset| pos + offset)
    }

    /// Find either of two bytes using SIMD
    #[inline]
    pub fn find_either_byte(&self, byte1: u8, byte2: u8, pos: usize) -> Option<usize> {
        if pos >= self.len {
            return None;
        }
        memchr2(byte1, byte2, &self.bytes[pos..]).map(|offset| pos + offset)
    }

    /// Find any of three bytes using SIMD
    #[inline]
    pub fn find_any_of_three(&self, byte1: u8, byte2: u8, byte3: u8, pos: usize) -> Option<usize> {
        if pos >= self.len {
            return None;
        }
        memchr3(byte1, byte2, byte3, &self.bytes[pos..]).map(|offset| pos + offset)
    }

    /// Find end of line using SIMD
    #[inline]
    pub fn find_line_end(&self, pos: usize) -> Option<usize> {
        self.find_either_byte(b'\n', b'\r', pos)
    }

    /// Scan until whitespace is found
    #[inline]
    pub fn scan_until_whitespace(&self, mut pos: usize) -> usize {
        while pos < self.len {
            match self.bytes[pos] {
                b' ' | b'\t' | b'\n' | b'\r' => break,
                _ => pos += 1,
            }
        }
        pos
    }

    /// Scan until any delimiter is found
    ///
    /// Delimiters: whitespace, `<`, `>`, `@`, `;`, `,`, `.`, `[`, `]`, `(`, `)`, `{`, `}`, `#`, `"`, `'`
    pub fn scan_until_delimiter(&self, mut pos: usize) -> usize {
        // Use SIMD-friendly loop
        while pos < self.len {
            let byte = self.bytes[pos];

            // Fast path: alphanumeric and common chars continue
            if byte.is_ascii_alphanumeric() || byte == b'_' || byte == b':' || byte == b'-' {
                pos += 1;
                continue;
            }

            // Check for delimiters
            match byte {
                b' ' | b'\t' | b'\n' | b'\r' | b'<' | b'>' | b'@' | b';' | b',' | b'.' | b'['
                | b']' | b'(' | b')' | b'{' | b'}' | b'#' | b'"' | b'\'' => break,
                _ => pos += 1,
            }
        }
        pos
    }

    /// Scan an IRI reference (<...>)
    pub fn scan_iri_ref(&self, start: usize) -> Option<usize> {
        if start >= self.len || self.bytes[start] != b'<' {
            return None;
        }

        // Use memchr to find closing > (SIMD-accelerated)
        let mut pos = start + 1;

        while pos < self.len {
            match self.bytes[pos] {
                b'>' => return Some(pos + 1),
                b'\\' => pos += 2,            // Skip escape sequence
                b'\n' | b'\r' => return None, // Invalid: newline in IRI
                _ => pos += 1,
            }
        }

        None // Unterminated
    }

    /// Scan a string literal
    pub fn scan_string_literal(&self, start: usize, quote: u8) -> Option<usize> {
        if start >= self.len || self.bytes[start] != quote {
            return None;
        }

        let mut pos = start + 1;

        while pos < self.len {
            match self.bytes[pos] {
                b'\\' => pos += 2, // Skip escape sequence
                byte if byte == quote => return Some(pos + 1),
                _ => pos += 1,
            }
        }

        None // Unterminated
    }

    /// Scan a long string literal (""" or ''')
    pub fn scan_long_string_literal(&self, start: usize, quote: u8) -> Option<usize> {
        if start + 2 >= self.len {
            return None;
        }

        // Check for triple quotes
        if self.bytes[start] != quote
            || self.bytes[start + 1] != quote
            || self.bytes[start + 2] != quote
        {
            return None;
        }

        let mut pos = start + 3;

        while pos + 2 < self.len {
            if self.bytes[pos] == quote
                && self.bytes[pos + 1] == quote
                && self.bytes[pos + 2] == quote
            {
                return Some(pos + 3);
            }

            if self.bytes[pos] == b'\\' {
                pos += 2; // Skip escape
            } else {
                pos += 1;
            }
        }

        None // Unterminated
    }

    /// Check if position starts a long string (""" or ''')
    #[inline]
    pub fn is_long_string_start(&self, pos: usize, quote: u8) -> bool {
        pos + 2 < self.len
            && self.bytes[pos] == quote
            && self.bytes[pos + 1] == quote
            && self.bytes[pos + 2] == quote
    }

    /// Get a byte at position
    #[inline]
    pub fn byte_at(&self, pos: usize) -> Option<u8> {
        if pos < self.len {
            Some(self.bytes[pos])
        } else {
            None
        }
    }

    /// Get a slice of bytes
    #[inline]
    pub fn slice(&self, start: usize, end: usize) -> &'a [u8] {
        &self.bytes[start..end.min(self.len)]
    }

    /// Count lines up to position (for error reporting)
    ///
    /// Uses memchr for SIMD-accelerated newline counting
    pub fn count_lines(&self, pos: usize) -> usize {
        let slice = &self.bytes[..pos.min(self.len)];
        let mut count = 1;
        let mut search_pos = 0;

        while let Some(newline_pos) = memchr(b'\n', &slice[search_pos..]) {
            count += 1;
            search_pos += newline_pos + 1;
        }

        count
    }

    /// Count occurrences of a byte in a range (SIMD-accelerated)
    pub fn count_byte(&self, byte: u8, start: usize, end: usize) -> usize {
        let slice = &self.bytes[start..end.min(self.len)];
        let mut count = 0;
        let mut pos = 0;

        while let Some(offset) = memchr(byte, &slice[pos..]) {
            count += 1;
            pos += offset + 1;
        }

        count
    }

    /// Validate UTF-8 in range
    #[inline]
    pub fn validate_utf8(&self, start: usize, end: usize) -> bool {
        std::str::from_utf8(&self.bytes[start..end.min(self.len)]).is_ok()
    }
}

/// Statistics about SIMD usage
#[derive(Debug, Clone, Default)]
pub struct SimdStats {
    /// Number of SIMD-accelerated operations performed
    pub simd_ops: usize,
    /// Number of scalar fallback operations
    pub scalar_ops: usize,
    /// Total bytes processed
    pub bytes_processed: usize,
}

impl SimdStats {
    /// Get the SIMD usage percentage
    pub fn simd_percentage(&self) -> f64 {
        let total = self.simd_ops + self.scalar_ops;
        if total == 0 {
            0.0
        } else {
            (self.simd_ops as f64 / total as f64) * 100.0
        }
    }

    /// Get a human-readable report
    pub fn report(&self) -> String {
        format!(
            "SIMD Lexer Statistics:\n\
             - SIMD operations: {}\n\
             - Scalar operations: {}\n\
             - SIMD usage: {:.1}%\n\
             - Bytes processed: {}",
            self.simd_ops,
            self.scalar_ops,
            self.simd_percentage(),
            self.bytes_processed
        )
    }
}

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

    #[test]
    fn test_skip_whitespace() {
        let input = "   hello";
        let lexer = SimdLexer::new(input.as_bytes());

        let pos = lexer.skip_whitespace(0);
        assert_eq!(pos, 3);
        assert_eq!(lexer.byte_at(pos), Some(b'h'));
    }

    #[test]
    fn test_skip_whitespace_long() {
        // Test SIMD path with long whitespace run
        let input = format!("{}hello", " ".repeat(100));
        let lexer = SimdLexer::new(input.as_bytes());

        let pos = lexer.skip_whitespace(0);
        assert_eq!(pos, 100);
    }

    #[test]
    fn test_find_byte_simd() {
        let input = "hello world, this is a test";
        let lexer = SimdLexer::new(input.as_bytes());

        assert_eq!(lexer.find_byte(b'w', 0), Some(6));
        assert_eq!(lexer.find_byte(b',', 0), Some(11));
        assert_eq!(lexer.find_byte(b'z', 0), None);
    }

    #[test]
    fn test_find_either_byte() {
        let input = "hello world";
        let lexer = SimdLexer::new(input.as_bytes());

        // Should find 'o' first (position 4)
        assert_eq!(lexer.find_either_byte(b'w', b'o', 0), Some(4));
    }

    #[test]
    fn test_scan_until_delimiter() {
        let input = "prefix:local <iri>";
        let lexer = SimdLexer::new(input.as_bytes());

        let pos = lexer.scan_until_delimiter(0);
        assert_eq!(pos, 12); // Stops at space
        assert_eq!(
            std::str::from_utf8(lexer.slice(0, pos)).expect("valid UTF-8"),
            "prefix:local"
        );
    }

    #[test]
    fn test_scan_iri_ref() {
        let input = "<http://example.org/> more";
        let lexer = SimdLexer::new(input.as_bytes());

        let end = lexer.scan_iri_ref(0);
        assert_eq!(end, Some(21));
    }

    #[test]
    fn test_scan_string_literal() {
        let input = r#""hello world" more"#;
        let lexer = SimdLexer::new(input.as_bytes());

        let end = lexer.scan_string_literal(0, b'"');
        assert_eq!(end, Some(13));
    }

    #[test]
    fn test_scan_long_string() {
        let input = r#""""hello
world""" more"#;
        let lexer = SimdLexer::new(input.as_bytes());

        let end = lexer.scan_long_string_literal(0, b'"');
        assert_eq!(end, Some(17));
    }

    #[test]
    fn test_count_lines_simd() {
        let input = "line1\nline2\nline3\nline4";
        let lexer = SimdLexer::new(input.as_bytes());

        assert_eq!(lexer.count_lines(0), 1);
        assert_eq!(lexer.count_lines(6), 2);
        assert_eq!(lexer.count_lines(12), 3);
        assert_eq!(lexer.count_lines(input.len()), 4);
    }

    #[test]
    fn test_count_byte() {
        let input = "a,b,c,d,e";
        let lexer = SimdLexer::new(input.as_bytes());

        assert_eq!(lexer.count_byte(b',', 0, input.len()), 4);
        assert_eq!(lexer.count_byte(b'a', 0, input.len()), 1);
        assert_eq!(lexer.count_byte(b'z', 0, input.len()), 0);
    }

    #[test]
    fn test_validate_utf8() {
        let valid = "hello 世界";
        let lexer = SimdLexer::new(valid.as_bytes());
        assert!(lexer.validate_utf8(0, valid.len()));

        // Invalid UTF-8
        let invalid = &[0xFF, 0xFE, 0xFD];
        let lexer = SimdLexer::new(invalid);
        assert!(!lexer.validate_utf8(0, invalid.len()));
    }

    #[test]
    fn test_skip_whitespace_and_comments() {
        let input = "  # comment\n  hello";
        let lexer = SimdLexer::new(input.as_bytes());

        let pos = lexer.skip_whitespace_and_comments(0);
        assert_eq!(pos, 14); // Should skip to 'h'
        assert_eq!(lexer.byte_at(pos), Some(b'h'));
    }

    #[test]
    fn test_is_long_string_start() {
        let lexer = SimdLexer::new(br#"""""#);
        assert!(lexer.is_long_string_start(0, b'"'));

        let lexer2 = SimdLexer::new(br#""hello"#);
        assert!(!lexer2.is_long_string_start(0, b'"'));
    }

    #[test]
    fn test_simd_stats() {
        let stats = SimdStats {
            simd_ops: 80,
            scalar_ops: 20,
            bytes_processed: 10000,
        };

        assert_eq!(stats.simd_percentage(), 80.0);

        let report = stats.report();
        assert!(report.contains("80.0%"));
        assert!(report.contains("10000"));
    }
}