In-depth exploration of empty Base Class Optimization EBO

Source: Internet
Author: User

I. background of EBO
We know that an empty class contains no non-static data members and no virtual pointers (including pointers to virtual function tables and virtual base class sub-objects ), its size is usually 1. Of course, in some alignment requirements, the system may be another number (usually 4). If the empty class is inherited, what will happen to the size of the derived class? A compiler that supports the C ++ standard and EBO will optimize the null base class, that is, it will not allocate space to null base class sub-objects. In other words, the address of the null base class sub-object is the same as the address of its derived class instance. From the perspective of compiler implementation, we need to consider different situations during inheritance. In this case, P indicates the parent class, C indicates the subclass, the circle indicates the empty class, And the rectangle indicates the non-empty class. Shows the situation of single-inheritance EBO

The EBO-1 reflects the null class derived from the null base class, the EBO-2 reflects the non-empty class derived from the null base class, the EBO-3, The EBO-4 reflects in the inheritance chain, can the optimization of the null base class be passed to future generations. Shows how to inherit multiple EBO instances.


The EBO-5 reflects the empty class derived from two empty base classes, the EBO-6 reflects the non-empty class derived from two empty base classes, the EBO-6 represents a null class derived from a non-empty base class and an empty base class, and a non-empty class derived from a non-empty base class and an empty base class. In the above eight cases, whether single inheritance or multi-inheritance, a compiler that fully supports EBO should be able to optimize all the null base classes.

II. Application of EBO
The empty base class optimization technology saves unnecessary space for objects and improves operation efficiency. Therefore, it has become the cornerstone of some powerful technologies, this service is applicable to the reuse of Type Definition classes such as binary_function, unary_function, iterator, and iterator_traits in stl. It is also applicable to the reuse of strategy classes such as memory management and multi-thread secure synchronization. When a class has a data member of an empty class type, you can use EBO to optimize the object layout. The code description is as follows:
1 template <typename T1, typename T2>
2 class EBO
3 {
4 private:
5 T1 m_t1;
6 T2 m_t2;
7 };
When T1 and T2 are empty classes, the following improvements can be made:

1 template <typename T1, typename T2>
2 class EBO: T1, T2
3 {
4 };
For further extension, if T1 or T2 is of a non-class type, such as a basic built-in type or function pointer, or if T1 and T2 are of the same type, directly inheriting them will cause compilation errors, what should we do? You can add an intermediate layer to solve the problem. The Code is as follows:
1 template <typename T1, typename T2, bool isSame, bool isFirstEmpty, bool isSecondEmpty>
2 class EBO_IMPL;
3
4 template <typename T1, typename T2>
5 class EBO_IMPL <T1, T2, false>
6 {
7 T1 m_t1;
8 T2 m_t2;
9 };
10
11 template <typename T1, typename T2>
12 class EBO_IMPL <T1, T2, false, true, true>: T1, T2
13 {
14 };
15
16 template <typename T1, typename T2>
17 class EBO_IMPL <T1, T2, false, true, false>: T1
18 {
19 T2 m_t2;
20 };
21
22 template <typename T1, typename T2>
23 class EBO_IMPL <T1, T2, false, false, true>: T2
24 {
25 T1 m_t1;
26 };
27
28 template <typename T1, typename T2>
29 class EBO_IMPL <T1, T2, true, false, false>
30 {
31 T1 m_t1;
32 T2 m_t2;
33 };
34
35 template <typename T1, typename T2>
36 class EBO_IMPL <T1, T2, true>: T1
37 {
38 T2 m_t2;
39 };
40
41 template <typename T1, typename T2>
42 class EBO: EBO_IMPL <T1, T2, boost: is_same <T1, T2 >:: value, boost: is_empty <T1 >:: value, boost :: is_empty <T2 >:: value>
43 {
44 };
For ease of implementation, the above Code directly uses the is_same and is_empty functions in boost to determine the type attributes. In fact, boost has implemented the EBO selection and application tool, namely the compressed_pair class template, after studying the source code, we can find that the tool fully considers the actual types of T1 and T2, and is_empty uses sizeof to determine the type size. After compressed_pair is replaced, the Code is as follows:
1 template <typename T1, typename T2>
2 class EBO: boost: compressed_pair <T1, T2>
3 {
4 };

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.