程序员什么时候使用空基优化(EBO)

我正在阅读有关空基优化 (EBO) 的内容.在阅读的过程中,我的脑海中浮现出以下问题:

  1. 当它对派生类没有任何贡献(无论是功能方面还是数据方面)时,使用 Empty 类作为基类有什么意义?

  2. 在 这篇文章,我读了这个:

<块引用>

//S为空
类结构 T : S
{
int x;
};

[...]

请注意,我们没有丢失任何数据或代码准确性:当您创建一个S 类型的独立对象,对象的大小仍然是 1(或更多)作为前;仅当 S 用作基数时另一个类的类做它的记忆足迹缩小到零.意识到这种节省的影响,想象一下包含 125,000 个的向量对象.仅 EBO 就可节省一半兆字节内存!

这是否意味着如果我们不使用S"作为T"的基类,我们必然会消耗两倍的内存吗?我认为,这篇文章比较了两种我认为不正确的不同场景.

我想知道当 EBO 被证明有用时的真实场景.(意思是,在同一场景中,如果我们不使用 EBO,我们必然会不知所措!).

请注意,如果您的回答包含这样的解释:

重点是一个空类的大小不为零,但是在派生或派生时它的大小可以为零,那么我不是问这个,因为我已经知道了.我的问题是,首先为什么有人会从一个空类派生他的类?即使他不派生并简单地编写他的类(没有任何空基),他是否有任何损失?

解决方案

EBO 在 基于策略的设计,您通常私下从多个策略类继承.如果我们以线程安全策略为例,可以想象伪代码:

class MTSafePolicy{民众:void lock() { mutex_.lock();}void unlock() { mutex_.unlock();}私人的:互斥互斥_;};类 MTUnsafePolicy{民众:void lock() {/* 无操作 */}void unlock() {/* 无操作 */}};

给定一个基于策略的设计类,例如:

template类测试:ThreadSafetyPolicy{/* ... */};

使用带有 MTUnsafePolicy 的类不会增加类 Test 的大小开销:这是一个完美的例子,不要为你没有的东西买单使用.

I was reading about Empty Base Optimization(EBO). While reading, the following questions popped up in my mind:

  1. What is the point of using Empty class as base class when it contributes nothing to the derived classes (neither functionality-wise, nor data-wise)?

  2. In this article, I read this:

//S is empty
class struct T : S
{
??????int x;
};

[...]

Notice that we didn’t lose any data or code accuracy: when you create a standalone object of type S, the object’s size is still 1 (or more) as before; only when S is used as base class of another class does its memory footprint shrink to zero. To realize the impact of this saving, imagine a vector that contains 125,000 objects. The EBO alone saves half a megabyte of memory!

Does it mean that if we don't use "S" as base class of "T", we would necessarily consume double of megabyte of memory? I think, the article compares two different scenarios which I don't think is correct.

I would like to know a real scenario when EBO can proven to be useful.(means, in the same scenario, we would necessarily be at loss IF we don't use EBO!).

Please note that if your answer contains explanations like this :

The whole point is that an empty class has non-zero size, but when derived or deriving it can have zero size, then I'm NOT asking that, as I know that already. My question is, why would anyone derive his class from an empty class in the first place? Even if he doesn't derive and simply writes his class (without any empty base), is he at loss in ANY way?

解决方案

EBO is important in the context of policy based design, where you generally inherit privately from multiple policy classes. If we take the example of a thread safety policy, one could imagine the pseudo-code :

class MTSafePolicy
{
public:
  void lock() { mutex_.lock(); }
  void unlock() { mutex_.unlock(); }

private:
  Mutex mutex_;
};

class MTUnsafePolicy
{
public:
  void lock() { /* no-op */ }
  void unlock() { /* no-op */ }
};

Given a policy based-design class such as :

template<class ThreadSafetyPolicy>
class Test : ThreadSafetyPolicy
{
  /* ... */
};

Using the class with a MTUnsafePolicy simply add no size overhead the class Test : it's a perfect example of don't pay for what you don't use.

相关文章