pjson-rs 0.5.1

Priority JSON Streaming Protocol - high-performance priority-based JSON streaming (requires nightly Rust)
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
//! Simplified serde-based parser for MVP
//!
//! This uses serde_json as the foundation for JSON parsing and focuses
//! on PJS's unique value proposition: semantic analysis and streaming.

use crate::{Error, Result, SemanticMeta, Frame, FrameFlags};
use crate::semantic::{SemanticType, ProcessingStrategy};
use crate::security::SecurityValidator;
use crate::config::SecurityConfig;
use serde_json::{self, Value};
use std::time::Instant;

/// Configuration for semantic detection
#[derive(Debug, Clone)]
pub struct SemanticConfig {
    /// Enable semantic type detection
    pub detect_semantics: bool,
    /// Minimum size for chunking (bytes)
    pub min_chunk_size: usize,
    /// Maximum size for chunks
    pub max_chunk_size: usize,
    /// Enable streaming mode for large arrays/objects
    pub enable_streaming: bool,
}

/// Input complexity estimation for backend selection
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Complexity {
    /// Simple flat structures, good for SIMD
    Simple,
    /// Complex nested structures
    Complex,
    /// Deep nesting levels
    Nested,
    /// Large arrays of primitives
    LargeArray,
}

/// Performance metrics for adaptive backend selection
#[derive(Debug, Default)]
pub struct ParserMetrics {
    /// Throughput in MB/s
    pub throughput_mbps: f64,
    /// P99 latency in microseconds
    pub latency_p99_us: f64,
    /// Memory allocated in MB
    pub memory_allocated_mb: f64,
    /// Number of cache misses (estimated)
    pub cache_misses: u64,
    /// Total parsing operations
    pub operations_count: u64,
}

/// Hybrid parser that adaptively selects best backend
pub struct HybridParser {
    /// Current metrics for adaptive selection
    metrics: ParserMetrics,
    /// Fallback serde parser
    serde_parser: SerdeBackend,
    /// SIMD parser (when available)
    simd_parser: Option<SimdBackend>,
    /// Backend selection thresholds
    thresholds: BackendThresholds,
    /// Security validator for input validation
    validator: SecurityValidator,
}

/// Thresholds for backend selection
#[derive(Debug)]
pub struct BackendThresholds {
    /// Minimum size for SIMD optimization
    pub simd_min_size: usize,
    /// Maximum complexity for SIMD
    pub simd_max_complexity: u8,
    /// Throughput threshold for switching to SIMD
    pub simd_throughput_threshold: f64,
}

/// Serde-based backend for fallback and MVP
pub struct SerdeBackend;

/// SIMD backend placeholder (will be implemented later)
pub struct SimdBackend {
    _placeholder: (),
}

impl HybridParser {
    /// Create new hybrid parser with sensible defaults
    pub fn new() -> Self {
        Self {
            metrics: ParserMetrics::default(),
            serde_parser: SerdeBackend,
            simd_parser: None, // Will be enabled later
            thresholds: BackendThresholds::default(),
            validator: SecurityValidator::default(),
        }
    }

    /// Create new hybrid parser with custom security configuration
    pub fn with_security_config(security_config: SecurityConfig) -> Self {
        Self {
            metrics: ParserMetrics::default(),
            serde_parser: SerdeBackend,
            simd_parser: None,
            thresholds: BackendThresholds::default(),
            validator: SecurityValidator::new(security_config),
        }
    }

    /// Parse JSON with adaptive backend selection
    pub fn parse(&mut self, input: &[u8]) -> Result<JsonValue<'_>> {
        let start_time = Instant::now();
        
        let backend = self.select_backend(input);
        let result = match backend {
            ParserBackend::Serde => self.parse_with_serde(input),
            ParserBackend::Simd => {
                if let Some(ref simd) = self.simd_parser {
                    simd.parse(input)
                } else {
                    // Fallback to serde if SIMD not available
                    self.parse_with_serde(input)
                }
            }
            ParserBackend::Validator => {
                // Validation-only mode
                self.validate_only(input)?;
                Ok(JsonValue::Raw(input))
            }
        };

        // Update metrics
        let duration = start_time.elapsed();
        self.update_metrics(input.len(), duration, result.is_ok());
        
