5

I just learned about padding, and I was trying to do some tests about it, I tried to pack this struct :

struct B {
        int a,b,c;
        string s;
        char x;
        string t;
        char y;
        string u;

}__attribute__((packed)) ;

But I get this Warning :

warning: ignoring packed attribute because of unpacked non-POD field 'std::string B::u'
      string u;

Does this mean that structs containing strings cannot be packed ? Is there any other way to do it ? if so does it affect the performance ?

Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36
  • _"Does this mean that strings cannot be packed ?"_ Obviously they can't. You probably have a misconception what C++ `std::strings` actually are. They are different from `char[]` arrays. – πάντα ῥεῖ Apr 03 '15 at 07:59
  • I didn't mean to pack the string, I meant to pack a struct containing a string – Othman Benchekroun Apr 03 '15 at 08:06
  • 1
    `std::string` objects will contain pointers that - depending on the CPU - may need to be aligned for optimal performance, or simply to avoid a crash. Other classes can't come along and retrospectively modify their alignment needs while packing them in. – Tony Delroy Apr 03 '15 at 08:07
  • What is then the best way to order the attributes when there is a string ? – Othman Benchekroun Apr 03 '15 at 08:09
  • 5
    Why would you want to use packed in the first place? What are you trying to obtain? For best performance leave the layout to the compiler except if you have a very good reason to take control yourself. – Support Ukraine Apr 03 '15 at 08:15
  • I am working on a research project, I have to optimize the usage of memory of a huge software, I trying to see if this can help, however it must not have a worse performance than before – Othman Benchekroun Apr 03 '15 at 08:24
  • @Othman - If you have many structs with mixed char, int, char, etc you may save some memory by using packed. However, unligned data will often cause a performance hit (system dependent). – Support Ukraine Apr 03 '15 at 08:42
  • Are you sure of that ?? I am doing some tests about that, and It's showing that the structs with `packed` attribute are performant... Can you give me an example of how it can cause a performance hit ? – Othman Benchekroun Apr 03 '15 at 08:57
  • @Othman - I did not state that packed will always cause a performance hit! It is system dependent. I would however expect a performance hit on most systems. If your structs are packed by nature, you will of cause not see any performance hit. If the number of structs you are dealing with are relative small, I would suggest that you reorganized them manually and avoided packed. – Support Ukraine Apr 03 '15 at 09:08
  • Can you tell more why I shouldn't try packing ?? Because I will have to create a gcc plugin, and it's not an easy task – Othman Benchekroun Apr 03 '15 at 09:27
  • 1
    @Othman: Unaligned data access might impact your performance for various reasons: first, the instruction might take more time (not sure whether this is the case for x86/x64) and/or your data might end up in two separate cache lines. – MikeMB Apr 03 '15 at 10:04
  • 1
    On the other hand, it might improve your performance if you e.g. have to iterate over a very large array of those structs and - due to a denser packing - less data has to be loaded from main memory. – MikeMB Apr 03 '15 at 10:06

1 Answers1

3

A good rule of thumb is to sort your members from biggest to smallest. That way your data is aligned and (usually) has no gaps. E.g. on VS2013 for an x64 target the following layout requires 112 instead of 128 bytes:

struct B {  
    string s,t,u; 
    int a,b,c;    
    char x,y;
};

For an x86 target however, this only saves you 4 bytes. Whether or not and how this impacts your performance depends on so many other factors, that it can only be determined by measurement.

MikeMB
  • 20,029
  • 9
  • 57
  • 102
  • Thanks I can see why that is a good rule, but is there any compilers that can reorder the structs like that ?? Because I think I will create a gcc extension for that, but if it already exists, better not lose my time on that – Othman Benchekroun Apr 03 '15 at 09:00
  • 1
    @Othman: The C++ standard doesn't allow the compiler to reorder member variables in general, see http://stackoverflow.com/questions/916600/can-a-c-compiler-re-order-elements-in-a-struct – amdn Apr 03 '15 at 09:13
  • @amdn: Thanks! I thought so too, but wasn't quite sure. – MikeMB Apr 03 '15 at 10:06