From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9A4B63854812; Fri, 5 Feb 2021 17:14:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A4B63854812 From: "andysem at mail dot ru" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/98978] New: Consider packing _M_Engaged in the tail padding of T in optional<> Date: Fri, 05 Feb 2021 17:14:39 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: andysem at mail dot ru X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Feb 2021 17:14:39 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98978 Bug ID: 98978 Summary: Consider packing _M_Engaged in the tail padding of T in optional<> Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: andysem at mail dot ru Target Milestone: --- Using std::optional with some types may considerably increase object sizes since it adds alignof(T) bytes worth of overhead. Sometimes it is possible = to avoid this overhead if the flag indicating presence of the stored value (_M_Engaged in libstdc++ sources) is placed in the tail padding of the T object. This can be done if std::optional constructs an object of a type th= at derives from T, which has an additional bool data member that is initialize= d to true upon construction. The below code roughly illustrates the idea: template< typename T > struct _Optional_payload_base { struct _PresentT : T { const bool _M_Engaged =3D true; // Forwarding ctors and other members }; static constexpr size_t engaged_offset =3D offsetof(_PresentT, _M_Engaged= ); struct _AbsentT { unsigned char _M_Offset[engaged_offset]; const bool _M_Engaged =3D false; }; union _Storage { _AbsentT _M_Empty; _PresentT _M_Value; _Storage() : _M_Empty() {} // Forwarding ctors and other members }; _Storage _M_payload bool is_engaged() const noexcept { return *reinterpret_cast< const bool* >(reinterpret_cast< const unsigned char* >(&_M_payload) + engaged_offset); } }; The above relies on some implementation details, such as: - offsetof works for the type T. It does for many types in gcc, beyond what= is required by the C++ standard. Maybe there is a way to avoid offsetof, I just didn't immediately see it. - The location of _M_Engaged in both _PresentT and _AbsentT is the same. Th= is is a property of the target ABI, and AFAICS it should be true at least on x= 86 psABI and I think Microsoft ABI. The above will only work for non-final class types, for other types, and wh= ere the above requirements don't hold true, the current code with a separate _M_Engaged flag would work.=