Are std::fill, std::copy specialized for std::vector?

后端 未结 4 1919
一整个雨季
一整个雨季 2020-12-31 07:14

When thinking about this question I start to wondering if std::copy() and/or std::fill are specialized (I really mean optimized) for std::vec

4条回答
  •  执念已碎
    2020-12-31 07:53

    23.2.5 Class vector from the C++ International Standard goes as far as to tell us

    To optimize space allocation, a specialization of vector for bool elements is provided:

    after which the bitset specialization is provided. That's as far as the standard goes regarding vector, vendors need to implement it using a bitset to optimize for space. Optimizing for space comes with a cost here, as to not optimize for speed.

    It's easier to get a book from the library than it is to find a book if it were between all the library books stapled closely together in containers....


    Take your example, you're trying to do a std::fill or std::copy from begin to end. But that's not always the case, sometimes it doen't just simply map to an entire byte. So, that's a bit of a problem in terms of speed optimization. It's easy for the case you'd have to change every bit to one, that's just changing the bytes to 0xF, but that's not the case here; it becomes much harder if you were to only changes certain bits of a byte. Then you'll need to actually compute what the byte will be; that's not a trivial thing to do*, or at least not as an atomic operation on current hardware.

    It's the premature optimization story, it's nice in terms of space but horrible in terms of performance.

    Is having a "is a multiple of 8 bits" check worth the overhead? I doubt it.

    * We're talking about multiple bits here, for the case it's just one bit you can of course do a bit operation.

提交回复
热议问题