public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/112666] Missed optimization: Value initialization zero-initializes members with user-defined constructor Date: Wed, 22 Nov 2023 11:40:37 +0000 [thread overview] Message-ID: <bug-112666-4-h5ftlA3DNy@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-112666-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112666 --- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Francisco Paisana from comment #0) > The struct "C" which is just "B" and an int is much slower at being > initialized than B when value initialization (via {}) is used. However, my > understanding of the C++ standard is that members with a user-defined > default constructor do not need to be zero-initialized in this situation. I think that's not quite right. Types with a user-provided default constructor will not be zero-initialized when value-init is used. B does have a user-provided default constructor, so value-init for an object of type B does not perform zero-init first. But that applies when constructing a complete B object, not when constructing a member subobject. C does not have a user-provided default constructor, so value-initialization means: "- the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;" So first it's zero-initialized, which means: "- if T is a (possibly cv-qualified) non-union class type, its padding bits (6.8.1) are initialized to zero bits and each non-static data member, each non-virtual base class subobject, and, if the object is not a base class subobject, each virtual base class subobject is zero-initialized;" This specifically says that *each non-static data member ... is zero-initialized." So the B subobject must be zero-initialized. That's not the same as when you value-init a B object. > Looking at the godbolt assembly output, I see that both `A a{}` and `C c{}` > generate a memset instruction, while `B b{}` doesn't. Clang, on the other > hand, seems to initialize C almost as fast as B. I don't know whether Clang considers the zero-init to be dead stores that are clobbered by B() and so can be eliminated, or something else. But my understanding of the standard is that requiring zero-init of B's members is very intentional here. > This potentially missed optimization in gcc is particularly nasty for > structs with large embedded storage (e.g. structs that contain C-arrays, > std::arrays, or static_vectors). Arguably, the problem here is that B has a default ctor that intentionally leaves members uninitialized. If you want to preserve that behaviour in types that contain a B subobject, then you also need to give those types (e.g. C in your example) a user-provided default ctor.
next prev parent reply other threads:[~2023-11-22 11:40 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-11-22 8:58 [Bug c++/112666] New: " paisanafc at gmail dot com 2023-11-22 11:40 ` redi at gcc dot gnu.org [this message] 2023-11-23 15:05 ` [Bug c++/112666] " paisanafc at gmail dot com 2023-11-23 15:08 ` paisanafc at gmail dot com 2023-11-23 15:37 ` paisanafc at gmail dot com 2023-11-23 16:34 ` redi at gcc dot gnu.org 2023-11-23 16:35 ` sjames at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-112666-4-h5ftlA3DNy@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).