Implicit Sharing
Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.
Overview
A shared class consists of a pointer to a shared data block that contains a reference count and the data.
When a shared object is created, it sets the reference count to 1. The reference count is incremented whenever a new object references the shared data, and decremented when the object dereferences the shared data. The shared data is deleted when the reference count becomes zero.
When dealing with shared objects, there are two ways of copying an object. We usually speak about deep and shallow copies. A deep copy implies duplicating an object. A shallow copy is a reference copy, i.e. just a pointer to a shared data block. Making a deep copy can be expensive in terms of memory and CPU. Making a shallow copy is very fast, because it only involves setting a pointer and incrementing the reference count.
Object assignment (with operator=()) for implicitly shared objects is implemented using shallow copies.
The benefit of sharing is that a program does not need to duplicate data unnecessarily, which results in lower memory use and less copying of data. Objects can easily be assigned, sent as function arguments, and returned from functions.
Implicit sharing mostly takes place behind the scenes; the programmer rarely needs to worry about it. However, Qt's container iterators have different behavior than those from the STL. Read Implicit sharing iterator problem.
In multithreaded applications, implicit sharing takes place, as explained in Threads and Implicitly Shared Classes.
When implementing your own implicitly shared classes, use the QSharedData and QSharedDataPointer classes.
Implicit Sharing in Detail
Implicit sharing automatically detaches the object from a shared block if the object is about to change and the reference count is greater than one. (This is often called copy-on-write or value semantics.)
An implicitly shared class has control of its internal data. In any member functions that modify its data, it automatically detaches before modifying the data. Notice, however, the special case with container iterators; see Implicit sharing iterator problem.
The QPen class, which uses implicit sharing, detaches from the shared data in all member functions that change the internal data.
Code fragment:
void QPen::setStyle(Qt::PenStyle style) { detach(); // detach from common data d->style = style; // set the style member } void QPen::detach() { if (d->ref != 1) { ... // perform a deep copy } }
List of Classes
The classes listed below automatically detach from common data if an object is about to be changed. The programmer will not even notice that the objects are shared. Thus you should treat separate instances of them as separate objects. They will always behave as separate objects but with the added benefit of sharing data whenever possible. For this reason, you can pass instances of these classes as arguments to functions by value without concern for the copying overhead.
Example:
QPixmap p1, p2; p1.load("image.bmp"); p2 = p1; // p1 and p2 share data QPainter paint; paint.begin(&p2); // cuts p2 loose from p1 paint.drawText(0,50, "Hi"); paint.end();
In this example, p1
and p2
share data until QPainter::begin() is called for p2
, because painting a pixmap will modify it.
Warning: Be careful with copying an implicitly shared container (QMap, QList, etc.) while you use STL-style iterator. See Implicit sharing iterator problem.
Array of bits | |
Monochrome (1-bit depth) pixmaps | |
Defines the fill pattern of shapes drawn by QPainter | |
Array of bytes | |
List of byte arrays | |
View on an array of bytes with a read-only subset of the QByteArray API | |
Template class that provides a cache | |
Compares strings according to a localized collation algorithm | |
Can be used to speed up string collation | |
Defines a possible command-line option | |
Template class that provides a contiguous cache | |
Mouse cursor with an arbitrary shape | |
Refers to one pending asynchronous call | |
Holds one Unix file descriptor | |
Date and time functions | |
Output stream for debugging information | |
Access to directory structures and their contents | |
Stores information about a domain name record | |
Stores information about a host address record | |
Stores information about a DNS MX record | |
Stores information about a DNS SRV record | |
Stores information about a DNS TXT record | |
System-independent file information | |
Specifies a query for a font used for drawing text | |
General information about fonts | |
Font metrics information | |
Font metrics information | |
Describes the parameters of an area or region to be monitored for proximity | |
Contains information gathered on a global position, direction and velocity at a particular point in time | |
Contains basic information about a satellite | |
Direct access to the internal glyphs in a font | |
Used in combination with QBrush to specify gradient fills | |
Template class that provides a hash-table-based dictionary | |
IP address | |
Controls HTTP/2 parameters and settings | |
Holds a body part to be used inside a HTTP multipart MIME message | |
Scalable icons in different modes and states | |
Hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device | |
Encapsulates a JSON array | |
Way to read and write JSON documents | |
Encapsulates a JSON object | |
Used to report errors during JSON parsing | |
Encapsulates a value in JSON | |
Encapsulates a key sequence as used by shortcuts | |
Template class that provides linked lists | |
Template class that provides a dynamic array | |
Converts between numbers and their string representations in various languages | |
Represents the data to be broadcast during Bluetooth Low Energy advertising | |
Represents the parameters used for Bluetooth Low Energy advertising | |
Used to set up GATT service data | |
Used when requesting or reporting an update of the parameters of a Bluetooth LE connection | |
Used to create GATT service data | |
Used to set up GATT service data | |
Template class that provides an associative array | |
Describes types of file or data, represented by a MIME type string | |
Represents a MQTT topic filter | |
Represents a MQTT topic name | |
Convenience QHash subclass that provides multi-valued hashes | |
Template class that provides an associative array with multiple equivalent keys | |
Stores one IP address supported by a network interface, along with its associated netmask and broadcast address | |
Cache information | |
Holds one network cookie | |
Listing of the host's IP addresses and network interfaces | |
Network layer proxy | |
Used to query the proxy settings for a socket | |
Holds a request to be sent with QNetworkAccessManager | |
Wraps an OpenGL debug message | |
Represents a collection of page ranges | |
Container for painting operations, enabling graphical shapes to be constructed and reused | |
Contains color groups for each widget state | |
Defines how a QPainter should draw lines and outlines of shapes | |
Used to locate data in a data model | |
Paint device that records and replays QPainter commands | |
Off-screen image representation that can be used as a paint device | |
List of points using integer precision | |
List of points using floating point precision | |
Holds the environment variables that can be passed to a program | |
Generic container that provides a queue | |
Access to a single physical instance of a font | |
Pattern matching using regular expressions | |
Specifies a clip region for a painter | |
Pattern matching using regular expressions | |
The results of a matching a QRegularExpression against a string | |
Iterator on the results of a global match of a QRegularExpression object against a string | |
Template class that provides a hash-table-based set | |
Manipulates the fields in SQL database tables and views | |
Means of executing and manipulating SQL statements | |
Encapsulates a database record | |
Convenient API for an X509 certificate | |
API for accessing the extensions of an X509 certificate | |
Represents an SSL cryptographic cipher | |
Holds the configuration and state of an SSL connection | |
Interface for Diffie-Hellman parameters for servers | |
SSL error | |
Interface for private and public keys | |
Authentication data for pre shared keys (PSK) ciphersuites | |
Template class that provides a stack | |
Enables optimized drawing of text when the text and its layout is updated rarely | |
Provides information about currently mounted storage and drives | |
Unicode character string | |
List of strings | |
Formatting information for blocks of text in a QTextDocument | |
Way of finding Unicode text boundaries in a string | |
Formatting information for characters in a QTextDocument | |
Offers an API to access and modify QTextDocuments | |
Represents a piece of formatted text from a QTextDocument | |
Formatting information for a QTextDocument | |
Formatting information for frames in a QTextDocument | |
Formatting information for images in a QTextDocument | |
Formatting information for lists in a QTextDocument | |
Formatting information for table cells in a QTextDocument | |
Formatting information for tables in a QTextDocument | |
Convenient interface for working with URLs | |
Way to manipulate a key-value pairs in a URL's query | |
Acts like a union for the most common Qt data types |