PDA

View Full Version : returning Qlist of QDomDocument



franki
19th April 2012, 11:35
Hi

I would like to know if when I return QList<QDomDocument> from function, is it copied in memory, or only some reference to this xml's is returned?
These xml's contains files (base64 encoded) so they are really big.

Simplified example is like this



QList<QDomDocument> MyClass::createXmls() {
QList<QDomDocument> xmls;
for(int i=0;i<something;i++) {
//create xmls
QDomDocument xml;
// fill in xml...
xmls.append(xml);
}
return xmls;
}
void MyClass::sendXmls() {
QList<QDomDocument> xmls=this->createXmls();
// process xmls in loop
}


best regards
Marek

wysota
19th April 2012, 15:51
Nothing (well, almost) is copied. All relevant classes share data between their instances.

franki
20th April 2012, 08:03
Thanks,

Is this non-copying behaviour due to QDomDocument itself?
As I read somewhere in docs, QDomDocument stays in memory until last variable is assigned to its memory area, and if all variables that were assigned are deleted then memory is freed. Is this why in case above nothing is copied upon returning from function? Because "normal" return from function is done by copying value

Marek

wysota
20th April 2012, 08:40
Is this non-copying behaviour due to QDomDocument itself?
Both to QList and QDomDocument. QList is an implicitly shared class, QDomDocument is an explicitly shared class.


Because "normal" return from function is done by copying value
Only the facade is copied, the data itself is a separate object hidden behind the facade and it's shared between instances thus the copy is cheap if you don't modify the data.

franki
20th April 2012, 10:59
OK, so I suppose this is because QSharedData is used inside objects like QString or QList, and in docs there is said that this classes are "implicitly shared".
So every class in Qt that is implicitly shared can be passed as argument to function or returned from function with minimum copying, and to determine that I should read about class constructor to check whether data is shared or not, right?
I've found implicitly shared class list, but what about explicitly shared: QDomDocument, QMemArray<T>, QImage, and QMovie any other? and where to check if class is explicitly shared, there is nothing about this in QDomDocument class reference.

best regards
Marek

wysota
20th April 2012, 11:20
OK, so I suppose this is because QSharedData is used inside objects like QString or QList, and in docs there is said that this classes are "implicitly shared".
So every class in Qt that is implicitly shared can be passed as argument to function or returned from function with minimum copying, and to determine that I should read about class constructor to check whether data is shared or not, right?
Essentially yes. Almost all "value-based" classes in Qt are implicitly shared. These that are not, are simpler (faster) to copy than to share. So in all cases you can be sure copying the object is optimal.


I've found implicitly shared class list, but what about explicitly shared: QDomDocument, QMemArray<T>, QImage, and QMovie any other? and where to check if class is explicitly shared, there is nothing about this in QDomDocument class reference.
The difference between an implicitly shared class and an explicitly shared class is that if you modify a copy of an implicitly shared object, the object is automatically detached (the data is copied) from the original so that only the new object gets modified. For an explicitly shared class modifying one incarnation of the object, modifies them all. Regarding QDomDocument, the class is not marked as explicitly shared but you can see by its behaviour that it is. Besides, the docs for the copy constructor of QDomDocument says:


The data of the copy is shared (shallow copy): modifying one node will also change the other. which effectively makes it an explicitly shared class.