c++stringfolly

Max capacity for FBString?


For FBString, max_size() simply returns std::numeric_limits<size_type>::max(). However, the higher two bits of capacity_ in struct MediumLarge is used to denotes the type of FBString(small/medium/large), which means the max of capacity_ will be 2^62-1(64-bit processor), it's less than the max value of size_t. Do I misunderstand the implementation or is this actually a bug?

  struct MediumLarge {
    Char* data_;
    size_t size_;
    size_t capacity_;

    size_t capacity() const {
      return kIsLittleEndian ? capacity_ & capacityExtractMask : capacity_ >> 2;
    }

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

Solution

  • Since the documentation for folly::FBString claims to be:

    100% compatible with std::string

    it actually seems to be a bug (in my opinion).

    BTW, for large strings, FBString applies copy-on-write (COW), which also breaks the compatibility with std::string (since C++11).

    See Legality of COW std::string implementation in C++11 for more details.

    I would guess that they simply do not care about strings of unrealistic lengths (nowadays). The incompatibility due to COW might be much more severe.