Why does `QDateTime` implement implicit sharing?

Viewed 121

According to the documentation implicit sharing is done mostly for performance (avoiding copies and automatic copy-on-change).

Why is it implemented for the class QDateTime that should apparently be just a small amount of bytes? I'd say that the cost of heap allocation and thread synchronization for reference counting update would exceed by a lot the small gain of not copying a few bytes.

1 Answers

Short answer: It is cheaper.

Long answer: Qt is designed around the PIMPL pattern. Creating a real copy of an object means that you have to allocate new heap for the PIMPL and copy both. Especially the new allocation is rather expensive for small objects. To overcome this problem they decided to implement copy-on-write for many even small classes.

Related