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

后端 未结 7 1483
天涯浪人
天涯浪人 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:59

    I was always wondering about immutable cows: once cow is created I could be changed only through assignment from another cow, hence it will be compliant with the standard.

    I had time to try it today for a simple comparison test: a map of size N keyed by string/cow with every node holding a set of all strings in the map (we have NxN number of objects).

    With strings sized ~300 bytes and N=2000 cows are slightly faster and use almost order of magnitude less memory. See below, sizes are in kbs, run b is with cows.

    ~/icow$ ./tst 2000
    preparation a
    run
    done a: time-delta=6 mem-delta=1563276
    preparation b
    run
    done a: time-delta=3 mem-delta=186384
    

提交回复
热议问题