public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "sebor at roguewave dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/31176]  New: reorder class data members to minimize space waste
Date: Wed, 14 Mar 2007 17:37:00 -0000	[thread overview]
Message-ID: <bug-31176-1186@http.gcc.gnu.org/bugzilla/> (raw)

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


             reply	other threads:[~2007-03-14 17:37 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-14 17:37 sebor at roguewave dot com [this message]
2007-03-14 18:15 ` [Bug c++/31176] " fang at csl dot cornell dot edu
2007-03-14 18:16 ` pinskia at gcc dot gnu dot org
2007-03-14 19:04 ` sebor at roguewave dot com
2007-03-14 19:06 ` sebor at roguewave dot com
2007-03-14 19:09 ` pinskia at gcc dot gnu dot org
2007-03-15 19:54 ` sebor at roguewave dot com
2007-03-15 19:55 ` sebor at roguewave dot com
2007-03-15 23:51 ` sebor at roguewave dot com
2007-04-28  0:37 ` pinskia at gcc dot gnu dot 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-31176-1186@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: link
Be 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).