Skip to content

SYSUljz/memory_layout_test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Memory Layout Optimization Performance Comparison

This project demonstrates various Rust memory layout optimization techniques and benchmarks their performance using Criterion.

Project Structure

  • src/lib.rs - Implements all optimization techniques
  • src/main.rs - Demonstrates the optimizations in action
  • benches/allocation_bench.rs - Benchmarks comparing allocation strategies

Implemented Optimization Techniques

1. Memory Allocation Strategies

  • Direct Allocation: Allocate memory using Box::new()
  • Memory Pool Allocation: Use a custom ObjectPool to reuse memory and reduce allocation/deallocation overhead

2. Memory Layout Optimizations

  • AoS (Array of Structures): Traditional struct array layout
  • SoA (Structure of Arrays): Separate struct fields into individual arrays for better cache efficiency

3. Cache Access Patterns

  • Cache-Friendly Access: Sequential memory access leveraging spatial locality
  • Cache-Unfriendly Access: Random memory access that disrupts cache locality

4. Memory Preallocation

  • Dynamic Growth: Let Vec grow automatically as elements are pushed
  • Preallocated Capacity: Use Vec::with_capacity() to allocate memory ahead of time

Usage

Running the Demo Program

cargo run

Running the Performance Benchmarks

cargo bench

Viewing Benchmark Reports

After benchmarks complete, open target/criterion/report/index.html in your browser to view the detailed performance report.

Key Optimization Concepts

Object Pooling

Object pooling reduces dynamic allocations by reusing preallocated memory blocks:

  • Reduces system calls
  • Minimizes memory fragmentation
  • Speeds up allocation/deallocation

SoA vs AoS

AoS (Array of Structures):

struct DataNode {
    id: u64,
    value: f64,
    data: [u8; 512],
}
let nodes: Vec<DataNode> = vec![...];

SoA (Structure of Arrays):

struct SoADataStore {
    ids: Vec<u64>,
    values: Vec<f64>,
    data: Vec<[u8; 512]>,
}

SoA improves cache locality when accessing specific fields.

SoA在需要访问特定字段时更加缓存友好,因为相关数据在内存中连续存放。

3. 缓存局部性

  • 空间局部性: 访问相邻内存地址的数据
  • 时间局部性: 短时间内重复访问同一数据

顺序访问模式能更好地利用CPU缓存,显著提高性能。

4. 内存预分配

预分配避免了频繁的内存重新分配:

  • 减少realloc调用
  • 避免数据复制
  • 提供更可预测的性能

Performance Improvement Examples

Based on benchmark results, you can expect:

  • 10-30% speedup with SoA compared to AoS
  • 20-50% speedup with memory pool vs direct allocation
  • 2-5× speedup with cache-friendly vs cache-unfriendly access
  • 15-40% speedup with preallocated capacity

具体数值取决于硬件配置和数据规模。

Practical Recommendations

  1. Choose data layout based on access patterns (AoS vs SoA)
  2. Use memory pooling for frequently allocated/freed objects
  3. Preallocate memory when you know the element count
  4. Optimize access patterns (favor sequential over random)
  5. Measure performance using benchmarks (e.g., Criterion)

Further Reading

About

This is a simple memory_layout test in rust help understand how memory layout influence Program efficiency

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors