简体   繁体   English

布尔标志与未签名的字符标志

[英]Bool Flags vs. Unsigned Char Flags

Disclaimer: Please correct me in the event that I make any false claims in this post. 免责声明:如果我在此帖子中有任何虚假声明,请纠正我。

Consider a struct that contains eight bool member variables. 考虑一个包含八个布尔成员变量的结构。

/*
 * Struct uses one byte for each flag.
 */
struct WithBools
{
  bool f0 = true;
  bool f1 = true;
  bool f2 = true;
  bool f3 = true;
  bool f4 = true;
  bool f5 = true;
  bool f6 = true;
  bool f7 = true;
};

The space allocated to each variable is a byte in length, which seems like a waste if the variables are used solely as flags. 分配给每个变量的空间的长度为一个字节,如果仅将变量用作标志,这似乎是一种浪费。 One solution to reduce this wasted space, as far as the variables are concerned, is to encapsulate the eight flags into a single member variable of unsigned char. 就变量而言,一种减少此浪费空间的解决方案是将八个标志封装到一个无符号char的单个成员变量中。

/*
 * Struct uses a single byte for eight flags; retrieval and
 * manipulation of data is achieved through accessor functions.
 */
struct WithoutBools
{
  unsigned char getFlag(unsigned index)
  {
    return flags & (1 << (index % 8));
  }
  void toggleFlag(unsigned index)
  {
    flags ^= (1 << (index % 8));
  }

private:
  unsigned char flags = 0xFF;
};

The flags are retrieved and manipulated via. 这些标志是通过检索和操纵的。 bitwise operators, and the struct provides an interface for the user to retrieve and manipulate the flags. 按位运算符,该结构为用户提供了检索和操纵标志的接口。 While flag sizes have been reduced, we now have the two additional methods that add to the size of the struct. 在减小标志大小的同时,我们现在有两个附加的方法可以增加结构的大小。 I do not know how to benchmark this difference, therefore I could not be certain of any fluctuation between the above structs. 我不知道如何对这种差异进行基准测试,因此无法确定上述结构之间是否存在任何波动。

My questions are: 我的问题是:

1) Would the difference in space between these two structs be negligible? 1)这两个结构之间的空间差异可以忽略不计吗?

2) Generally, is this approach of "optimising" a collection of bools by compacting them into a single byte a good idea? 2)通常,这种通过将布尔值压缩为单个字节来“优化”布尔值集合的方法是一个好主意吗? Either in an embedded systems context or otherwise. 要么在嵌入式系统环境中,要么在其他方面。

3) Would a C++ compiler make such an optimisation that compacts a collection of bools wherever possible and appropriate. 3) C ++编译器是否会进行这种优化,以便在可能和适当的情况下压缩一组布尔变量。

Would the difference in space between these two structs be negligible? 这两个结构之间的空间差异可忽略不计吗?

That depends on how many values you are storing and how much space you have to store them in. The size difference is 1 to 8. 这取决于要存储多少值以及必须在其中存储多少空间。大小差为1到8。

Generally, is this approach of "optimising" a collection of bools by compacting them into a single byte a good idea? 通常,通过将它们压缩为单个字节来“优化”布尔集的方法是一个好主意吗? Either in an embedded systems context or otherwise. 要么在嵌入式系统环境中,要么在其他方面。

Again, it depends on how many values and how much space. 同样,它取决于多少个值和多少空间。 Also note that dealing with bits instead of bytes increases code size and execution time. 另请注意,使用位而不是字节来处理会增加代码大小和执行时间。

Many embedded systems have relatively little RAM and plenty of Flash. 许多嵌入式系统具有相对较少的RAM和大量的Flash。 Code is stored in Flash, so the increased code size can be ignored, and the saved memory could be important on small RAM systems. 代码存储在Flash中,因此可以忽略增加的代码大小,并且节省的内存在小型RAM系统上可能很重要。

Would a C++ compiler make such an optimisation that compacts a collection of bools wherever possible and appropriate. C ++编译器是否会进行这种优化,以便在可能和适当的情况下压缩一组布尔变量。

Hypothetically it could. 假设可以。 I would consider that an aggressive space optimization, at the expense of execution time. 我认为这是一种积极的空间优化,但会浪费执行时间。

STL has a specialization for vector<bool> that I frequently avoid for performance reasons - vector<char> is much faster. STL具有vector<bool>的特殊化,出于性能原因,我经常避免使用它vector<char>更快。

we now have the two additional methods that add to the size of the struct 现在,我们有两个其他方法来增加结构的大小

Methods are code and do not increase the size of the struct. 方法是代码,不会增加结构的大小。 Only data makes up size on the structure. 只有数据会构成结构的大小。

3) Would a C++ compiler make such an optimisation that compacts a collection of bools wherever possible and appropriate. 3)C ++编译器是否会进行这种优化,以便在可能和适当的情况下压缩一组布尔变量。

That is a sound resounding no. 那是一个响亮的声音。 The compiler is not allowed to change data types. 不允许编译器更改数据类型。

1) Would the difference in space between these two structs be negligible? 1)这两个结构之间的空间差异可以忽略不计吗?

No, there definitely is a size difference between the two approaches. 不,两种方法之间肯定存在大小差异。

2) Generally, is this approach of "optimising" a collection of bools by compacting them into a single byte a good idea? 2)通常,这种通过将布尔值压缩为单个字节来“优化”布尔值集合的方法是一个好主意吗? Either in an embedded systems context or otherwise. 要么在嵌入式系统环境中,要么在其他方面。

Generally yes, the idiomatic way to model flags is with bit-wise manipulation inside an unsigned integer. 通常是的,对标记进行建模的惯用方式是在无符号整数内进行按位操作。 Depending on the number of flags needed you can use std::uint8_t , std::uint16_t and so on. 根据所需标志的数量,您可以使用std::uint8_tstd::uint16_t等。

However the most common way to model this is not via index as you've done, but via masks. 但是,最常见的建模方法不是通过索引,而是通过蒙版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM