So, in the previous post I talked about, fast_remove_if, and going faster than the std::remove_if function. Now there is a further improvement that can be made to go *even* faster, but with no need to modify any functions – instead it involves modifying the alignment of the data in the vector.
Here’s the object we used, it has no specific alignment.
class TestObject {
public:
bool remove{ false };
int i{ 8 };
float k{ 2 }, a{ 1 };
double d{ 99.99 };
int i2{ 81 };
float k2{ 21 }, a2{ 11 };
double d2{ 991.99 };
int i3{ 80 };
float k3{ 20 }, a3{ 10 };
double d3{ 990.990 };
};
To make this object faster to work with, it needs to be aligned to the stack size, which on x64 bit systems, is 16-bytes.
Here’s how we do that:
__declspec(align(16)) class TestObject {
public:
bool remove{ false };
int i{ 8 };
float k{ 2 }, a{ 1 };
double d{ 99.99 };
int i2{ 81 };
float k2{ 21 }, a2{ 11 };
double d2{ 991.99 };
int i3{ 80 };
float k3{ 20 }, a3{ 10 };
double d3{ 990.990 };
};
And now, magically, our object is going to be speedier. Here’s some results from the previous unit test, using this new aligned object.
Given 19730 objects, with a randomly selected amount to remove.
Results for fast_remove_if:
- one percent of objects: 65 avg micro
- varying percent of objects: 116 avg micro
Results for erase, std::remove_if
- one percent of objects: 88 avg micro
- varying percent of objects: 117 avg micro
An additional improvement of around 20%.