Trusted Software Excellence across Desktop and Embedded
Take a glance at the areas of expertise where KDAB excels ranging from swift troubleshooting, ongoing consulting and training to multi-year, large-scale software development projects.
Find out why customers from innovative industries rely on our extensive expertise, including Medical, Biotech, Science, Renewable Energy, Transportation, Mobility, Aviation, Automation, Electronics, Agriculture and Defense.
High-quality Embedded Engineering across the Stack
To successfully develop an embedded device that meets your expectations regarding quality, budget and time to market, all parts of the project need to fit perfectly together.
Learn more about KDAB's expertise in embedded software development.
Where the capabilities of modern mobile devices or web browsers fall short, KDAB engineers help you expertly architect and build high-functioning desktop and workstation applications.
Extensible, Safety-compliant Software for the Medical Sector
Create intelligent, patient-focused medical software and devices and stay ahead with technology that adapts to your needs.
KDAB offers you expertise in developing a broad spectrum of clinical and home-healthcare devices, including but not limited to, internal imaging systems, robotic surgery devices, ventilators and non-invasive monitoring systems.
Building digital dashboards and cockpits with fluid animations and gesture-controlled touchscreens is a big challenge.
In over two decades of developing intricate UI solutions for cars, trucks, tractors, scooters, ships, airplanes and more, the KDAB team has gained market leading expertise in this realm.
Build on Advanced Expertise when creating Modern UIs
KDAB assists you in the creation of user-friendly interfaces designed specifically for industrial process control, manufacturing, and fabrication.
Our specialties encompass the custom design and development of HMIs, enabling product accessibility from embedded systems, remote desktops, and mobile devices on the move.
Legacy software is a growing but often ignored problem across all industries. KDAB helps you elevate your aging code base to meet the dynamic needs of the future.
Whether you want to migrate from an old to a modern GUI toolkit, update to a more recent version, or modernize your code base, you can rely on over 25 years of modernization experience.
KDAB offers a wide range of services to address your software needs including consulting, development, workshops and training tailored to your requirements.
Our expertise spans cross-platform desktop, embedded and 3D application development, using the proven technologies for the job.
When working with KDAB, the first-ever Qt consultancy, you benefit from a deep understanding of Qt internals, that allows us to provide effective solutions, irrespective of the depth or scale of your Qt project.
Qt Services include developing applications, building runtimes, mixing native and web technologies, solving performance issues, and porting problems.
KDAB helps create commercial, scientific or industrial desktop applications from scratch, or update its code or framework to benefit from modern features.
Discover clean, efficient solutions that precisely meet your requirements.
Boost your team's programming skills with in-depth, constantly updated, hands-on training courses delivered by active software engineers who love to teach and share their knowledge.
Our courses cover Modern C++, Qt/QML, Rust, 3D programming, Debugging, Profiling and more.
The collective expertise of KDAB's engineering team is at your disposal to help you choose the software stack for your project or master domain-specific challenges.
Our particular focus is on software technologies you use for cross-platform applications or for embedded devices.
Since 1999, KDAB has been the largest independent Qt consultancy worldwide and today is a Qt Platinum partner. Our experts can help you with any aspect of software development with Qt and QML.
KDAB specializes in Modern C++ development, with a focus on desktop applications, GUI, embedded software, and operating systems.
Our experts are industry-recognized contributors and trainers, leveraging C++'s power and relevance across these domains to deliver high-quality software solutions.
KDAB can guide you incorporating Rust into your project, from as overlapping element to your existing C++ codebase to a complete replacement of your legacy code.
Unique Expertise for Desktop and Embedded Platforms
Whether you are using Linux, Windows, MacOS, Android, iOS or real-time OS, KDAB helps you create performance optimized applications on your preferred platform.
If you are planning to create projects with Slint, a lightweight alternative to standard GUI frameworks especially on low-end hardware, you can rely on the expertise of KDAB being one of the earliest adopters and official service partner of Slint.
KDAB has deep expertise in embedded systems, which coupled with Flutter proficiency, allows us to provide comprehensive support throughout the software development lifecycle.
Our engineers are constantly contributing to the Flutter ecosystem, for example by developing flutter-pi, one of the most used embedders.
KDAB invests significant time in exploring new software technologies to maintain its position as software authority. Benefit from this research and incorporate it eventually into your own project.
Start here to browse infos on the KDAB website(s) and take advantage of useful developer resources like blogs, publications and videos about Qt, C++, Rust, 3D technologies like OpenGL and Vulkan, the KDAB developer tools and more.
The KDAB Youtube channel has become a go-to source for developers looking for high-quality tutorial and information material around software development with Qt/QML, C++, Rust and other technologies.
Click to navigate the all KDAB videos directly on this website.
In over 25 years KDAB has served hundreds of customers from various industries, many of them having become long-term customers who value our unique expertise and dedication.
Learn more about KDAB as a company, understand why we are considered a trusted partner by many and explore project examples in which we have proven to be the right supplier.
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications.
Read more about the history, the values, the team and the founder of the company.
When working with KDAB you can expect quality software and the desired business outcomes thanks to decades of experience gathered in hundreds of projects of different sizes in various industries.
Have a look at selected examples where KDAB has helped customers to succeed with their projects.
KDAB is committed to developing high-quality and high-performance software, and helping other developers deliver to the same high standards.
We create software with pride to improve your engineering and your business, making your products more resilient and maintainable with better performance.
KDAB has been the first certified Qt consulting and software development company in the world, and continues to deliver quality processes that meet or exceed the highest expectations.
In KDAB we value practical software development experience and skills higher than academic degrees. We strive to ensure equal treatment of all our employees regardless of age, ethnicity, gender, sexual orientation, nationality.
Interested? Read more about working at KDAB and how to apply for a job in software engineering or business administration.
The container classes introduced in Qt 4 (Tulip, for the aficionados) had an interesting optimization: the ability to turn certain operations on the contained objects into byte-level manipulations.
Example: vector reallocation
Consider the reallocation of a QVector<T>: when the vector is full and we want to insert a new value (of type T), the vector has to allocate a bigger block of memory.
Simplifying, the way this is done for a generic type T is:
1. Allocate a bigger block of memory.
2. Move-construct the T objects from the current storage into the new memory.
Elements are being moved. The red, underlined elements in the source represent moved-from objects. C will be moved next.
3. Destroy the moved-from objects in old current storage.
4. Deallocate the old block of memory.
5. Update bookkeeping (adjust data pointer, size, capacity, as needed).
Depending on the operation that causes the reallocation (push_back, insert in the middle, simple reserve) a new element may also be added to the just allocated block of memory; that's not really relevant here. I am also deliberately ignoring a lot of details, such as exception handling, move_if_noexcept, out of memory, allocators and similar.
The optimization
Types like int don't do anything when they are constructed or destructed. If an int object is simply memcpy-ed to another place in memory, you would get the same integer in another place. Moreover, there would be nothing to do to destroy the original integer.
Therefore, if you have a QVector<int>, the above approach has some steps that are not needed. There's a much faster way to achieve the same result: 1. Allocate a bigger block of memory.
2. memcpy all the data from the old storage to the new one.
Qt is exploiting the fact that, for some datatypes, move-construction of an object A into a new object B, followed by the destruction of A, is equivalent / implementable as a byte copy (memcpy of A's object representation) into some suitable storage. In pseudocode:
// Given:T *ptrA =~~~;// points to a valid objectT *ptrB =~~~;// points to uninitialized storage// Then this:new(ptrB)T(std::move(*ptrA));// move-construct A into new storage (placement new)ptrA->~T();// destroy A// can be implemented like this:memcpy(ptrB, ptrA,sizeof(T));
The combination of move-construction followed by destruction was happening in the generic code for vector reallocation. Instead of that, in the optimized version, we memcpy each object.
Note that in the optimized version we never call the move constructor of T; and we never call the destructor of A. They are both realized by the call to memcpy.
It's easy to convince ourselves that this works for a type like int, where we can copy its object representation and end up with the same value.
What about a more complicated type, like QString?
It still works! When we memcpy a QString, we end up creating a new QString object that points to the same payload as the original, so that part is fine. Now, normally, when we create a copy of a QString, we would need to increase its reference counter. But here we are not creating a new copy: we are moving from the original string, and destroying the original. The consequence is that the reference counter of the string does not need to change; in the end the total number of QString objects pointing to that payload is the same. This is ensured by the fact that we are not running QString's destructor over the original object.
QString is trivially relocatable: we can memcpy the QString object (just a pointer, really) and deallocate the original without running its destructor. This achieves the same effects as move-construction + destruction.
We can do even better
We have just established that we can use memcpy instead of move constructing one object (from the old storage into the new) and destroying the old object.
Now, QVector has to repeat this operation for multiple objects; since they are all stored contiguously in memory, then QVector can take a shortcut, and simply do a bulk memcpy of its contents!
This can be pushed even further: QVector simply calls realloc when reallocating, which is even better, because it allows to grow the allocated area in-place. Otherwise, realloc itself will move the data in memory. Simple, easy and efficient.
Terminology
In Qt we call a type that can be moved in memory via memcpyrelocatable, but this specific jargon means different things to different people. I'll try to clarify it in the rest of this post, as we go along.
For the moment, let's try to be slightly more precise than Qt is, and use this vocabulary:
"relocation" means to move-construct an object, and destroy the source;
"trivial relocation" means to do the same, but achieve it via a simple memcpy.
However, please take this definition with a grain of salt. In the next instalments we will need to tweak it, as we discover some interesting properties of relocation.
Does this mean that QVector uses trivial relocation for all types?
No, it doesn't. While it works for things like int, QPoint, QString, or QPen, this optimization is not safe to do in general: for some types Qt cannot replace move construction + destruction with a memcpy.
For instance, consider a type like a string class which employes the so-called Short/Small String Optimization, or SSO. This would be for instance std::string on most implementations. (For more background about SSO, see here.)
classstring{// may point to the heap, or into `m_buffer` if the string// is short enough:char*m_begin; size_t m_size; size_t m_capacity;char m_buffer[SSO_BUFFER_SIZE];};string s ="Hello";
What would happen if we tried to replace a move-construction + destruction of a such a string class with a memcpy? Well, nothing good! In the source object the m_begin pointer may be pointing into itself.
If we try to relocate that string into another string via a memcpy, the new string's pointer will point to the old object's buffer; but the old object has been destroyed!
In fact, the move constructor of this string type would probably look something like this.
The bottom line is that there is no way to know "from the outside" if a generic type T can be moved in memory via memcpy. Qt must assume that it cannot do that, and therefore Qt by default sticks to the vector reallocation algorithm that moves constructs and destroys each element.
The types that cannot be moved in memory via memcpy are, generally speaking, types whose objects invariants depend on their specific address in memory. This is the case of self-referential types (like the string type), but also types which are referenced to externally (for instance, a node in a linked list is pointed to by the successor and the predecessor in the list, so we can't just move it around in memory without breaking those links).
So how does Qt know that QString can be trivially relocated?
QString isn't "hardcoded" to be special; any type can tell Qt that it is safe to be trivially relocated, and it does so by using a macro:
The macro expands to a template specialization that says that MyClass is opting in the optimization. For QString the macro is here, just hidden behind another macro.
Since most Qt value classes are pimpled (they just wrap a pointer to the private class, allocated on the heap; for more info, see here and here) it turns out that most of them benefit from this optimization, which is likely why it got introduced in the first place.
Q_DECLARE_TYPEINFO is public API; you are supposed to mark your own trivially relocatable types with it.
What does Q_RELOCATABLE_TYPE mean?
According to the documentation:
Q_RELOCATABLE_TYPE specifies that Type has a constructor and/or a destructor but can be moved in memory using memcpy().
This seems to pretty much match our previous definition of trivial relocatable type. The wording is however a bit imprecise: it does not clarify what "moved in memory" really means; that is, what sequence of operations is realized through the call to memcpy.
In Qt 4 the macro was indeed called Q_MOVABLE_TYPE. That name however clashed with "move semantics" (introduced later in C++: Qt 4 was released in 2005!), so Qt wanted to move away from it, in order to avoid any confusion. While that spelling is still around, in Qt 5 and 6 one should prefer the name Q_RELOCATABLE_TYPE.
To nitpick, it should have really been called Q_TRIVIALLY_RELOCATABLE_TYPE, but that ship has sailed.
Does Qt only use trivial relocation for QVector reallocation?
No, it also uses it in a few other places. For instance:
QVarLengthArray is also a vector-like data structure, and uses a number of similar optimizations;
QVariant (Qt's equivalent of std::any) allocates memory in order to store the object it contains, but it also has a "small object optimization", where the held object is stored directly inside the QVariant object. SOO is enabled only if:
the target object is small enough to fit (obviously);
but also only if the object is trivially relocatable.
In this sense, QVariant move constructor is type-erased; if SSO is in use, then the buffer is simply copied over into the new variant (as in, memcpy), and the old one is cleared.
There's another instance of using trivial relocation which deserves a special discussion: certain algorithms are optimized in Qt for trivially relocatable types. For instance, inserting or erasing elements from a QVector of a trivially relocatable type is optimized as well.
This has some important consequences. Since this post is already overly long, I am going to analyze them in the next instalment of this series. :-)
Trusted software excellence across embedded and desktop platforms
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications. In addition to being leading experts in Qt, C++ and 3D technologies for over two decades, KDAB provides deep expertise across the stack, including Linux, Rust and modern UI frameworks. With 100+ employees from 20 countries and offices in Sweden, Germany, USA, France and UK, we serve clients around the world.
I have written a small sample that simply detaches a list with 10k items of a custom type 10k times, and I could not measure any benefit from specifying Q_RELOCATABLE_TYPE for my custom type. This is true both for Debug and Release builds (Release was about 3x faster in any case).
I admit, it was a very quick hack, not a properly setup profiling, but do you have counter-examples where specifying Q_RELOCATABLE_TYPE really brought measurable benefits?
14 - Jun - 2024
Giuseppe D'Angelo
Hi,
Trivial relocation isn't going to give you performance benefits for detaching. When you detach a Qt container, a copy of each element needs to be taken, and there are no real shortcuts for that (unless the type is trivially copyable or similar).
Instead, you're going to get performance benefits when a QVector is reallocated, or when certain operations are applied to it (for instance, erasing an element from the middle). A realistic benchmark is something along these lines:
Create a Rule Of 5 class, where construction, copy construction/assignement, destruction are expensive and out of line. For instance, a pimpled value class, or something like std::shared_ptr.
Create a QList of that class and do some operations on it (e.g. trigger a reallocation), measuring the cost
Do the same after marking the class trivially relocatable (via Q_DECLARE_TYPEINFO).
The benchmark should show a huge improvement.
Giuseppe D’Angelo
Senior Software Engineer
Senior Software Engineer at KDAB. Giuseppe is a long-time contributor to Qt, having used Qt and C++ since 2000, and is an Approver in the Qt Project. His contributions in Qt range from containers and regular expressions to GUI, Widgets, and OpenGL. A free software passionate and UNIX specialist, before joining KDAB, he organized conferences on opensource around Italy. He holds a BSc in Computer Science.
Our hands-on Modern C++ training courses are designed to quickly familiarize newcomers with the language. They also update professional C++ developers on the latest changes in the language and standard library introduced in recent C++ editions.
2 Comments
13 - Jun - 2024
Robert
I have written a small sample that simply detaches a list with 10k items of a custom type 10k times, and I could not measure any benefit from specifying Q_RELOCATABLE_TYPE for my custom type. This is true both for Debug and Release builds (Release was about 3x faster in any case). I admit, it was a very quick hack, not a properly setup profiling, but do you have counter-examples where specifying Q_RELOCATABLE_TYPE really brought measurable benefits?
14 - Jun - 2024
Giuseppe D'Angelo
Hi,
Trivial relocation isn't going to give you performance benefits for detaching. When you detach a Qt container, a copy of each element needs to be taken, and there are no real shortcuts for that (unless the type is trivially copyable or similar).
Instead, you're going to get performance benefits when a QVector is reallocated, or when certain operations are applied to it (for instance, erasing an element from the middle). A realistic benchmark is something along these lines:
Create a Rule Of 5 class, where construction, copy construction/assignement, destruction are expensive and out of line. For instance, a pimpled value class, or something like
std::shared_ptr
.Create a QList of that class and do some operations on it (e.g. trigger a reallocation), measuring the cost
Do the same after marking the class trivially relocatable (via
Q_DECLARE_TYPEINFO
).The benchmark should show a huge improvement.