当程序员使用空基本优化(EBO) [英] When do programmers use Empty Base Optimization (EBO)

查看:213
本文介绍了当程序员使用空基本优化(EBO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于空基本优化(EBO)。阅读时,我脑海里浮现出以下问题:


  1. 使用Empty类作为基类的意义是什么?


  2. 本文,我阅读:





// S是空的
class struct T:S

{
       int x;

};



请注意,我们没有丢失任何数据或
代码准确性:当您创建一个
类型为S的独立对象时,
对象的大小仍然是1(或更多)为
before;只有当S用作基础
另一个类的类做它的内存
足迹收缩到零。为了实现
这个保存的影响,想象一个
向量包含125,000
个对象。 仅EBO可节省半美元b $ b兆字节的内存!


不要使用S作为T的基类,我们必然消耗双倍的内存?我认为,文章比较了两个不同的情况,我不认为是正确的。



我想知道一个真正的场景,当EBO可以证明是有用的。意味着,在同一场景中,如果我们不使用EBO,我们将必然会处于亏损状态。



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



总的来说,一个空类具有非零大小,但是当派生或派生时,它可以具有零大小,不要问,因为我知道已经。我的问题是,为什么会有人从一个空的类中得到他的类? 即使他没有得出并且只写了他的类(没有任何空基),他是否以任何方式失去了?

解决方案

EBO在基于策略的设计的上下文中很重要,您通常可以继承 如果我们以线程安全策略为例,可以想象伪代码:

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

private:
Mutex mutex_;
};

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

基于策略的设计类如:

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

使用带有 MTUnsafePolicy 没有尺寸开销类测试:这是一个完美的例子不支付你不使用


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.

这篇关于当程序员使用空基本优化(EBO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