zipora 3.1.2

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use std::collections::HashMap;
use zipora::{
    BitVector, BlobStore, DictionaryBuilder, DictionaryCompressor, EntropyStats, FastStr, FastVec,
    HuffmanBlobStore, HuffmanEncoder, HuffmanTree, MemoryBlobStore, RankSelect256,
    Rans64Encoder, ZiporaHashMap,
};
use zipora::succinct::rank_select::RankSelectOps;
use zipora::entropy::ParallelX1;

#[cfg(feature = "mmap")]
use zipora::{DataInput, DataOutput, MemoryMappedInput, MemoryMappedOutput};

use std::fs::File;
use std::io::Write;
use tempfile::NamedTempFile;

fn benchmark_fast_vec_push(c: &mut Criterion) {
    c.bench_function("FastVec push 100k elements", |b| {
        b.iter(|| {
            let mut vec = FastVec::new();
            for i in 0..100_000 {
                vec.push(black_box(i)).unwrap();
            }
            vec
        });
    });
}

fn benchmark_fast_vec_vs_vec(c: &mut Criterion) {
    let mut group = c.benchmark_group("Vector Comparison");

    group.bench_function("FastVec", |b| {
        b.iter(|| {
            let mut vec = FastVec::new();
            for i in 0..10_000 {
                vec.push(black_box(i)).unwrap();
            }
            vec
        });
    });

    group.bench_function("std::Vec", |b| {
        b.iter(|| {
            let mut vec = Vec::new();
            for i in 0..10_000 {
                vec.push(black_box(i));
            }
            vec
        });
    });

    group.finish();
}

fn benchmark_fast_str_hash(c: &mut Criterion) {
    let data = "The quick brown fox jumps over the lazy dog".repeat(100);
    let fast_str = FastStr::from_string(&data);

    c.bench_function("FastStr hash", |b| {
        b.iter(|| black_box(fast_str.hash_fast()));
    });
}

fn benchmark_fast_str_operations(c: &mut Criterion) {
    let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";
    let fast_str = FastStr::from_string(text);
    let needle = FastStr::from_string("dolor");

    let mut group = c.benchmark_group("FastStr Operations");

    group.bench_function("find", |b| {
        b.iter(|| black_box(fast_str.find(needle)));
    });

    group.bench_function("starts_with", |b| {
        let prefix = FastStr::from_string("Lorem");
        b.iter(|| black_box(fast_str.starts_with(prefix)));
    });

    group.bench_function("substring", |b| {
        b.iter(|| black_box(fast_str.substring(6, 5)));
    });

    group.finish();
}

fn benchmark_succinct_data_structures(c: &mut Criterion) {
    let mut group = c.benchmark_group("Succinct Data Structures");

    // Create a large bit vector with known pattern
    let mut bv = BitVector::new();
    for i in 0..100_000 {
        bv.push(i % 7 == 0).unwrap(); // Every 7th bit is set
    }

    group.bench_function("BitVector creation", |b| {
        b.iter(|| {
            let mut bv = BitVector::new();
            for i in 0..10_000 {
                bv.push(black_box(i % 7 == 0)).unwrap();
            }
            bv
        });
    });

    group.bench_function("RankSelect256 construction", |b| {
        b.iter(|| {
            let rs = RankSelect256::new(black_box(bv.clone())).unwrap();
            rs
        });
    });

    let rs = RankSelect256::new(bv.clone()).unwrap();

    group.bench_function("rank1 operation", |b| {
        b.iter(|| rs.rank1(black_box(50_000)));
    });

    group.bench_function("select1 operation", |b| {
        b.iter(|| rs.select1(black_box(5_000)).unwrap_or(0));
    });

    group.finish();
}