        result
    }

    /// Parse with semantic optimization hints
    pub fn parse_with_semantics(&mut self, input: &[u8], semantics: &SemanticMeta) -> Result<JsonValue<'_>> {
        let backend = self.select_backend_with_semantics(input, semantics);
        
        match backend {
            ParserBackend::Simd => {
                // Use semantic-aware SIMD parsing
                self.parse_semantic_simd(input, semantics)
            }
            _ => {
                // Fallback to regular parsing with post-processing
                let mut value = self.parse(input)?;
                self.apply_semantic_hints(&mut value, semantics);
                Ok(value)
            }
        }
    }

    /// Select optimal backend based on input characteristics
    fn select_backend(&self, input: &[u8]) -> ParserBackend {
        let complexity = self.estimate_complexity(input);
        let size = input.len();

        // During MVP phase, prefer serde for reliability
        if size < self.thresholds.simd_min_size {
            return ParserBackend::Serde;
        }

        match complexity {
            Complexity::Simple | Complexity::LargeArray if self.simd_parser.is_some() => {
                ParserBackend::Simd
            }
            _ => ParserBackend::Serde,
        }
    }

    /// Select backend with semantic hints
    fn select_backend_with_semantics(&self, input: &[u8], semantics: &SemanticMeta) -> ParserBackend {
        match semantics.processing_strategy() {
            ProcessingStrategy::Simd if self.simd_parser.is_some() => {
                ParserBackend::Simd
            }
            ProcessingStrategy::Generic => {
                self.select_backend(input)
            }
            _ => ParserBackend::Serde,
        }
    }

    /// Estimate input complexity for backend selection
    fn estimate_complexity(&self, input: &[u8]) -> Complexity {
        if input.len() < 100 {
            return Complexity::Simple;
        }

        let mut brace_count = 0;
        let mut bracket_count = 0;
        let mut max_depth = 0;
        let mut current_depth = 0;
        let mut number_count = 0;

        for &byte in input.iter().take(std::cmp::min(input.len(), 1024)) {
            match byte {
                b'{' => {
                    brace_count += 1;
                    current_depth += 1;
                    max_depth = max_depth.max(current_depth);
                }
                b'}' => {
                    current_depth = current_depth.saturating_sub(1);
                }
                b'[' => {
                    bracket_count += 1;
                    current_depth += 1;
                    max_depth = max_depth.max(current_depth);
                }
                b']' => {
                    current_depth = current_depth.saturating_sub(1);
                }
                b'0'..=b'9' => {
                    number_count += 1;
                }
                _ => {}
            }
        }

        // Heuristic classification
        if max_depth > 5 {
            Complexity::Nested
        } else if bracket_count > 0 && number_count > bracket_count * 10 {
            Complexity::LargeArray
        } else if brace_count + bracket_count < 5 {
            Complexity::Simple
        } else {
            Complexity::Complex
        }
    }

    /// Parse using serde backend
    fn parse_with_serde(&self, input: &[u8]) -> Result<JsonValue<'_>> {
        self.serde_parser.parse(input)
    }

    /// Parse with semantic SIMD optimization
    fn parse_semantic_simd(&self, input: &[u8], semantics: &SemanticMeta) -> Result<JsonValue<'_>> {
        match &semantics.semantic_type {
            SemanticType::NumericArray { .. } => {
                // TODO: Implement SIMD numeric array parsing
                self.parse_with_serde(input)
            }
            _ => {
                self.parse_with_serde(input)
            }
        }
    }

    /// Apply semantic hints to parsed value
    fn apply_semantic_hints(&self, _value: &mut JsonValue<'_>, _semantics: &SemanticMeta) {
        // TODO: Apply semantic transformations
    }

    /// Validation-only parsing
    fn validate_only(&self, input: &[u8]) -> Result<()> {
        // Security validation first
        self.validator.validate_input_size(input.len())?;
        
        // Quick JSON validation without full parsing
        serde_json::from_slice::<serde_json::Value>(input)
            .map(|_| ())
            .map_err(|e| Error::invalid_json(0, e.to_string()))
    }

    /// Update performance metrics
    fn update_metrics(&mut self, input_size: usize, duration: std::time::Duration, success: bool) {
        if !success {
            return;
        }

        let duration_us = duration.as_micros() as f64;
        let throughput = (input_size as f64) / (duration_us / 1_000_000.0) / 1_024_000.0; // MB/s

        // Simple exponential moving average
        let alpha = 0.1;
        self.metrics.throughput_mbps = alpha * throughput + (1.0 - alpha) * self.metrics.throughput_mbps;
        self.metrics.latency_p99_us = alpha * duration_us + (1.0 - alpha) * self.metrics.latency_p99_us;
        self.metrics.operations_count += 1;
    }

    /// Get current performance metrics
    pub fn metrics(&self) -> &ParserMetrics {
        &self.metrics
    }

    /// Check if SIMD should be preferred for given conditions
    pub fn should_use_simd(&self, input_size: usize) -> bool {
        input_size > self.thresholds.simd_min_size
            && self.metrics.throughput_mbps < self.thresholds.simd_throughput_threshold
            && self.simd_parser.is_some()
    }
}

impl SerdeBackend {
    /// Parse using serde_json
    pub fn parse(&self, input: &[u8]) -> Result<JsonValue<'_>> {
        let value: serde_json::Value = serde_json::from_slice(input)
            .map_err(|e| Error::invalid_json(0, e.to_string()))?;
        
        // Convert serde Value to our JsonValue
        // This is a simplified conversion - real implementation would be more complete
        match value {
            serde_json::Value::String(_) => Ok(JsonValue::Raw(input)), // Simplified
            serde_json::Value::Number(_) => Ok(JsonValue::Raw(input)),
            serde_json::Value::Bool(b) => Ok(JsonValue::Bool(b)),
            serde_json::Value::Null => Ok(JsonValue::Null),
            _ => Ok(JsonValue::Raw(input)), // Lazy parsing for complex types
        }
    }
}

impl SimdBackend {
    /// Parse using SIMD (placeholder)
    pub fn parse(&self, input: &[u8]) -> Result<JsonValue<'_>> {
        // TODO: Implement actual SIMD parsing
        Ok(JsonValue::Raw(input))
    }
}

impl Default for HybridParser {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for BackendThresholds {
    fn default() -> Self {
        Self {
            simd_min_size: 4096,        // 4KB minimum for SIMD
            simd_max_complexity: 3,     // Max nesting depth for SIMD
            simd_throughput_threshold: 1000.0, // 1 GB/s threshold
        }
    }
}

impl ParserMetrics {
    /// Check if current performance suggests SIMD would be beneficial
    pub fn should_use_simd(&self, input_size: usize) -> bool {
        input_size > 4096 
            && self.throughput_mbps < 1000.0
            && self.latency_p99_us > 100.0
    }

    /// Get performance summary
    pub fn summary(&self) -> String {
        format!(
            "Throughput: {:.1} MB/s, Latency P99: {:.1}μs, Operations: {}",
            self.throughput_mbps,
            self.latency_p99_us,
            self.operations_count
        )
    }
}

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

    #[test]
    fn test_hybrid_parser_creation() {
        let parser = HybridParser::new();
        assert_eq!(parser.metrics.operations_count, 0);
    }

    #[test]
    fn test_complexity_estimation() {
        let parser = HybridParser::new();
        
        // Simple JSON
        let simple = br#"{"key": "value"}"#;
        assert_eq!(parser.estimate_complexity(simple), Complexity::Simple);
        
        // Array
        let array = b"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]";
        let complexity = parser.estimate_complexity(array);
        assert!(matches!(complexity, Complexity::Simple | Complexity::LargeArray));
    }

    #[test]
    fn test_serde_backend() {
        let backend = SerdeBackend;
        let input = br#"{"hello": "world"}"#;
        let result = backend.parse(input);
        
        assert!(result.is_ok());
    }

    #[test]
    fn test_backend_selection() {
        let parser = HybridParser::new();
        
        // Small input should use serde
        let small_input = b"{}";
        assert_eq!(parser.select_backend(small_input), ParserBackend::Serde);
        
        // Large simple input should use serde (no SIMD available yet)
        let large_input = vec![b'{'; 5000];
        assert_eq!(parser.select_backend(&large_input), ParserBackend::Serde);
    }
}