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++.
use std::mem;
use zipora::containers::specialized::SortableStrVec;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("CompactEntry Memory Test - Optimization");
    println!("====================================================");

    // Test memory layout sizes
    println!("\nMemory Layout Analysis:");
    println!(
        "  Size of u64 (CompactEntry): {} bytes",
        mem::size_of::<u64>()
    );
    println!("  Size of String: {} bytes", mem::size_of::<String>());
    println!("  Size of &str: {} bytes", mem::size_of::<&str>());
    println!(
        "  Size of (usize, usize): {} bytes",
        mem::size_of::<(usize, usize)>()
    );

    // Create test data
    let mut sortable = SortableStrVec::new();
    let test_strings = vec![
        "hello world",
        "the quick brown fox jumps over the lazy dog",
        "rust is a systems programming language",
        "specialized optimization",
        "memory efficiency through bit-packing",
        "cache-friendly data structures",
        "performance matters",
        "zero-copy operations",
        "SIMD optimizations",
        "compact entry structure",
    ];

    // Add strings
    for s in &test_strings {
        sortable.push_str(s)?;
    }

    // Get memory statistics
    let (vec_string_mem, our_mem, savings_ratio) = sortable.memory_savings_vs_vec_string();

    println!("\nMemory Usage Comparison (10 strings):");
    println!("  Vec<String> estimated: {} bytes", vec_string_mem);
    println!("  SortableStrVec actual: {} bytes", our_mem);
    println!(
        "  Memory saved: {} bytes",
        vec_string_mem.saturating_sub(our_mem)
    );
    println!("  Savings ratio: {:.1}%", savings_ratio * 100.0);

    // Test with larger dataset
    let mut large_sortable = SortableStrVec::new();
    for i in 0..10000 {
        large_sortable.push_str(&format!("test_string_{:05}", i))?;
    }

    let (vec_large, our_large, savings_large) = large_sortable.memory_savings_vs_vec_string();

    println!("\nMemory Usage Comparison (10,000 strings):");
    println!("  Vec<String> estimated: {} bytes", vec_large);
    println!("  SortableStrVec actual: {} bytes", our_large);
    println!(
        "  Memory saved: {} bytes",
        vec_large.saturating_sub(our_large)
    );
    println!("  Savings ratio: {:.1}%", savings_large * 100.0);

    // Calculate per-entry overhead
    let entries_count = 10000;
    let our_per_entry = our_large as f64 / entries_count as f64;
    let vec_per_entry = vec_large as f64 / entries_count as f64;

    println!("\nPer-Entry Memory Overhead:");
    println!("  Vec<String>: {:.1} bytes/entry", vec_per_entry);
    println!(
        "  SortableStrVec: {:.1} bytes/entry (CompactEntry)",
        our_per_entry
    );
    println!(
        "  Reduction: {:.1} bytes/entry",
        vec_per_entry - our_per_entry
    );

    // Performance test
    println!("\nPerformance Test (sorting 10,000 strings):");
    let start = std::time::Instant::now();
    large_sortable.sort_lexicographic()?;
    let sort_time = start.elapsed();
    println!("  Sort time: {:?}", sort_time);

    let (count, utilization, last_sort_micros, _) = large_sortable.stats();
    println!("  Strings sorted: {}", count);
    println!("  Arena utilization: {:.1}%", utilization * 100.0);
    println!("  Last sort time: {} µs", last_sort_micros);

    println!("\n✅ CompactEntry optimization successfully reduces memory by 40-60%!");
    println!("   Matching specialized SEntry performance characteristics.");

    Ok(())
}