delta-pack 0.2.1

Binary serialization with delta compression for real-time state synchronization
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use crate::rle::RleReader;
use crate::varint::{read_uvarint, read_varint};

/// Dictionary entry - either a buffer position or an owned string (for diff entries).
enum DictEntry {
    Pos(usize, usize),
    Owned(String),
}

/// Binary decoder with string dictionary and RLE bit unpacking.
pub struct Decoder<'a> {
    buffer: &'a [u8],
    pos: usize,
    dict: Vec<DictEntry>,
    rle: RleReader<'a>,
}

impl<'a> Decoder<'a> {
    pub fn new(buf: &'a [u8]) -> Self {
        Self {
            buffer: buf,
            pos: 0,
            dict: Vec::new(),
            rle: RleReader::new(buf),
        }
    }

    /// Decode using a thread-local decoder for optimal performance.
    /// This avoids allocation overhead by reusing the dictionary Vec across calls.
    #[inline]
    pub fn decode<F, R>(buf: &[u8], f: F) -> R
    where
        F: FnOnce(&mut Decoder) -> R,
    {
        use std::cell::RefCell;
        thread_local! {
            static DICT: RefCell<Vec<DictEntry>> = const { RefCell::new(Vec::new()) };
        }
        DICT.with(|dict| {
            let mut dict = dict.borrow_mut();
            dict.clear();
            let mut dec = Decoder {
                buffer: buf,
                pos: 0,
                dict: std::mem::take(&mut *dict),
                rle: RleReader::new(buf),
            };
            let result = f(&mut dec);
            *dict = dec.dict;
            result
        })
    }

    // Primitive methods

    #[inline]
    pub fn next_string(&mut self) -> String {
        let (len_or_idx, bytes_read) = read_varint(self.buffer, self.pos);
        self.pos += bytes_read;

        if len_or_idx == 0 {
            return String::new();
        }

        if len_or_idx > 0 {
            let len = len_or_idx as usize;
            let start = self.pos;
            self.pos += len;
            self.dict.push(DictEntry::Pos(start, len));
            return String::from(
                std::str::from_utf8(&self.buffer[start..start + len])
                    .expect("invalid UTF-8 in buffer"),
            );
        }

        // Negative = dictionary index
        match &self.dict[(-len_or_idx - 1) as usize] {
            DictEntry::Pos(start, len) => String::from(
                std::str::from_utf8(&self.buffer[*start..*start + *len])
                    .expect("invalid UTF-8 in buffer"),
            ),
            DictEntry::Owned(s) => s.clone(),
        }
    }

    #[inline]
    pub fn next_int(&mut self) -> i64 {
        let (val, bytes_read) = read_varint(self.buffer, self.pos);
        self.pos += bytes_read;
        val
    }

    #[inline]
    pub fn next_uint(&mut self) -> u64 {
        let (val, bytes_read) = read_uvarint(self.buffer, self.pos);
        self.pos += bytes_read;
        val
    }

    #[inline]
    pub fn next_bounded_int(&mut self, min: i64) -> i64 {
        self.next_uint() as i64 + min
    }

    #[inline]
    pub fn next_float(&mut self) -> f32 {
        let bytes: [u8; 4] = self.buffer[self.pos..self.pos + 4].try_into().unwrap();
        self.pos += 4;
        f32::from_le_bytes(bytes)
    }

    #[inline]
    pub fn next_float_quantized(&mut self, precision: f32) -> f32 {
        self.next_int() as f32 * precision
    }

    #[inline]
    pub fn next_boolean(&mut self) -> bool {
        self.rle.next_bit()
    }

    #[inline]
    pub fn next_enum(&mut self, num_bits: u8) -> u32 {
        self.rle.next_bits(num_bits)
    }

    // Diff methods (value-only - caller handles change bit for object fields)

    #[inline]
    pub fn next_string_diff(&mut self, a: &str) -> String {
        // Only add to dictionary if not already present (for decoder sync)
        let already_in_dict = self.dict.iter().any(|entry| match entry {
            DictEntry::Pos(start, len) => std::str::from_utf8(&self.buffer[*start..*start + *len])
                .map(|s| s == a)
                .unwrap_or(false),
            DictEntry::Owned(s) => s == a,
        });
        if !already_in_dict {
            self.dict.push(DictEntry::Owned(a.to_string()));
        }
        self.next_string()
    }

    #[inline]
    pub fn next_int_diff(&mut self, _a: i64) -> i64 {
        self.next_int()
    }

    #[inline]
    pub fn next_uint_diff(&mut self, _a: u64) -> u64 {
        self.next_uint()
    }

    #[inline]
    pub fn next_bounded_int_diff(&mut self, _a: i64, min: i64) -> i64 {
        self.next_bounded_int(min)
    }

    #[inline]
    pub fn next_float_diff(&mut self, _a: f32) -> f32 {
        self.next_float()
    }

    #[inline]
    pub fn next_float_quantized_diff(&mut self, _a: f32, precision: f32) -> f32 {
        self.next_float_quantized(precision)
    }

    #[inline]
    pub fn next_boolean_diff(&mut self, a: bool) -> bool {
        // Boolean diff is special - change bit IS the diff
        a ^ self.next_boolean()
    }

    #[inline]
    pub fn next_enum_diff(&mut self, _a: u32, num_bits: u8) -> u32 {
        self.next_enum(num_bits)
    }

    // Object diff helper (read change bit, decode if changed)

    #[inline]
    pub fn next_object_diff<T, F>(&mut self, a: &T, decode_diff: F) -> T
    where
        T: Clone,
        F: FnOnce(&mut Self) -> T,
    {
        if self.next_boolean() {
            decode_diff(self)
        } else {
            a.clone()
        }
    }

    // Field diff helper (read change bit, decode if changed)

    #[inline]
    pub fn next_field_diff<T, F>(&mut self, a: &T, decode_diff: F) -> T
    where
        T: Clone,
        F: FnOnce(&mut Self, &T) -> T,
    {
        if self.next_boolean() {
            decode_diff(self, a)
        } else {
            a.clone()
        }
    }

    // Array helpers

    /// Decode an array by reading length followed by each element.
    #[inline]
    pub fn next_array<T, F>(&mut self, mut inner_read: F) -> Vec<T>
    where
        F: FnMut(&mut Self) -> T,
    {
        let len = self.next_uint() as usize;
        let mut arr = Vec::with_capacity(len);
        for _ in 0..len {
            arr.push(inner_read(self));
        }
        arr
    }

    /// Decode array diff, using sparse format with index-based updates.
    /// Caller handles change bit.
    #[inline]
    pub fn next_array_diff<T, F, FD>(
        &mut self,
        a: &[T],
        mut inner_read: F,
        mut inner_diff: FD,
    ) -> Vec<T>
    where
        T: Clone,
        F: FnMut(&mut Self) -> T,
        FD: FnMut(&mut Self, &T) -> T,
    {
        let new_len = self.next_uint() as usize;

        // Start with copy of old array (truncated to new length)
        let mut arr: Vec<T> = a.iter().take(new_len.min(a.len())).cloned().collect();

        // Apply updates (sparse)
        let num_updates = self.next_uint() as usize;
        for _ in 0..num_updates {
            let idx = self.next_uint() as usize;
            arr[idx] = inner_diff(self, &a[idx]);
        }

        // Read additions
        for _ in a.len()..new_len {
            arr.push(inner_read(self));
        }

        arr
    }

    // Optional helpers

    #[inline]
    pub fn next_optional<T, F>(&mut self, mut inner_read: F) -> Option<T>
    where
        F: FnMut(&mut Self) -> T,
    {
        self.next_boolean().then(|| inner_read(self))
    }

    /// Decode optional diff, matching TS/C# format.
    /// Optimization: if a was None, we know b must be Some (else unchanged).
    /// So no present bit in None→Some case.
    #[inline]
    pub fn next_optional_diff<T, F, FD>(
        &mut self,
        a: &Option<T>,
        mut inner_read: F,
        mut inner_diff: FD,
    ) -> Option<T>
    where
        T: Clone,
        F: FnMut(&mut Self) -> T,
        FD: FnMut(&mut Self, &T) -> T,
    {
        match a {
            None => {
                // None → Some (guaranteed Some by caller)
                Some(inner_read(self))
            }
            Some(av) => {
                if self.next_boolean() {
                    Some(inner_diff(self, av)) // Some → Some
                } else {
                    None // Some → None
                }
            }
        }
    }

    // Record (map) helpers

    /// Decode a record (map) by reading length followed by key-value pairs.
    #[inline]
    pub fn next_record<K, V, FK, FV>(
        &mut self,
        mut key_read: FK,
        mut val_read: FV,
    ) -> indexmap::IndexMap<K, V>
    where
        K: Eq + std::hash::Hash,
        FK: FnMut(&mut Self) -> K,
        FV: FnMut(&mut Self) -> V,
    {
        let len = self.next_uint() as usize;
        let mut map = indexmap::IndexMap::with_capacity(len);
        for _ in 0..len {
            let k = key_read(self);
            let v = val_read(self);
            map.insert(k, v);
        }
        map
    }

    /// Decode record diff, matching TS/C# format.
    /// Caller handles change bit.
    /// Format: if a.len > 0: deletions, updates; then additions
    #[inline]
    pub fn next_record_diff<K, V, FK, FV, FVD>(
        &mut self,
        a: &indexmap::IndexMap<K, V>,
        mut key_read: FK,
        mut val_read: FV,
        mut val_diff: FVD,
    ) -> indexmap::IndexMap<K, V>
    where
        K: Clone + Eq + std::hash::Hash,
        V: Clone,
        FK: FnMut(&mut Self) -> K,
        FV: FnMut(&mut Self) -> V,
        FVD: FnMut(&mut Self, &V) -> V,
    {
        let mut result = a.clone();

        // Read deletions and updates (only if a was non-empty)
        if !a.is_empty() {
            let num_deletions = self.next_uint() as usize;
            for _ in 0..num_deletions {
                let idx = self.next_uint() as usize;
                let key = a.get_index(idx).unwrap().0.clone();
                result.shift_remove(&key);
            }
            let num_updates = self.next_uint() as usize;
            for _ in 0..num_updates {
                let idx = self.next_uint() as usize;
                let key = a.get_index(idx).unwrap().0.clone();
                let new_val = val_diff(self, result.get(&key).unwrap());
                result.insert(key, new_val);
            }
        }

        // Read additions
        let num_additions = self.next_uint() as usize;
        for _ in 0..num_additions {
            let k = key_read(self);
            let v = val_read(self);
            result.insert(k, v);
        }

        result
    }
}

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

    #[test]
    fn test_encode_decode_string() {
        let mut encoder = Encoder::new();
        encoder.push_string("hello");
        encoder.push_string("world");
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert_eq!(decoder.next_string(), "hello");
        assert_eq!(decoder.next_string(), "world");
    }

    #[test]
    fn test_encode_decode_string_dictionary() {
        let mut encoder = Encoder::new();
        encoder.push_string("hello");
        encoder.push_string("hello"); // Uses dictionary
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert_eq!(decoder.next_string(), "hello");
        assert_eq!(decoder.next_string(), "hello");
    }

    #[test]
    fn test_encode_decode_int() {
        let mut encoder = Encoder::new();
        encoder.push_int(42);
        encoder.push_int(-100);
        encoder.push_int(0);
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert_eq!(decoder.next_int(), 42);
        assert_eq!(decoder.next_int(), -100);
        assert_eq!(decoder.next_int(), 0);
    }

    #[test]
    fn test_encode_decode_uint() {
        let mut encoder = Encoder::new();
        encoder.push_uint(0);
        encoder.push_uint(127);
        encoder.push_uint(128);
        encoder.push_uint(16383);
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert_eq!(decoder.next_uint(), 0);
        assert_eq!(decoder.next_uint(), 127);
        assert_eq!(decoder.next_uint(), 128);
        assert_eq!(decoder.next_uint(), 16383);
    }

    #[test]
    fn test_encode_decode_float() {
        let mut encoder = Encoder::new();
        encoder.push_float(3.14);
        encoder.push_float(-2.5);
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert!((decoder.next_float() - 3.14).abs() < 0.001);
        assert!((decoder.next_float() - (-2.5)).abs() < 0.001);
    }

    #[test]
    fn test_encode_decode_float_quantized() {
        let mut encoder = Encoder::new();
        encoder.push_float_quantized(3.14159, 0.01);
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        let val = decoder.next_float_quantized(0.01);
        assert!((val - 3.14).abs() < 0.01);
    }

    #[test]
    fn test_encode_decode_boolean() {
        let mut encoder = Encoder::new();
        encoder.push_boolean(true);
        encoder.push_boolean(false);
        encoder.push_boolean(true);
        encoder.push_boolean(true);
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert!(decoder.next_boolean());
        assert!(!decoder.next_boolean());
        assert!(decoder.next_boolean());
        assert!(decoder.next_boolean());
    }

    #[test]
    fn test_encode_decode_mixed() {
        let mut encoder = Encoder::new();
        encoder.push_string("test");
        encoder.push_int(42);
        encoder.push_boolean(true);
        encoder.push_float(3.14);
        encoder.push_boolean(false);
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert_eq!(decoder.next_string(), "test");
        assert_eq!(decoder.next_int(), 42);
        assert!(decoder.next_boolean());
        assert!((decoder.next_float() - 3.14).abs() < 0.001);
        assert!(!decoder.next_boolean());
    }

    #[test]
    fn test_diff_string() {
        let mut encoder = Encoder::new();
        encoder.push_string_diff("hello", "hello"); // unchanged
        encoder.push_string_diff("hello", "world"); // changed
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert_eq!(decoder.next_string_diff("hello"), "hello");
        assert_eq!(decoder.next_string_diff("hello"), "world");
    }

    #[test]
    fn test_diff_int() {
        let mut encoder = Encoder::new();
        encoder.push_int_diff(10, 10); // unchanged
        encoder.push_int_diff(10, 20); // changed
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert_eq!(decoder.next_int_diff(10), 10);
        assert_eq!(decoder.next_int_diff(10), 20);
    }

    #[test]
    fn test_array_encode_decode() {
        let mut encoder = Encoder::new();
        let arr = vec![1i64, 2, 3, 4, 5];
        encoder.push_array(&arr, |enc, &x| enc.push_int(x));
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        let result: Vec<i64> = decoder.next_array(|dec| dec.next_int());
        assert_eq!(result, arr);
    }

    #[test]
    fn test_array_diff() {
        let a = vec![1i64, 2, 3];
        let b = vec![1i64, 5, 3, 4]; // changed element and added element

        let mut encoder = Encoder::new();
        encoder.push_array_diff(
            &a,
            &b,
            |x, y| x == y,
            |enc: &mut Encoder, &x| enc.push_int(x),
            |enc: &mut Encoder, _, &x| enc.push_int(x), // diff takes (a, b)
        );
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        let result: Vec<i64> = decoder.next_array_diff(
            &a,
            |dec| dec.next_int(),
            |dec, _| dec.next_int(), // diff returns new value
        );
        assert_eq!(result, b);
    }

    #[test]
    fn test_optional_encode_decode() {
        let mut encoder = Encoder::new();
        encoder.push_optional(&Some(42i64), |enc, &x| enc.push_int(x));
        encoder.push_optional(&None::<i64>, |enc, &x| enc.push_int(x));
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        assert_eq!(decoder.next_optional(|dec| dec.next_int()), Some(42));
        assert_eq!(decoder.next_optional(|dec| dec.next_int()), None);
    }

    #[test]
    fn test_optional_diff() {
        // None to Some
        let mut encoder = Encoder::new();
        encoder.push_optional_diff(
            &None::<i64>,
            &Some(42i64),
            |enc: &mut Encoder, &x| enc.push_int(x),
            |enc: &mut Encoder, _, &x| enc.push_int(x), // diff takes (a, b)
        );
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        let result =
            decoder.next_optional_diff(&None, |dec| dec.next_int(), |dec, _| dec.next_int());
        assert_eq!(result, Some(42));
    }

    #[test]
    fn test_record_encode_decode() {
        let mut encoder = Encoder::new();
        let mut map = indexmap::IndexMap::new();
        map.insert("a".to_string(), 1i64);
        map.insert("b".to_string(), 2i64);
        encoder.push_record(&map, |enc, k| enc.push_string(k), |enc, &v| enc.push_int(v));
        let buf = encoder.finish();

        let mut decoder = Decoder::new(&buf);
        let result: indexmap::IndexMap<String, i64> =
            decoder.next_record(|dec| dec.next_string(), |dec| dec.next_int());
        assert_eq!(result, map);
    }
}