thread-share 0.1.6

A Rust library for safe and efficient data sharing between threads with zero-copy operations, change detection, and enhanced thread management.
Documentation
use std::thread;
use std::time::{Duration, Instant};
use thread_share::{share, simple_share, ArcThreadShare, ArcThreadShareLocked};

#[test]
fn test_performance_comparison() {
    // Benchmark different sharing patterns
    let iterations = 100_000;

    // Test ThreadShare performance
    let thread_share = share!(0);
    let start = Instant::now();
    for _ in 0..iterations {
        thread_share.update(|x| *x += 1);
    }
    let thread_share_duration = start.elapsed();

    // Test SimpleShare performance
    let simple_share = simple_share!(0);
    let start = Instant::now();
    for _ in 0..iterations {
        simple_share.update(|x| *x += 1);
    }
    let simple_share_duration = start.elapsed();

    // Test ArcThreadShare performance
    let arc_share = ArcThreadShare::new(0);
    let start = Instant::now();
    for _ in 0..iterations {
        arc_share.update(|x| *x += 1);
    }
    let arc_share_duration = start.elapsed();

    // Test ArcThreadShareLocked performance
    let original = share!(0);
    let arc_locked_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_locked_data);
    let start = Instant::now();
    for _ in 0..iterations {
        locked_share.update(|x| *x += 1);
    }
    let locked_share_duration = start.elapsed();

    // Verify all reached the same value
    assert_eq!(thread_share.get(), iterations);
    assert_eq!(simple_share.get(), iterations);
    assert_eq!(arc_share.get(), iterations);
    assert_eq!(locked_share.get(), iterations);

    // Print performance results (for informational purposes)
    println!("Performance comparison ({} iterations):", iterations);
    println!("ThreadShare: {:?}", thread_share_duration);
    println!("SimpleShare: {:?}", simple_share_duration);
    println!("ArcThreadShare: {:?}", arc_share_duration);
    println!("ArcThreadShareLocked: {:?}", locked_share_duration);
}

#[test]
fn test_read_write_performance() {
    let iterations = 50_000;

    // Test read-heavy workload
    let data = share!(vec![0; 1000]);
    let start = Instant::now();

    for _ in 0..iterations {
        let _sum = data.read(|v| v.iter().sum::<i32>());
    }

    let read_duration = start.elapsed();

    // Test write-heavy workload
    let start = Instant::now();
    for i in 0..iterations {
        data.update(|v| {
            v[i % 1000] = i as i32;
        });
    }

    let write_duration = start.elapsed();

    println!("Read/Write performance ({} iterations):", iterations);
    println!("Read-heavy: {:?}", read_duration);
    println!("Write-heavy: {:?}", write_duration);

    // Verify data was modified
    let final_data = data.get();
    assert!(final_data.iter().any(|&x| x != 0));
}

#[test]
fn test_memory_efficiency() {
    // Test memory usage patterns

    // Create many small shares
    let mut shares = Vec::new();
    let start = Instant::now();

    for i in 0..1000 {
        shares.push(share!(i));
    }

    let creation_duration = start.elapsed();

    // Access all shares
    let start = Instant::now();
    for share in &shares {
        let _value = share.get();
    }

    let access_duration = start.elapsed();

    // Clean up
    let start = Instant::now();
    drop(shares);
    let cleanup_duration = start.elapsed();

    println!("Memory efficiency test:");
    println!("Creation: {:?}", creation_duration);
    println!("Access: {:?}", access_duration);
    println!("Cleanup: {:?}", cleanup_duration);
}

#[test]
fn test_large_data_performance() {
    let data_size = 100_000;
    let large_data: Vec<i32> = (0..data_size).collect();

    // Test ThreadShare with large data
    let start = Instant::now();
    let thread_share = share!(large_data.clone());
    let creation_duration = start.elapsed();

    let start = Instant::now();
    let _value = thread_share.get();
    let get_duration = start.elapsed();

    let start = Instant::now();
    thread_share.update(|v| v[0] = 999);
    let update_duration = start.elapsed();

    println!("Large data performance ({} elements):", data_size);
    println!("Creation: {:?}", creation_duration);
    println!("Get: {:?}", get_duration);
    println!("Update: {:?}", update_duration);

    // Verify update worked
    assert_eq!(thread_share.get()[0], 999);
}

#[test]
fn test_clone_performance() {
    let original = share!(42);
    let clone_count = 1000;

    // Test cloning performance
    let start = Instant::now();
    let mut clones = Vec::new();

    for _ in 0..clone_count {
        clones.push(original.clone());
    }

    let clone_duration = start.elapsed();

    // Test access through clones
    let start = Instant::now();
    for clone in &clones {
        let _value = clone.get();
    }

    let access_duration = start.elapsed();

    // Test update through original
    let start = Instant::now();
    original.set(100);
    let update_duration = start.elapsed();

    // Verify all clones see the change
    for clone in &clones {
        assert_eq!(clone.get(), 100);
    }

    println!("Clone performance ({} clones):", clone_count);
    println!("Cloning: {:?}", clone_duration);
    println!("Access through clones: {:?}", access_duration);
    println!("Update propagation: {:?}", update_duration);
}

#[test]
fn test_wait_performance() {
    let data = share!(false);
    let data_clone = data.clone();

    // Test wait_for_change performance
    let start = Instant::now();

    let handle = thread::spawn(move || {
        thread::sleep(Duration::from_millis(10));
        data_clone.set(true);
    });

    let timeout_occurred = data.wait_for_change(Duration::from_millis(5));
    assert!(timeout_occurred); // Should timeout

    let wait_duration = start.elapsed();

    // Test wait_for_change_forever performance
    let data2 = share!(false);
    let data2_clone = data2.clone();

    let start = Instant::now();

    let handle2 = thread::spawn(move || {
        thread::sleep(Duration::from_millis(10));
        data2_clone.set(true);
    });

    data2.wait_for_change_forever();
    let wait_forever_duration = start.elapsed();

    handle.join().unwrap();
    handle2.join().unwrap();

    println!("Wait performance:");
    println!("Wait with timeout: {:?}", wait_duration);
    println!("Wait forever: {:?}", wait_forever_duration);
}

#[test]
fn test_mixed_operations_performance() {
    let data = share!(vec![0; 1000]);
    let operations = 10_000;

    let start = Instant::now();

    for i in 0..operations {
        match i % 4 {
            0 => {
                // Read operation
                let _sum = data.read(|v| v.iter().sum::<i32>());
            }
            1 => {
                // Write operation
                data.update(|v| v[i % 1000] = i as i32);
            }
            2 => {
                // Get operation
                let _value = data.get();
            }
            3 => {
                // Set operation
                data.set(vec![i as i32; 1000]);
            }
            _ => unreachable!(),
        }
    }

    let mixed_duration = start.elapsed();

    println!("Mixed operations performance ({} operations):", operations);
    println!("Mixed read/write/get/set: {:?}", mixed_duration);

    // Verify final state
    let final_data = data.get();
    assert_eq!(final_data.len(), 1000);
}