Skip to content

Experimental meta MU for class/structure attributes 'sizeof' optimization

Notifications You must be signed in to change notification settings

marek094/attr-mu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

attr-MU

Experimental meta MU containing both aligned and size optimized structure mu::named_tuple<...>.

Demo

using point = named_tuple<
    attr<"x"_mu, double>,
    attr<"y"_mu, double>,
    attr<"z"_mu, double>,
    attr<"n a m e"_mu, std::string>
>;

point p1 { 1.5, 3.5, 0.5, "/src/img/point.jpg"s };
point p2 { make_attr<"x"_mu>(2.5), make_attr<"y"_mu>(2.6), make_attr<"z"_mu>(0.0) };

cout << p1.at<"n a m e"_mu>() << endl; // `/src/img/point.jpg`
cout << get<"x"_mu>( p2 ) << endl;     // `2.5`

Motivation

Consider example:

using size24 = long long[3];
//using size24 = std::string;

struct bad_S { char sign; size24 name; bool is_man; long double weight; };

struct nice_S { size24 name; char sign; bool is_man; long double weight; };

struct packed_S { char sign; size24 name; bool is_man; long double weight;
} __attribute__((packed));

struct packed_aligned_S { char sign; size24 name; bool is_man; long double weight;
}__attribute__((packed, aligned));

using tuple_S = std::tuple<char, size24, bool, long double>;

using named_tuple_S = mu::named_tuple<
    mu::attr<"sign"_mu, char>,
    mu::attr<"name"_mu, size24>,
    mu::attr<"is_man"_mu, bool>,
    mu::attr<"weight"_mu, long double>
>;

The main point is that the order of attributes in declaration of class affects size of the structure in C++, so we want to introduce something like attr_mu::packed_tuple<...> with named access to its elemets.

Statistics (clang 9.0.0. / gcc 5.4.1):

+-----------------------------------------+
| type            | sizeof [B] | alignof  |
+-----------------------------------------+
| size24          | 24         | 8        |
| bool            | 1          | 1        |
| char            | 1          | 1        |
| long double     | 16         | 16       |
+-----------------------------------------+
| bad_S           | 64         | 16       |
| nice_S          | 48         | 16       |
| packed_S        | 42         | 1        |
| packed_aligned_S| 48         | 16       |
| tuple_S         | 64         | 16       |
+-----------------------------------------+
| named_tuple_S   | 48         | 16       |
+-----------------------------------------+

Moreover when we set using size24 = std::string with gcc we obtain: warning: ignoring packed attribute because of unpacked non-POD field 'std::string packed_aligned_S::name'.

See: suggestion __attribute__ ((reorder))

About

Experimental meta MU for class/structure attributes 'sizeof' optimization

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages