- Hands-On High Performance Programming with Qt 5
- Marek Krajewski
- 194字
- 2021-07-02 13:54:07
Relocatability
The second optimization used in Qt containers is the tagging of data types with type info to perform some optimizations, just as STL would use memcpy for data structures which are trivially copyable (see the std::is_trivially_copyable<> trait).
Qt has three type traits that can be used: Q_PRIMITIVE_TYPE for types without a constructor and destructor, Q_MOVABLE_TYPE for types which need constructors but can be relocated by bitwise copy, and Q_COMPLEX_TYPE for all the rest. By default, your custom types are complex and if you want the relocation optimization to be enabled, you have to tag the type using the following macro:
Q_DECLARE_TYPEINFO(MyType, Q_MOVABLE_TYPE);
What is the benefit of a relocatable type? For example, if we had an array of relocatable objects we would need to call copy constructors for each of them in case of array resizing, but we would just call memmove a single time. In standard C++, there is a similar trait proposed for C++20, namely, is_trivially_relocatable_v<T> and a corresponding [[trivially_relocatable]] attribute.
Now, having learned the most important things about the general design of Qt containers, let's have a look at the most used ones.