AOS & SOA

A Brief Review of Cache

Cache Architecture

  • Cache is an essential component in modern computers. Usually, a CPU holds 3-level caches and the capacities are sorted in ascending order whereas the speed is verse versa.
  • Importantly, we need to avoid frequently data transfer among different levels for higher performance.

Cache Line

  • Data is transferred between memory and cache in blocks of fixed size (typically 64 bytes), called Cache Lines or Cache Blocks.
  • A cache can only hold a limited number of lines, determined by the cache size. For example, a 64 kilobyte cache with 64-byte lines has 1024 cache lines.
  • Every time you load any memory at all, you are loading in a full cache line of bytes.

AOS vs SOA

AOS(Arrary of Structure)

struct AOS
{
	int data_int;
	float data_float;
};
AOS aos[SIZE];

SOA(Structues of Array)

struct SOA
{
	int data_int[SIZE];
	float data_float[SIZE];
};
SOA soa;

Speed Test in O2 Optimization

C++
int main(int argc, char* argv[])
{
    // FU::LoopTimer timer; (You may use your timer here)

    for (int i = 0; i < SIZE; ++i) aos[i].data_float /= 3.0f;
    //std::cout << "AOS Time:: " << timer.GetDuration().Secend() << " sec\n";
    for (int i = 0; i < SIZE; ++i) soa.data_float[i] /= 3.0f;
    //std::cout << "SOA Time:: " << timer.GetDuration().Secend() << " sec\n";

    return EXIT_SUCCESS;
}
AOS0.55496 sec
SOA0.220317 sec

Conclusion

  • Obviously, SOA is denser than AOS in memory layout. Therefore, SOA is better for transfer data to cache and the test results also confirmed this point.

Leave a Reply

Your email address will not be published. Required fields are marked *