use rat_quickmem::{
simd::{SimdMemcpy, SimdConfig, SimdCapabilities},
};
use serde::{Deserialize, Serialize};
use bincode::{Encode, Decode};
use std::time::Instant;
#[derive(Serialize, Deserialize, Encode, Decode, Debug, Clone, PartialEq)]
struct LargeImageData {
width: u32,
height: u32,
channels: u8,
pixels: Vec<u8>,
}
impl LargeImageData {
fn new(width: u32, height: u32, channels: u8) -> Self {
let size = (width * height * channels as u32) as usize;
let pixels = (0..size).map(|i| (i % 256) as u8).collect();
Self {
width,
height,
channels,
pixels,
}
}
fn size_mb(&self) -> f64 {
self.pixels.len() as f64 / 1024.0 / 1024.0
}
}
#[derive(Serialize, Deserialize, Encode, Decode, Debug, Clone)]
struct NetworkPacket {
id: u64,
timestamp: u64,
payload: Vec<u8>,
}
impl NetworkPacket {
fn new(id: u64, payload_size: usize) -> Self {
let payload = (0..payload_size).map(|i| ((i + id as usize) % 256) as u8).collect();
Self {
id,
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as u64,
payload,
}
}
}
fn main() {
println!("=== SIMD 与编解码器集成示例 ===");
let caps = SimdCapabilities::detect();
println!("\n平台 SIMD 能力: {:?}", caps);
large_image_processing_example();
network_batch_processing_example();
memory_pool_simd_example();
performance_comparison_example();
}
fn large_image_processing_example() {
println!("\n=== 1. 大图像数据处理示例 ===");
let image = LargeImageData::new(3840, 2160, 3); println!("图像大小: {}x{} ({:.1} MB)", image.width, image.height, image.size_mb());
let simd_config = SimdConfig {
enable_avx2: true,
enable_sse2: true,
enable_neon: true,
min_chunk_size: 64, };
let simd = SimdMemcpy::new(simd_config);
println!("使用 SIMD: {}", simd.get_active_simd());
let start = Instant::now();
let encoded = bincode::encode_to_vec(&image, bincode::config::standard()).expect("编码失败");
let encode_time = start.elapsed();
println!("编码时间: {:?}", encode_time);
println!("编码后大小: {:.2} MB", encoded.len() as f64 / 1024.0 / 1024.0);
let start = Instant::now();
let (decoded, _): (LargeImageData, usize) = bincode::decode_from_slice(&encoded, bincode::config::standard()).expect("解码失败");
let decode_time = start.elapsed();
println!("解码时间: {:?}", decode_time);
println!("数据完整性: {}", if image == decoded { "✓" } else { "✗" });
let mut processed_pixels = vec![0u8; image.pixels.len()];
let start = Instant::now();
simd.copy(&image.pixels, &mut processed_pixels).expect("SIMD 拷贝失败");
let simd_copy_time = start.elapsed();
println!("SIMD 像素拷贝时间: {:?}", simd_copy_time);
let mut std_pixels = vec![0u8; image.pixels.len()];
let start = Instant::now();
std_pixels.copy_from_slice(&image.pixels);
let std_copy_time = start.elapsed();
println!("标准拷贝时间: {:?}", std_copy_time);
let speedup = std_copy_time.as_nanos() as f64 / simd_copy_time.as_nanos() as f64;
println!("SIMD 加速比: {:.2}x", speedup);
}
fn network_batch_processing_example() {
println!("\n=== 2. 网络数据批处理示例 ===");
let packet_sizes = [64, 256, 1024, 1500]; let packets_per_size = 1000;
let simd_config = SimdConfig {
min_chunk_size: 32, ..Default::default()
};
let simd = SimdMemcpy::new(simd_config);
for &packet_size in &packet_sizes {
println!("\n处理 {} 字节网络包 (批次: {} 个)", packet_size, packets_per_size);
let packets: Vec<NetworkPacket> = (0..packets_per_size)
.map(|i| NetworkPacket::new(i as u64, packet_size))
.collect();
let start = Instant::now();
let encoded_packets: Vec<_> = packets
.iter()
.map(|packet| bincode::encode_to_vec(packet, bincode::config::standard()).expect("编码失败"))
.collect();
let encode_time = start.elapsed();
let start = Instant::now();
let decoded_packets: Vec<NetworkPacket> = encoded_packets
.iter()
.map(|data| {
let (packet, _): (NetworkPacket, usize) = bincode::decode_from_slice(data, bincode::config::standard()).expect("解码失败");
packet
})
.collect();
let decode_time = start.elapsed();
let start = Instant::now();
for (original, decoded) in packets.iter().zip(decoded_packets.iter()) {
let mut buffer = vec![0u8; decoded.payload.len()];
simd.copy(&decoded.payload, &mut buffer).expect("SIMD 拷贝失败");
}
let simd_copy_time = start.elapsed();
let total_data = packet_size * packets_per_size;
let encode_throughput = total_data as f64 / encode_time.as_secs_f64() / 1024.0 / 1024.0;
let decode_throughput = total_data as f64 / decode_time.as_secs_f64() / 1024.0 / 1024.0;
let copy_throughput = total_data as f64 / simd_copy_time.as_secs_f64() / 1024.0 / 1024.0;
println!(" 编码吞吐量: {:.2} MB/s", encode_throughput);
println!(" 解码吞吐量: {:.2} MB/s", decode_throughput);
println!(" SIMD 拷贝吞吐量: {:.2} MB/s", copy_throughput);
let integrity_ok = packets.iter().zip(decoded_packets.iter())
.all(|(orig, decoded)| orig.payload == decoded.payload);
println!(" 数据完整性: {}", if integrity_ok { "✓" } else { "✗" });
}
}
fn memory_pool_simd_example() {
println!("\n=== 3. 内存池与 SIMD 结合示例 ===");
let mut buffers: Vec<Vec<u8>> = Vec::new();
let simd = SimdMemcpy::new(SimdConfig::default());
let data_sizes = [1024, 4096, 16384]; let iterations = 10000;
for &size in &data_sizes {
println!("\n处理 {} KB 数据 ({} 次迭代)", size / 1024, iterations);
let test_data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
let start = Instant::now();
for _ in 0..iterations {
let mut buffer = vec![0u8; size];
simd.copy(&test_data, &mut buffer).expect("SIMD 拷贝失败");
std::hint::black_box(&buffer);
buffers.push(buffer);
}
let duration = start.elapsed();
let throughput = (size * iterations) as f64 / duration.as_secs_f64() / 1024.0 / 1024.0;
println!(" 总时间: {:?}", duration);
println!(" 吞吐量: {:.2} MB/s", throughput);
println!(" 平均每次: {:?}", duration / iterations as u32);
println!(" 缓冲区数量: {}", buffers.len());
}
let block_count = 1000;
let block_size = 4096;
let start = Instant::now();
for i in 0..block_count {
let mut buffer = vec![0u8; block_size];
let data: Vec<u8> = (0..block_size).map(|j| ((i + j) % 256) as u8).collect();
simd.copy(&data, &mut buffer).expect("SIMD 拷贝失败");
buffers.push(buffer);
}
let total_time = start.elapsed();
println!("处理 {} 个 {} 字节块", block_count, block_size);
println!("总时间: {:?}", total_time);
println!("平均每块: {:?}", total_time / block_count as u32);
println!("吞吐量: {:.2} MB/s",
(block_count * block_size) as f64 / 1024.0 / 1024.0 / total_time.as_secs_f64());
println!("分配的缓冲区数量: {}", buffers.len());
}
fn performance_comparison_example() {
println!("\n=== 4. 性能对比测试 ===");
let test_sizes = [1024, 8192, 32768, 131072]; let iterations = 1000;
let configs = vec![
("标准拷贝", None),
("SIMD 默认", Some(SimdConfig::default())),
("SIMD 大块", Some(SimdConfig {
min_chunk_size: 64,
..Default::default()
})),
("SIMD 小块", Some(SimdConfig {
min_chunk_size: 16,
..Default::default()
})),
];
println!("\n{:<12} {:<8} {:<12} {:<12} {:<10}", "配置", "大小", "时间", "吞吐量", "加速比");
println!("{}", "-".repeat(60));
for &size in &test_sizes {
let test_data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
let mut baseline_time: Option<std::time::Duration> = None;
for (config_name, simd_config) in &configs {
let start = Instant::now();
for _ in 0..iterations {
let mut dst = vec![0u8; size];
match simd_config {
Some(config) => {
let simd = SimdMemcpy::new(*config);
simd.copy(&test_data, &mut dst).expect("SIMD 拷贝失败");
}
None => {
dst.copy_from_slice(&test_data);
}
}
std::hint::black_box(dst);
}
let duration = start.elapsed();
let throughput = (size * iterations) as f64 / duration.as_secs_f64() / 1024.0 / 1024.0;
let speedup = if let Some(baseline) = baseline_time {
baseline.as_nanos() as f64 / duration.as_nanos() as f64
} else {
baseline_time = Some(duration);
1.0
};
println!("{:<12} {:<8} {:<12?} {:<12.2} {:<10.2}x",
config_name,
format!("{}KB", size / 1024),
duration,
throughput,
speedup);
}
println!();
}
}