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
//! Comprehensive memory allocation performance benchmarks
//!
//! This benchmark suite tests the performance of the advanced tiered memory
//! allocator against standard allocation and compares with C++ performance.

use criterion::{BenchmarkId, Criterion, Throughput, black_box, criterion_group, criterion_main};
use std::alloc::{Layout, alloc, dealloc};
use zipora::memory::{
    MemoryMappedAllocator, MemoryPool, PoolConfig, TieredConfig, TieredMemoryAllocator,
    tiered_allocate, tiered_deallocate,
};

/// Test allocation patterns that match the C++ comparison
fn benchmark_allocation_patterns_detailed(c: &mut Criterion) {
    let mut group = c.benchmark_group("Memory Allocation Performance");

    // Test the specific allocation patterns from PERF_VS_CPP.md
    let test_cases = vec![
        ("Small_100x64B", 100, 64),
        ("Medium_100x1KB", 100, 1024),
        ("Large_100x16KB", 100, 16 * 1024),
        ("XLarge_100x64KB", 100, 64 * 1024),
        ("Huge_10x1MB", 10, 1024 * 1024),
        ("Huge_10x4MB", 10, 4 * 1024 * 1024),
    ];

    for (name, count, size) in test_cases {
        group.throughput(Throughput::Elements(count as u64));

        // Test standard Rust allocation
        group.bench_function(BenchmarkId::new("Rust_Standard", name), |b| {
            b.iter(|| {
                let mut allocations = Vec::new();
                for _ in 0..count {
                    let layout = Layout::from_size_align(size, 8).unwrap();
                    let ptr = unsafe { alloc(layout) };
                    if !ptr.is_null() {
                        allocations.push((ptr, layout));
                        // Touch memory to ensure allocation
                        unsafe {
                            ptr.write(42);
                        }
                    }
                }

                // Deallocate all
                for (ptr, layout) in allocations {
                    unsafe {
                        dealloc(ptr, layout);
                    }
                }
                black_box(());
            });
        });

        // Test tiered allocator
        group.bench_function(BenchmarkId::new("Rust_Tiered", name), |b| {
            b.iter(|| {
                let mut allocations = Vec::new();
                for _ in 0..count {
                    if let Ok(allocation) = tiered_allocate(size) {
                        // Touch memory to ensure allocation
                        let slice = allocation.as_slice();
                        if !slice.is_empty() {
                            unsafe {
                                *(slice.as_ptr() as *mut u8) = 42;
                            }
                        }
                        allocations.push(allocation);
                    }
                }

                // Deallocate all
                for allocation in allocations {
                    let _ = tiered_deallocate(allocation);
                }
                black_box(());
            });
        });

        // Test custom tiered allocator instance for this size
        group.bench_function(BenchmarkId::new("Rust_Optimized", name), |b| {
            let config = TieredConfig {
                enable_small_pools: size <= 1024,
                enable_medium_pools: size > 1024 && size <= 16 * 1024,
                enable_mmap_large: size > 16 * 1024,
                enable_hugepages: size > 2 * 1024 * 1024,
                mmap_threshold: 16 * 1024,
                hugepage_threshold: 2 * 1024 * 1024,
            };
            let allocator = TieredMemoryAllocator::new(config).unwrap();

            b.iter(|| {
                let mut allocations = Vec::new();
                for _ in 0..count {
                    if let Ok(allocation) = allocator.allocate(size) {
                        // Touch memory to ensure allocation
                        let slice = allocation.as_slice();
                        if !slice.is_empty() {
                            unsafe {
                                *(slice.as_ptr() as *mut u8) = 42;
                            }
                        }
                        allocations.push(allocation);
                    }
                }

                // Deallocate all
                for allocation in allocations {
                    let _ = allocator.deallocate(allocation);
                }
                black_box(());
            });
        });
    }

    group.finish();
}

/// Benchmark memory-mapped allocator performance specifically
fn benchmark_mmap_performance(c: &mut Criterion) {
    let mut group = c.benchmark_group("Memory Mapped Allocation");

    let allocator = MemoryMappedAllocator::default();

    let sizes = vec![
        16 * 1024,       // 16KB
        64 * 1024,       // 64KB
        256 * 1024,      // 256KB
        1024 * 1024,     // 1MB
        4 * 1024 * 1024, // 4MB
    ];

    for size in sizes {
        group.throughput(Throughput::Bytes(size as u64));

        group.bench_function(BenchmarkId::new("mmap_allocate", size), |b| {
            b.iter(|| {
                let allocation = allocator.allocate(size).unwrap();
                // Touch first and last page to ensure mapping
                let slice = allocation.as_slice();
                unsafe {
                    *(slice.as_ptr() as *mut u8) = 42;
                    *((slice.as_ptr() as *mut u8).add(size - 1)) = 84;
                }
                allocator.deallocate(allocation).unwrap();
                black_box(());
            });
        });

        // Test allocation reuse (cache efficiency)
        group.bench_function(BenchmarkId::new("mmap_reuse", size), |b| {
            b.iter(|| {
                let allocation1 = allocator.allocate(size).unwrap();
                allocator.deallocate(allocation1).unwrap();

                let allocation2 = allocator.allocate(size).unwrap();
                allocator.deallocate(allocation2).unwrap();
                black_box(());
            });
        });
    }

    group.finish();
}

/// Benchmark pool-based allocation for medium sizes
fn benchmark_pool_performance(c: &mut Criterion) {
    let mut group = c.benchmark_group("Memory Pool Allocation");

    // Create pools for different size classes
    let pool_configs = vec![
        ("1KB", PoolConfig::new(1024, 100, 8)),
        ("4KB", PoolConfig::new(4096, 50, 16)),
        ("16KB", PoolConfig::new(16384, 20, 32)),
    ];

    for (name, config) in pool_configs {
        let pool = MemoryPool::new(config).unwrap();
        let chunk_size = pool.config().chunk_size;

        group.throughput(Throughput::Bytes(chunk_size as u64));

        // Test allocation/deallocation performance
        group.bench_function(BenchmarkId::new("pool_alloc_dealloc", name), |b| {
            b.iter(|| {
                let chunk = pool.allocate().unwrap();
                // Touch memory
                unsafe {
                    chunk.as_ptr().write(42);
                }
                pool.deallocate(chunk).unwrap();
                black_box(());
            });
        });

        // Test bulk allocation
        group.bench_function(BenchmarkId::new("pool_bulk_alloc", name), |b| {
            b.iter(|| {
                let mut chunks = Vec::new();
                for _ in 0..10 {
                    let chunk = pool.allocate().unwrap();
                    chunks.push(chunk);
                }

                for chunk in chunks {
                    pool.deallocate(chunk).unwrap();
                }
                black_box(());
            });
        });

        // Test pool warmup effect
        group.bench_function(BenchmarkId::new("pool_warm", name), |b| {
            // Warm up the pool
            let mut warmup_chunks = Vec::new();
            for _ in 0..50 {
                if let Ok(chunk) = pool.allocate() {
                    warmup_chunks.push(chunk);
                }
            }
            for chunk in warmup_chunks {
                let _ = pool.deallocate(chunk);
            }

            b.iter(|| {
                let chunk = pool.allocate().unwrap();
                pool.deallocate(chunk).unwrap();
                black_box(());
            });
        });
    }

    group.finish();
}

/// Benchmark allocation pattern adaptation
fn benchmark_adaptive_allocation(c: &mut Criterion) {
    let mut group = c.benchmark_group("Adaptive Allocation Patterns");

    let allocator = TieredMemoryAllocator::default().unwrap();

    // Test different allocation patterns
    let patterns = vec![
        (
            "small_dominated",
            vec![128, 256, 512, 256, 128, 512, 256, 128],
        ),
        (
            "medium_dominated",
            vec![2048, 4096, 8192, 4096, 2048, 8192, 4096],
        ),
        ("large_dominated", vec![32768, 65536, 131072, 65536, 32768]),
        (
            "mixed_pattern",
            vec![128, 4096, 65536, 256, 8192, 131072, 512],
        ),
    ];

    for (pattern_name, sizes) in patterns {
        group.throughput(Throughput::Elements(sizes.len() as u64));

        group.bench_function(BenchmarkId::new("adaptive", pattern_name), |b| {
            b.iter(|| {
                let mut allocations = Vec::new();

                // Allocate according to pattern
                for &size in &sizes {
                    if let Ok(allocation) = allocator.allocate(size) {
                        allocations.push(allocation);
                    }
                }

                // Deallocate all
                for allocation in allocations {
                    let _ = allocator.deallocate(allocation);
                }

                black_box(());
            });
        });
    }

    group.finish();
}

