Skip to content

Enitity Component System

What Is ECS?


How ECS Is Fast?


let understand what is the actual problem & how ecs solve it

Modern CPUs are insanely fast at math but painfully slow at memory access

Operation Approx latency
CPU register ~1 cycle
L1 cache ~4 cycles
L2 cache ~12 cycles
L3 cache ~40 cycles
RAM 200–400 cycles

because RAM is slow cpu used cache for storing small data to process.

But the problem with OOPS in traditional Object-Oriented Programming (OOP), you organize code by Objects.

eg:

class model {
    std::string modelName;     // owns heap memory (dynamic memory)
    std::vector<double> pose;  // owns heap memory (dynamic memory)
    float size[3];             // inline
    float scale[3];            // inline
};

/* 
   modelObj is contiguous in memory.
   However, std::string and std::vector store their data on the heap,
   causing pointer indirection and non-contiguous access patterns,
   which reduces cache efficiency in tight loops.
*/
model modelObj[2]; 

memory

but modelName & pose are discontinuous memory because of these it cause trouble to caching
The core problem is CPU cache inefficiency caused by memory layout




ECS Continuous Memory


ECS we just control components & read/write on them one by one

components 1by1

First loop on modelName components, than loop on all Pose components
Because of component now continuous memory is allocated

obj memeory

obj pose