This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/31176] New: reorder class data members to minimize space waste


While discussing how today's C++ compilers lay out data members of structs and
classes on the C++ committee's mailing list the observation was made that no
known implementation, including gcc, takes advantage of the permission to
rearrange members declared with their own access specifier (see
c++std-core-11977). For example, the data members of the following struct are
laid out in declaration order even though a more space efficient order is
possible:

    struct S { public: char c; public: int i; public: char d; };

This problem is especially hard to solve in template code where the sizes of
one or more data members are not known, such as:

    template <class T, class U, class V>
    struct Triple { public: T a; public: U b; public: V c; };

Since reordering of existing structs would introduce a binary incompatibility I
would like to propose that a mechanism be provided whereby authors of new code
can mark up their types and/or data members in order to permit the compiler to
rearrange them in a space efficient manner, until gcc implements a new ABI
where the reordering algorithm becomes the default.

For example, a new type and/or variable attribute could be added (call it
reorder), that could be used to mark up types and/or data members to
participate in the reordering. To allow the compiler to arrange Triple members
in an efficient way the template would be marked up as follows:

    template <class T, class U, class V>
    struct Triple __attribute__ ((reorder)) {
       public: T a; public: U b; public: V c;
    };

or, alternatively, like so:

    template <class T, class U, class V>
    struct Triple {
       public: T a __attribute__ ((reorder));
       public: U b __attribute__ ((reorder));
       public: V c __attribute__ ((reorder));
    };

The order of members declared in the same section (introduced and closed by an
access specifier) without the attribute would not participate in the reordering
with one another.

Members of reordered aggregates would be initialized in declaration order.


-- 
           Summary: reorder class data members to minimize space waste
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sebor at roguewave dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31176


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]