Legality of COW std::string implementation in C++11

后端 未结 7 1384
天涯浪人
天涯浪人 2020-11-22 09:30

It had been my understanding that copy-on-write is not a viable way to implement a conforming std::string in C++11, but when it came up in discussion recently I

7条回答
  •  花落未央
    2020-11-22 09:43

    It is, CoW is an acceptable mechanism for making faster strings... but...

    it makes multithreading code slower (all that locking to check if you're the only one writing kills performance when using a lot of strings). This was the main reason CoW was killed off years ago.

    The other reasons are that the [] operator will return you the string data, without any protection for you to overwrite a string someone else expects to be unchanging. The same applies to c_str() and data().

    Quick google says that the multithreading is basically the reason it was effectively disallowed (not explicitly).

    The proposal says :

    Proposal

    We propose to make all iterator and element access operations safely concurrently executable.

    We are increasing the stability of operations even in sequential code.

    This change effectively disallows copy-on-write implementations.

    followed by

    The largest potential loss in performance due to a switch away from copy-on-write implementations is the increased consumption of memory for applications with very large read-mostly strings. However, we believe that for those applications ropes are a better technical solution, and recommend a rope proposal be considered for inclusion in Library TR2.

    Ropes are part of STLPort and SGIs STL.

提交回复
热议问题