/// Benchmark concurrent allocation performance
fn benchmark_concurrent_allocation(c: &mut Criterion) {
    let mut group = c.benchmark_group("Concurrent Allocation");

    let allocator = TieredMemoryAllocator::default().unwrap();
    let allocator = std::sync::Arc::new(allocator);

    let sizes = vec![1024, 4096, 16384, 65536];

    for size in sizes {
        group.throughput(Throughput::Bytes(size as u64));

        group.bench_function(BenchmarkId::new("concurrent", size), |b| {
            b.iter(|| {
                let handles: Vec<_> = (0..4)
                    .map(|_| {
                        let allocator = allocator.clone();
                        std::thread::spawn(move || {
                            let mut allocations = Vec::new();
                            for _ in 0..10 {
                                if let Ok(allocation) = allocator.allocate(size) {
                                    allocations.push(allocation);
                                }
                            }

                            for allocation in allocations {
                                let _ = allocator.deallocate(allocation);
                            }
                        })
                    })
                    .collect();

                for handle in handles {
                    handle.join().unwrap();
                }

                black_box(());
            });
        });
    }

    group.finish();
}

/// Test memory allocation latency distribution
fn benchmark_allocation_latency(c: &mut Criterion) {
    let mut group = c.benchmark_group("Allocation Latency");
    group.sample_size(1000); // More samples for latency analysis

    let allocator = TieredMemoryAllocator::default().unwrap();

    let sizes = vec![
        ("small", 512),
        ("medium", 4096),
        ("large", 65536),
        ("huge", 1048576),
    ];

    for (name, size) in sizes {
        group.bench_function(BenchmarkId::new("latency", name), |b| {
            b.iter(|| {
                let allocation = allocator.allocate(size).unwrap();
                let _ = allocator.deallocate(allocation);
                black_box(());
            });
        });
    }

    group.finish();
}

/// Performance regression test against the old system
fn benchmark_performance_regression(c: &mut Criterion) {
    let mut group = c.benchmark_group("Performance Regression Test");

    // Create old-style allocator (basic pools only)
    let old_style_config = TieredConfig {
        enable_small_pools: true,
        enable_medium_pools: false, // Disabled
        enable_mmap_large: false,   // Disabled
        enable_hugepages: false,    // Disabled
        mmap_threshold: usize::MAX,
        hugepage_threshold: usize::MAX,
    };
    let old_allocator = TieredMemoryAllocator::new(old_style_config).unwrap();

    // New optimized allocator
    let new_allocator = TieredMemoryAllocator::default().unwrap();

    let test_sizes = vec![1024, 16384, 65536, 1048576];

    for size in test_sizes {
        group.throughput(Throughput::Bytes(size as u64));

        group.bench_function(BenchmarkId::new("old_system", size), |b| {
            b.iter(|| {
                let allocation = old_allocator.allocate(size).unwrap();
                let _ = old_allocator.deallocate(allocation);
                black_box(());
            });
        });

        group.bench_function(BenchmarkId::new("new_system", size), |b| {
            b.iter(|| {
                let allocation = new_allocator.allocate(size).unwrap();
                let _ = new_allocator.deallocate(allocation);
                black_box(());
            });
        });
    }

    group.finish();
}

criterion_group!(
    memory_benches,
    benchmark_allocation_patterns_detailed,
    benchmark_mmap_performance,
    benchmark_pool_performance,
    benchmark_adaptive_allocation,
    benchmark_concurrent_allocation,
    benchmark_allocation_latency,
    benchmark_performance_regression
);

criterion_main!(memory_benches);