by Gendo (aka Daniele Maccioni)
RAM latency = ~1-2 cycles
RAM latency = ~200 cycles
When a byte of code or data is loaded in cache from the RAM a chunk of contiguous memory is fetched instead, called: cache line
class Object {
public:
void addChild(Object *child);
void removeChild(Object *child);
virtual void update();
private:
int ID;
int width;
int height;
std::vector<Object *> children;
};
class MovableObject: public Object {
public:
void translate(int x, int y);
private:
int x;
int y;
};
class ObjectManager : public Manager {
public:
// ...
void initObject(Object *);
void updateObject(Object *);
void removeObject(Object *);
// ...
};
Do we really have only one object?
The common case for data is not considered
class SystemNetwork : public SystemSocket {
public:
// ...
void sendMessage(Message message) {
int message_type = message.type;
if (message_type == Message::Type::Text) {
// ...
} else if (message_type == Message::Type::Binary) {
// ...
}
if (inactive) {
// ...
}
}
// ...
};
Difficult to predict the code path
// The Common Case
void updateObjects(Objects *objects, int count) {
// ...
}
class Object {
// The 0.01%: I will always have multiple objects!
void update() {}
}
class Object {
// ...
int x
int y
void move(int x, int y);
// ...
}
Point2D positions[COUNT_OBJS];
Point2D movements[COUNT_OBJS];
void moveObjects() {
for (int i = 0; i < COUNT_OBJS; ++i) {
positions[i] += movements[i];
}
}
class Object {
// ...
void update() {
if (active) {
// ...
} else {
// ...
}
}
// ...
};
void updateObjects(Objects *objects) {
int numActives = sortByActive(objects);
for (int i = 0; i < numActives; ++i) {
// ...
Object *obj = objects[i];
// ...
}
}