5

This question is related to another post of mine: why allocate_shared and make_shared so slow

In here I can describe the question more clearly.

Think about the following code:

struct A {
    char data_[0x10000];
};

class C {
public:
    C() : a_() { }
    A a_;
};

int main() {
    C c;
    return 0;
}

I found for the code C() : a_(), the compiler uses memset(addr,0,0x10000) as the constructor of the A. And if the type A has a empty constructor, the asm code is right.

To describe the issue more clearly, I wrote some test code:

#include <stdlib.h>

struct A {
    //A() {}
    char data_[0x10000];
    void dummy() { // avoid optimize erase by compiler
        data_[rand() % sizeof(data_)] = 1;
    }
    int dummy2() { // avoid optimize erase by compiler
        return data_[0];
    }
};

class B {
public:
    template<class ... T> B(T&...t) 
        : a_(std::forward<T>(t)...) {
    }
    A a_;
};

class C {
public:
    C() : a_() {
    }
    A a_;
};

template<class ... T>
int test(T&...t) {
    A a(t...);
    a.dummy();
    return a.dummy2();
}

int main() {
    A a;
    a.dummy();
    auto r1 = a.dummy2();

    auto r2 = test();

    B b;
    b.a_.dummy();
    auto r3 = b.a_.dummy2();

    C c;
    c.a_.dummy();
    auto r4 = c.a_.dummy2();
    return r1 + r2 + r3 + r4;
}

I compiled the code with vs2017, in windows 10, x86 release build. Then I checked the asm code:

template<class ... T>
int test(T&...t) {
00E510B8  call        _chkstk (0E51CE0h)  
00E510BD  mov         eax,dword ptr [__security_cookie (0E53004h)]  
00E510C2  xor         eax,ebp  
00E510C4  mov         dword ptr [ebp-4],eax  
    A a(t...);
00E510C7  push        10000h  
00E510CC  lea         eax,[a]  
00E510D2  push        0  
00E510D4  push        eax  
00E510D5  call        _memset (0E51C3Ah)  
00E510DA  add         esp,0Ch  
    a.dummy();
00E510DD  call        dword ptr [__imp__rand (0E520B4h)]  
}
00E510E3  mov         ecx,dword ptr [ebp-4]  

It is very clear that the function test() calls memset(p, 0, 0x10000).

And if I add an empty constructor in A (line A(){}), the compiler removes the memset.

So why does the code call memset when type A does not have constructor but does not call memset when A has a constructor?

Is it part of the c++ standard, or just a compiler bug?

Obviously the memset(p, 0, sizeof(T)) is useless and harmful which slows down the program. How do I workaround it?

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
alpha
  • 1,228
  • 1
  • 11
  • 26

2 Answers2

9
A a(t...);

Will be parsed as initializing a with t.... When t... is empty, as when you call it, this will be understood as value-initializing a.

For A without a user-provided default constructor, value-initialize is to zero all its members, hence the memset.

When you provide a constructor for A, value-initialize is to call the default constructor, which you defined to be do nothing, therefore no memset will be called.

This is not a bug in the compiler, this is required behaviour. To remove the redundant memset, you could just write A a;. In this case a is default-initialized and no automatic zeroing occurs, with or without the user-provided constructor.

† This is important since A a() will be parsed as a function called a with return type A

Passer By
  • 19,325
  • 6
  • 49
  • 96
  • 1
    Take a look at the class B, it's a standard perfect-forwarding. If the class A does not have a constructor, the B also call memset. How to workaround it? And, please move to the url: https://stackoverflow.com/questions/45072486/why-allocate-shared-and-make-shared-so-slow , I found std::allocate_shared also do this useless memset and I do not know how to workaround it. – alpha Jul 14 '17 at 09:48
  • @alpha What do you mean move to the url. If the questions are duplicate, you are doing things wrong. If they are not, __don't ask multiple questions in one post__ – Passer By Jul 14 '17 at 09:50
  • @alpha `B` calls `a_()` in its constructor, this is covered __directly__ in the answer why `memset` is needed here. – Passer By Jul 14 '17 at 09:51
  • I first meet the performance issue described by that url, and then I diged for some days, finally I found out it's because of the memset. It's not duplicate question. Even after I understand the value-initializing, I still do not know how to solve that issue. – alpha Jul 14 '17 at 09:55
  • If I change the define of A as struct A { A()=default; char data_[0x10000]; }; I found the compiler still call memset. Why? The compiler should generate a dummy constructor for A? – alpha Jul 18 '17 at 08:14
  • @alpha In that case, if `A` is value-initialized (as in `A a(t...);` above), then `A` is just zero-initialized, its constructor isn't called since it is also trivial. This part of C++ is just tedious to work through, I'd suggest just provide the exact constructor that you want to call to sidestep all this. – Passer By Jul 19 '17 at 15:03
1

Doesn't this explain it?

We can see that:

Zero initialization is performed [...] as part of value-initialization sequence for [...] members of value-initialized class types that have no constructors, including value initialization of elements of aggregates for which no initializers are provided.

...

Value initialization is performed [...] when a non-static data member or a base class is initialized using a member initializer with an empty pair of parentheses or braces (since C++11);

So putting a_() in the member initializer list falls into the latter case, that as a result invokes zero initialization of the array.

To answer your question: to me this seems to be a standard behavior not a compiler bug.

K. Kirsz
  • 1,384
  • 10
  • 11