big endian and little endian in folly of Facebook

258 views Asked by At

I have read the code of folly created by Facebook,In this page https://github.com/facebook/folly/blob/master/folly/FBString.h ,I found that author considers big endian and little endian when he set some value, such as capacity_ , The code is as follows:

void setCapacity(size_t cap, Category cat) {
  capacity_ = kIsLittleEndian
      ? cap | (static_cast<size_t>(cat) << kCategoryShift)
      : (cap << 2) | static_cast<size_t>(cat);
}

I want to know why author should consider big endian and little endian, I think we don't need to consider them in the same machine, geting and settting value are dealed with by the machines and we can ignore them

1

There are 1 answers

0
pdpi On

That string implementation has some cleverness around how it allocates memory depending on string size. Notably here you can find where a union is used to swap between strategies.

On a 64-bit machine with 8-bit characters, the MediumLarge struct is 24 bytes long, and can hold 24 chars. Two bits from the last byte are reserved for determining the storage strategy, though, so short strings can be up to 23 chars long.

It's that "last byte" thing that justifies the need to worry about endianness: that "last byte" is the highest address, and, therefore, on a little endian machine, and those flags are stored in the two most significant bits, and you can extract the capacity length by masking off those two bits. On big-endian, the last byte is the least significant byte, you store the flags in the two least significant bits, and you can extract the capacity by doing a 2-bit shift to the right.

Now, the fact that the code uses kIsLittleEndian and the conditional operator to swap between these behaviours seems to suggest these checks are happening at run-time. However, kIsLittleEndian is declared as constexpr, and all conditionals on it can be evaluated at compile time.