fn benchmark_hash_map_comparison(c: &mut Criterion) {
    let mut group = c.benchmark_group("HashMap Comparison");

    // Benchmark insertion performance
    group.bench_function("ZiporaHashMap insert 10k", |b| {
        b.iter(|| {
            let mut map: ZiporaHashMap<String, i32> = ZiporaHashMap::new().unwrap();
            for i in 0..10_000 {
                let key = format!("key_{}", i);
                map.insert(black_box(key), black_box(i)).unwrap();
            }
            map
        });
    });

    group.bench_function("std::HashMap insert 10k", |b| {
        b.iter(|| {
            let mut map = HashMap::new();
            for i in 0..10_000 {
                let key = format!("key_{}", i);
                map.insert(black_box(key), black_box(i));
            }
            map
        });
    });

    // Create pre-populated maps for lookup benchmarks
    let mut gold_map: ZiporaHashMap<String, i32> = ZiporaHashMap::new().unwrap();
    let mut std_map = HashMap::new();

    for i in 0..10_000 {
        let key = format!("key_{}", i);
        gold_map.insert(key.clone(), i).unwrap();
        std_map.insert(key, i);
    }

    // Benchmark lookup performance
    group.bench_function("ZiporaHashMap lookup", |b| {
        b.iter(|| {
            for i in 0..1_000 {
                let key = format!("key_{}", black_box(i));
                black_box(gold_map.get(&key));
            }
        });
    });

    group.bench_function("std::HashMap lookup", |b| {
        b.iter(|| {
            for i in 0..1_000 {
                let key = format!("key_{}", black_box(i));
                black_box(std_map.get(&key));
            }
        });
    });

    group.finish();
}

/// Benchmark memory mapping performance (Phase 2.5.4)
#[cfg(feature = "mmap")]
fn benchmark_memory_mapping(c: &mut Criterion) {
    let mut group = c.benchmark_group("Memory Mapping Performance");

    // Create test data files of different sizes
    let small_data = vec![42u8; 1024]; // 1KB
    let medium_data = vec![42u8; 1024 * 1024]; // 1MB
    let large_data = vec![42u8; 10 * 1024 * 1024]; // 10MB

    // Benchmark memory mapped input vs regular file I/O
    for (size_name, data) in [
        ("1KB", &small_data),
        ("1MB", &medium_data),
        ("10MB", &large_data),
    ]
    .iter()
    {
        // Create temporary file
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(data).unwrap();
        temp_file.flush().unwrap();
        let file_path = temp_file.path();

        // Benchmark memory mapped reading
        group.bench_function(&format!("MemoryMappedInput read {}", size_name), |b| {
            b.iter(|| {
                let file = File::open(file_path).unwrap();
                let mut mmap_input = MemoryMappedInput::new(file).unwrap();
                let mut buffer = vec![0u8; data.len()];
                let mut pos = 0;
                while pos < data.len() {
                    let chunk = std::cmp::min(1024, data.len() - pos);
                    mmap_input
                        .read_bytes(&mut buffer[pos..pos + chunk])
                        .unwrap();
                    pos += chunk;
                }
                black_box(buffer)
            });
        });

        // Benchmark regular file I/O for comparison
        group.bench_function(&format!("Regular File read {}", size_name), |b| {
            b.iter(|| {
                use std::io::Read;
                let mut file = File::open(file_path).unwrap();
                let mut buffer = vec![0u8; data.len()];
                file.read_exact(&mut buffer).unwrap();
                black_box(buffer)
            });
        });
    }

    // Benchmark memory mapped output
    group.bench_function("MemoryMappedOutput write 1MB", |b| {
        let data = vec![42u8; 1024 * 1024];
        b.iter(|| {
            let temp_file = NamedTempFile::new().unwrap();
            let mut mmap_output = MemoryMappedOutput::create(temp_file.path(), data.len()).unwrap();

            for chunk in data.chunks(1024) {
                mmap_output.write_bytes(chunk).unwrap();
            }

            black_box(mmap_output)
        });
    });

    group.finish();
}

/// Fallback memory mapping benchmark for when mmap feature is disabled
#[cfg(not(feature = "mmap"))]
fn benchmark_memory_mapping(_c: &mut Criterion) {
    // No-op when mmap feature is disabled
}

/// Benchmark entropy coding performance (Phase 3.6)
fn benchmark_entropy_coding(c: &mut Criterion) {
    let mut group = c.benchmark_group("Entropy Coding Performance");

    // Test data with different entropy characteristics
    let random_data = (0..10000).map(|i| (i * 17 + 13) as u8).collect::<Vec<_>>();
    let biased_data = "hello world! ".repeat(1000).into_bytes();
    let repeated_data = "the quick brown fox jumps over the lazy dog. "
        .repeat(200)
        .into_bytes();

    let test_datasets = [
        ("Random", &random_data),
        ("Biased", &biased_data),
        ("Repeated", &repeated_data),
    ];

    for (name, data) in test_datasets.iter() {
        // Benchmark entropy calculation
        group.bench_function(&format!("Entropy calculation {}", name), |b| {
            b.iter(|| {
                let entropy = EntropyStats::calculate_entropy(black_box(data));
                black_box(entropy)
            });
        });

        // Benchmark Huffman encoding
        group.bench_function(&format!("Huffman tree construction {}", name), |b| {
            b.iter(|| {
                let tree = HuffmanTree::from_data(black_box(data)).unwrap();
                black_box(tree)
            });
        });

        group.bench_function(&format!("Huffman encoding {}", name), |b| {
            let encoder = HuffmanEncoder::new(data).unwrap();
            b.iter(|| {
                let encoded = encoder.encode(black_box(data)).unwrap();
                black_box(encoded)
            });
        });

        // Benchmark rANS encoding
        group.bench_function(&format!("rANS encoder creation {}", name), |b| {
            b.iter(|| {
                let mut frequencies = [0u32; 256];
                for &byte in data.iter() {
                    frequencies[byte as usize] += 1;
                }
                let encoder: Rans64Encoder<ParallelX1> = Rans64Encoder::new(black_box(&frequencies)).unwrap();
                black_box(encoder)
            });
        });

        // Benchmark dictionary compression
        group.bench_function(&format!("Dictionary construction {}", name), |b| {
            b.iter(|| {
                let builder = DictionaryBuilder::new()
                    .min_match_length(3)
                    .max_match_length(20)
                    .max_entries(100);
                let dictionary = builder.build(black_box(data));
                black_box(dictionary)
            });
        });

        group.bench_function(&format!("Dictionary compression {}", name), |b| {
            let builder = DictionaryBuilder::new();
            let dictionary = builder.build(data);
            let compressor = DictionaryCompressor::new(dictionary);

            b.iter(|| {
                let ratio = compressor.estimate_compression_ratio(black_box(data));
                black_box(ratio)
            });
        });
    }

    group.finish();
}

/// Benchmark entropy coding blob store integration
fn benchmark_entropy_blob_store(c: &mut Criterion) {
    let mut group = c.benchmark_group("Entropy Blob Store Performance");

    let test_data = "hello world! this is test data for entropy blob store. "
        .repeat(100)
        .into_bytes();

    // Benchmark Huffman blob store
    group.bench_function("HuffmanBlobStore setup", |b| {
        b.iter(|| {
            let inner = MemoryBlobStore::new();
            let mut huffman_store = HuffmanBlobStore::new(inner);
            huffman_store.add_training_data(&test_data);
            huffman_store.build_tree().unwrap();
            black_box(huffman_store)
        });
    });

    group.bench_function("HuffmanBlobStore put operations", |b| {
        let inner = MemoryBlobStore::new();
        let mut huffman_store = HuffmanBlobStore::new(inner);
        huffman_store.add_training_data(&test_data);
        huffman_store.build_tree().unwrap();

        b.iter(|| {
            let data = b"test data for blob store";
            let id = huffman_store.put(black_box(data)).unwrap();
            black_box(id)
        });
    });

    // Benchmark compression effectiveness
    group.bench_function("Compression ratio analysis", |b| {
        b.iter(|| {
            // Test multiple data types
            let datasets = [
                (
                    "Random",
                    (0..1000).map(|i| (i * 17) as u8).collect::<Vec<_>>(),
                ),
                (
                    "Text",
                    "the quick brown fox jumps over the lazy dog. "
                        .repeat(50)
                        .into_bytes(),
                ),
                (
                    "Structured",
                    "{\"key\": \"value\", \"number\": 42}"
                        .repeat(100)
                        .into_bytes(),
                ),
            ];

            let mut results = Vec::new();
            for (name, data) in datasets.iter() {
                let entropy = EntropyStats::calculate_entropy(data);
                let theoretical_limit = (1.0 - entropy / 8.0) * 100.0;

                if let Ok(encoder) = HuffmanEncoder::new(data) {
                    let ratio = encoder.estimate_compression_ratio(data);
                    let actual_savings = (1.0 - ratio) * 100.0;
                    results.push((name.to_string(), theoretical_limit, actual_savings));
                }
            }

            black_box(results)
        });
    });

    group.finish();
}

fn benchmark_optimized_rank_select(c: &mut Criterion) {
    let mut group = c.benchmark_group("Optimized Rank-Select Performance");

    // Create test bit vectors of different sizes and patterns
    let sizes = [10_000, 100_000, 1_000_000];
    let densities = [0.1, 0.5, 0.9]; // Different bit densities

    for &size in &sizes {
        for &density in &densities {
            let mut bv = BitVector::new();
            let mut rng_state = 12345u64; // Simple LCG for reproducible results

            for _ in 0..size {
                rng_state = rng_state.wrapping_mul(1103515245).wrapping_add(12345);
                let rand_val = (rng_state >> 16) as f64 / 65536.0;
                bv.push(rand_val < density).unwrap();
            }

            let rs = RankSelect256::new(bv.clone()).unwrap();
            let ones_count = rs.count_ones();

            // Benchmark optimized rank1
            group.bench_function(
                &format!("rank1 size:{} density:{:.1}", size, density),
                |b| {
                    b.iter(|| {
                        let pos = black_box(size / 2);
                        rs.rank1(pos)
                    });
                },
            );

            // Benchmark optimized select1 (if we have enough ones)
            if ones_count > 1000 {
                group.bench_function(
                    &format!("select1_optimized size:{} density:{:.1}", size, density),
                    |b| {
                        b.iter(|| {
                            let k = black_box(ones_count / 2);
                            rs.select1(k).unwrap_or(0)
                        });
                    },
                );

                // Compare with legacy implementation
                group.bench_function(
                    &format!("select1_legacy size:{} density:{:.1}", size, density),
                    |b| {
                        b.iter(|| {
                            let k = black_box(ones_count / 2);
                            rs.select1(k).unwrap_or(0)
                        });
                    },
                );
            }
        }
    }

    group.finish();
}

fn benchmark_lookup_table_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("Lookup Table Operations");

    // Test the core lookup table functions - need to make them public for testing
    group.bench_function("std_u64_count_ones", |b| {
        b.iter(|| {
            let val = black_box(0xAAAAAAAAAAAAAAAAu64);
            val.count_ones()
        });
    });

    // Test with different bit patterns
    let test_values = [
        0x0000000000000000u64, // All zeros
        0xFFFFFFFFFFFFFFFFu64, // All ones
        0xAAAAAAAAAAAAAAAAu64, // Alternating
        0x5555555555555555u64, // Alternating opposite
        0x123456789ABCDEFu64,  // Mixed pattern
    ];

    for (i, &val) in test_values.iter().enumerate() {
        group.bench_function(&format!("count_ones_pattern_{}", i), |b| {
            b.iter(|| black_box(val).count_ones());
        });
    }

    group.finish();
}

fn benchmark_rank_select_comparison(c: &mut Criterion) {
    let mut group = c.benchmark_group("Rank-Select Method Comparison");

    // Create a large bit vector for comprehensive testing
    let mut bv = BitVector::new();
    for i in 0..500_000 {
        bv.push((i * 17 + 7) % 23 == 0).unwrap(); // Complex pattern
    }

    let rs = RankSelect256::new(bv.clone()).unwrap();
    let ones_count = rs.count_ones();

    // Multiple rank operations to test cache effects
    group.bench_function("rank1_batch", |b| {
        b.iter(|| {
            let mut total = 0;
            for i in (0..10).map(|x| x * 50_000) {
                total += rs.rank1(black_box(i));
            }
            total
        });
    });

    // Compare with bit vector's native rank implementation
    group.bench_function("bitvector_rank1_batch", |b| {
        b.iter(|| {
            let mut total = 0;
            for i in (0..10).map(|x| x * 50_000) {
                total += rs.rank1(black_box(i));
            }
            total
        });
    });

    if ones_count > 100 {
        // Multiple select operations
        group.bench_function("select1_optimized_batch", |b| {
            b.iter(|| {
                let mut total = 0;
                for i in (0..10).map(|x| (ones_count * x / 10).min(ones_count - 1)) {
                    total += rs.select1(black_box(i)).unwrap_or(0);
                }
                total
            });
        });

        group.bench_function("select1_legacy_batch", |b| {
            b.iter(|| {
                let mut total = 0;
                for i in (0..10).map(|x| (ones_count * x / 10).min(ones_count - 1)) {
                    total += rs.select1(black_box(i)).unwrap_or(0);
                }
                total
            });
        });
    }

    group.finish();
}

criterion_group!(
    benches,
    benchmark_fast_vec_push,
    benchmark_fast_vec_vs_vec,
    benchmark_fast_str_hash,
    benchmark_fast_str_operations,
    benchmark_succinct_data_structures,
    benchmark_hash_map_comparison,
    benchmark_memory_mapping,
    benchmark_entropy_coding,
    benchmark_entropy_blob_store,
    benchmark_optimized_rank_select,
    benchmark_lookup_table_operations,
    benchmark_rank_select_comparison
);
criterion_main!(benches);