dx-serializer 0.1.0

A token-efficient serialization format for LLM context windows with high-performance binary encoding
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
//! Optimized RKYV wrapper using DX features
//!
//! This module wraps RKYV with intelligent optimizations that automatically
//! choose the best strategy based on data size and workload:
//! - Small data (<1KB): Direct RKYV (no overhead)
//! - Medium batches (10-100): Pre-allocation (8-15% faster)
//! - Large files (>1KB): Shared file I/O backend
//! - Huge batches (>10k): Parallel processing
//! - Network transfer: LZ4 compression (70% smaller)
//!
//! The binary format is still RKYV - we just make it faster!

use rkyv::util::AlignedVec;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
use std::path::Path;

use crate::machine::{AsyncFileIO, blocking::BlockingIO};

#[cfg(feature = "parallel")]
use rayon::prelude::*;

// Thresholds for optimization strategies (tuned from benchmarks)
const SMALL_FILE_THRESHOLD: usize = 1024; // 1KB - use std::fs below this
const PARALLEL_THRESHOLD: usize = 10_000; // Use parallel above this

fn deserialize_checked<T>(bytes: &[u8]) -> Result<T, std::io::Error>
where
    T: Archive,
    T::Archived: for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, rkyv::rancor::Error>>
        + RkyvDeserialize<T, rkyv::rancor::Strategy<rkyv::de::Pool, rkyv::rancor::Error>>,
{
    let archived = rkyv::access::<T::Archived, rkyv::rancor::Error>(bytes)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
    let mut deserializer = rkyv::de::Pool::new();
    archived
        .deserialize(rkyv::rancor::Strategy::wrap(&mut deserializer))
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))
}

/// Optimized RKYV serializer with intelligent strategy selection
pub struct OptimizedRkyv {
    io: BlockingIO,
}

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

impl OptimizedRkyv {
    /// Create a new optimized RKYV serializer
    pub fn new() -> Self {
        Self {
            io: BlockingIO::new(),
        }
    }

    /// Serialize to file with intelligent I/O strategy
    ///
    /// - Small files (<1KB): Uses std::fs (faster, less overhead)
    /// - Large files (≥1KB): Uses the configured file I/O backend
    pub fn serialize_to_file<T>(&self, value: &T, path: &Path) -> Result<(), std::io::Error>
    where
        T: for<'a> RkyvSerialize<
            rkyv::rancor::Strategy<
                rkyv::ser::Serializer<
                    AlignedVec,
                    rkyv::ser::allocator::ArenaHandle<'a>,
                    rkyv::ser::sharing::Share,
                >,
                rkyv::rancor::Error,
            >,
        >,
    {
        let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(value)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

        // Use std::fs for small files (less overhead)
        if bytes.len() < SMALL_FILE_THRESHOLD {
            if let Some(parent) = path.parent() {
                if !parent.as_os_str().is_empty() && !parent.exists() {
                    std::fs::create_dir_all(parent)?;
                }
            }
            std::fs::write(path, &bytes)
        } else {
            // Use platform I/O for large files
            self.io.write_sync(path, &bytes)
        }
    }

    /// Deserialize from file with intelligent I/O strategy
    pub fn deserialize_from_file<T>(&self, path: &Path) -> Result<T, std::io::Error>
    where
        T: Archive,
        T::Archived: for<'a> rkyv::bytecheck::CheckBytes<
                rkyv::api::high::HighValidator<'a, rkyv::rancor::Error>,
            > + RkyvDeserialize<T, rkyv::rancor::Strategy<rkyv::de::Pool, rkyv::rancor::Error>>,
    {
        // Check file size to choose strategy
        let metadata = std::fs::metadata(path)?;
        let file_size = metadata.len() as usize;

        let bytes = if file_size < SMALL_FILE_THRESHOLD {
            // Small file: use std::fs
            std::fs::read(path)?
        } else {
            // Large file: use the configured file I/O backend.
            self.io.read_sync(path)?
        };

        deserialize_checked::<T>(&bytes)
    }

    /// Batch serialize with intelligent strategy selection
    ///
    /// - Small batches (<10k): Sequential with pre-allocation (8-15% faster)
    /// - Large batches (≥10k): Parallel processing (scales with cores)
    pub fn serialize_batch_smart<T>(&self, items: &[T]) -> Result<Vec<AlignedVec>, std::io::Error>
    where
        T: for<'a> RkyvSerialize<
                rkyv::rancor::Strategy<
                    rkyv::ser::Serializer<
                        AlignedVec,
                        rkyv::ser::allocator::ArenaHandle<'a>,
                        rkyv::ser::sharing::Share,
                    >,
                    rkyv::rancor::Error,
                >,
            > + Sync,
    {
        if items.is_empty() {
            return Ok(Vec::new());
        }

        // Small-medium batches: use pre-allocation (proven 8-15% faster)
        if items.len() < PARALLEL_THRESHOLD {
            let mut results = Vec::with_capacity(items.len());
            for item in items {
                results.push(
                    rkyv::to_bytes::<rkyv::rancor::Error>(item).map_err(|e| {
                        std::io::Error::new(std::io::ErrorKind::Other, e.to_string())
                    })?,
                );
            }
            return Ok(results);
        }

        // Large batches: use parallel processing
        #[cfg(feature = "parallel")]
        {
            items
                .par_iter()
                .map(|item| {
                    rkyv::to_bytes::<rkyv::rancor::Error>(item)
                        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
                })
                .collect()
        }

        #[cfg(not(feature = "parallel"))]
        {
            // Fallback to sequential if parallel feature not enabled
            let mut results = Vec::with_capacity(items.len());
            for item in items {
                results.push(
                    rkyv::to_bytes::<rkyv::rancor::Error>(item).map_err(|e| {
                        std::io::Error::new(std::io::ErrorKind::Other, e.to_string())
                    })?,
                );
            }
            Ok(results)
        }
    }

    /// Batch file operations with platform-optimized I/O
    pub fn serialize_batch_to_files<T>(
        &self,
        items: &[(T, &Path)],
    ) -> Result<Vec<std::io::Result<()>>, std::io::Error>
    where
        T: for<'a> RkyvSerialize<
            rkyv::rancor::Strategy<
                rkyv::ser::Serializer<
                    AlignedVec,
                    rkyv::ser::allocator::ArenaHandle<'a>,
                    rkyv::ser::sharing::Share,
                >,
                rkyv::rancor::Error,
            >,
        >,
    {
        // Serialize all items first
        let serialized: Vec<_> = items
            .iter()
            .map(|(item, _)| {
                rkyv::to_bytes::<rkyv::rancor::Error>(item)
                    .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
            })
            .collect::<Result<Vec<_>, _>>()?;

        // Batch write with optimized I/O
        let files: Vec<_> = items
            .iter()
            .zip(serialized.iter())
            .map(|((_, path), bytes)| (*path, bytes.as_ref()))
            .collect();

        self.io.write_batch_sync(&files)
    }

    /// Batch read with platform-optimized I/O
    pub fn deserialize_batch_from_files<T>(
        &self,
        paths: &[&Path],
    ) -> Result<Vec<Result<T, std::io::Error>>, std::io::Error>
    where
        T: Archive,
        T::Archived: for<'a> rkyv::bytecheck::CheckBytes<
                rkyv::api::high::HighValidator<'a, rkyv::rancor::Error>,
            > + RkyvDeserialize<T, rkyv::rancor::Strategy<rkyv::de::Pool, rkyv::rancor::Error>>,
    {
        let read_results = self.io.read_batch_sync(paths)?;
        Ok(read_results
            .into_iter()
            .map(|result| result.and_then(|bytes| deserialize_checked::<T>(&bytes)))
            .collect())
    }

    /// Get the I/O backend name
    pub fn backend_name(&self) -> &'static str {
        self.io.backend_name()
    }
}

/// Arena-based batch serializer for zero-allocation batch operations
///
/// Best for: Repeated batch operations where you can reuse the arena
#[cfg(feature = "arena")]
pub struct ArenaRkyv {
    arena: crate::machine::arena::DxArena,
    capacity: usize,
}

#[cfg(feature = "arena")]
impl ArenaRkyv {
    /// Create a new arena-based RKYV serializer
    pub fn new() -> Self {
        let capacity = 1024 * 1024; // 1MB default
        Self {
            arena: crate::machine::arena::DxArena::new(capacity),
            capacity,
        }
    }

    /// Create with specific capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            arena: crate::machine::arena::DxArena::new(capacity),
            capacity,
        }
    }

    /// Serialize a batch of items using arena allocation
    pub fn serialize_batch<T>(&mut self, items: &[T]) -> Result<Vec<AlignedVec>, std::io::Error>
    where
        T: for<'a> RkyvSerialize<
            rkyv::rancor::Strategy<
                rkyv::ser::Serializer<
                    AlignedVec,
                    rkyv::ser::allocator::ArenaHandle<'a>,
                    rkyv::ser::sharing::Share,
                >,
                rkyv::rancor::Error,
            >,
        >,
    {
        let mut results = Vec::with_capacity(items.len());
        for item in items {
            results.push(
                rkyv::to_bytes::<rkyv::rancor::Error>(item)
                    .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?,
            );
        }
        Ok(results)
    }

    /// Reset the arena for reuse
    pub fn reset(&mut self) {
        self.arena = crate::machine::arena::DxArena::new(self.capacity);
    }
}

#[cfg(feature = "arena")]
impl Default for ArenaRkyv {
    fn default() -> Self {
        Self::new()
    }
}

