1 year ago
#322889
nickexists
Why does removing class member make code significantly slower
I have a class which is frequently copied in a performance critical section of code. The class has a string
member variable which may be set multiple times during the an object's lifetime but ultimately isn't used for anything meaningful. For obvious reasons I would like to remove this variable.
class MyClass {
public:
//Constructor
...
//Methods
...
//Properties
double usefulMember;
int usefulMember2;
std::string uselessString;
};
However, I have found that if I remove uselessString
then my code runs ~15% slower. Can somebody explain why this would be? If anything I would think that getting rid of the need to copy this string over and over would improve performance.
One thing I noticed is that with the uselessString
variable sizeof(MyClass)
is 448 (divisible by 64). Could this have to do with how MyClass
aligns with the CPU cache? I tried declaring the class with alignas(64)
but it didn't help.
Edit: I ended up using the Intel TBB malloc_proxy
to implement scalable memory allocation across my whole program. I don't know exactly what the issue was before but things are now running faster than ever before.
c++
alignment
tbb
0 Answers
Your Answer