/// Compressed RKYV with LZ4
///
/// Best for: Network transfer or storage (70% size reduction)
/// Overhead: ~212ns per operation
/// Use when: Data size > 100 bytes AND (network transfer OR storage optimization needed)
#[cfg(feature = "compression")]
pub struct CompressedRkyv {
    level: crate::machine::compress::CompressionLevel,
}

#[cfg(feature = "compression")]
impl CompressedRkyv {
    /// Create a new compressed RKYV serializer
    pub fn new(level: crate::machine::compress::CompressionLevel) -> Self {
        Self { level }
    }

    /// Serialize and compress (best for network transfer)
    ///
    /// Always emits the compression wire format expected by
    /// [`CompressedRkyv::deserialize_compressed`].
    pub fn serialize_compressed<T>(&mut self, value: &T) -> Result<Vec<u8>, std::io::Error>
    where
        T: for<'a> RkyvSerialize<
            rkyv::rancor::Strategy<
                rkyv::ser::Serializer<
                    AlignedVec,
                    rkyv::ser::allocator::ArenaHandle<'a>,
                    rkyv::ser::sharing::Share,
                >,
                rkyv::rancor::Error,
            >,
        >,
    {
        let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(value)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

        let compressed = crate::machine::compress::DxCompressed::compress_level(&bytes, self.level);
        Ok(compressed.to_wire())
    }

    /// Decompress and deserialize
    pub fn deserialize_compressed<T>(&mut self, compressed: &[u8]) -> Result<T, std::io::Error>
    where
        T: Archive,
        T::Archived: for<'a> rkyv::bytecheck::CheckBytes<
                rkyv::api::high::HighValidator<'a, rkyv::rancor::Error>,
            > + RkyvDeserialize<T, rkyv::rancor::Strategy<rkyv::de::Pool, rkyv::rancor::Error>>,
    {
        // Decompress using DxCompressed
        let mut dx_compressed = crate::machine::compress::DxCompressed::from_wire(compressed)
            .map_err(|e| {
                std::io::Error::new(std::io::ErrorKind::InvalidData, format!("{:?}", e))
            })?;
        let bytes = dx_compressed.decompress().map_err(|e| {
            std::io::Error::new(std::io::ErrorKind::InvalidData, format!("{:?}", e))
        })?;

        deserialize_checked::<T>(bytes)
    }
}

/// Memory-mapped RKYV for large files
///
/// Best for: Very large files (>10MB) with random access patterns
#[cfg(feature = "mmap")]
pub struct MmapRkyv {
    _phantom: std::marker::PhantomData<()>,
}

#[cfg(feature = "mmap")]
impl MmapRkyv {
    /// Create a new memory-mapped RKYV accessor
    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }

    /// Open a memory-mapped file and access archived data
    ///
    /// Best for files >10MB where you need random access without loading entire file
    pub fn open<T>(&self, path: &Path) -> Result<crate::machine::mmap::DxMmap, std::io::Error>
    where
        T: Archive,
    {
        crate::machine::mmap::DxMmap::open(path)
    }
}

#[cfg(feature = "mmap")]
impl Default for MmapRkyv {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rkyv::{Archive, Deserialize, Serialize};
    use tempfile::TempDir;

    #[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
    struct TestData {
        id: u64,
        name: String,
    }

    #[test]
    fn test_optimized_file_io() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.rkyv");
        let opt = OptimizedRkyv::new();

        let data = TestData {
            id: 42,
            name: "test".to_string(),
        };

        opt.serialize_to_file(&data, &path).unwrap();
        let loaded: TestData = opt.deserialize_from_file(&path).unwrap();

        assert_eq!(data, loaded);
    }

    #[test]
    fn test_backend_name() {
        let opt = OptimizedRkyv::new();
        let backend = opt.backend_name();

        assert_eq!(backend, "blocking");
    }

    #[cfg(feature = "compression")]
    #[test]
    fn test_compressed_rkyv() {
        use crate::machine::compress::CompressionLevel;

        let mut comp = CompressedRkyv::new(CompressionLevel::Fast);
        let data = TestData {
            id: 42,
            name: "test".to_string(),
        };

        let compressed = comp.serialize_compressed(&data).unwrap();
        let decompressed: TestData = comp.deserialize_compressed(&compressed).unwrap();

        assert_eq!(data, decompressed);
    }

    #[cfg(feature = "arena")]
    #[test]
    fn test_arena_rkyv() {
        let mut arena = ArenaRkyv::new();
        let items = vec![
            TestData {
                id: 1,
                name: "one".to_string(),
            },
            TestData {
                id: 2,
                name: "two".to_string(),
            },
            TestData {
                id: 3,
                name: "three".to_string(),
            },
        ];

        let serialized = arena.serialize_batch(&items).unwrap();
        assert_eq!(serialized.len(), 3);
    }
}