PDA

View Full Version : Why is QBuffer's close call so slow?



eekisa
8th April 2009, 08:18
I have the following piece of code:


void Class::processElements(const QString & aInputFile,
const QString & aNodeName)
{
QFile queryFile(QString(":queries/") + "syncronizationresponse.xq");
if (queryFile.open(QFile::ReadOnly))
{
QString queryString(queryFile.readAll());

QFile sourceDocument(aInputFile);
if (sourceDocument.open(QIODevice::ReadOnly))
{
//Create query
QXmlQuery query;
query.bindVariable("inputDocument", &sourceDocument);
query.bindVariable("bindedVariable", QVariant(aNodeName));
query.setQuery(queryString);

if(query.isValid())
{
//Evaluate
QByteArray byteArray;
QBuffer buffer(&byteArray);
if (buffer.open(QIODevice::ReadWrite))
{
QXmlSerializer xmlSerializer(query, &buffer);
if (query.evaluateTo(&xmlSerializer))
{
//Process the stuff in the byteArray
}
else
qDebug() << "Evaluation to serializer failed";
}
else
{
qDebug() << "Opening output buffer failed " << buffer.errorString();
}
//Starting to close buffer for "ROUTE" "ke 8. huhti 06:53:37 2009"
buffer.close();
//Finished closing buffer for "ROUTE" "ke 8. huhti 06:53:37 2009"
}
else
{
qDebug() << "XmlQuery failed";
}
}
else
{
qDebug() << "Opening input file failed " << sourceDocument.errorString();
}
//Starting to close source document for "ROUTE" "ke 8. huhti 06:53:46 2009"
sourceDocument.close();
//Finished closing source document for "ROUTE" "ke 8. huhti 06:53:46 2009"
}
else
{
qDebug() << "Opening query file failed " << queryFile.errorString();
}
//Starting to close query file for "ROUTE" "ke 8. huhti 06:53:46 2009"
queryFile.close();
//Finished closing query file for "ROUTE" "ke 8. huhti 06:53:46 2009"
}

Who can tell me why the heck it takes 9 seconds from buffer.close(); to sourceDocument.close(); ?? There should not be anything that slows it down? Unless it has something to do with signals that the QBuffer emits when close is called? Does anyone know what makes close call to QBuffer so slow??

BR, Kimmo

wysota
8th April 2009, 09:34
It looks like close finishes immediately. It seems that it's the destructors that might be causing the slowdown. What is the point of using the buffer if you destroy it immediately afterwards without doing anything with the data saved in it? What happens if you move the buffer and byte array declarations outside the most inner scope?

eekisa
8th April 2009, 10:18
What is the point of using the buffer if you destroy it immediately afterwards without doing anything with the data saved in it

The section where it stands "//Process the stuff in the byteArray" is where I use the byteArray. I just ripped from this code sniplet that part out.


What happens if you move the buffer and byte array declarations outside the most inner scope

I haven't tried :) I'll see what happens..

eekisa
8th April 2009, 11:00
After further investigation I found out that it is the deletion of "QXmlQuery query" that takes ages. Wysota's tip for moving declations outside the if clause gave me the hint that it could be some destruction (when leaving the if) that is taking such a long time. I moved (byteArray, buffer, sourceDocument and query) declarations to the beging of my method and changed the creation of "QXmlQuery query" to "QXmlQuery* query = new QXmlQuery();". I now see that


//Starting to delete query "ROUTE" "ke 8. huhti 09:53:36 2009"
if (query)
delete query;
//Finished deleting query "ROUTE" "ke 8. huhti 09:53:46 2009"

Any idea why deletion of QXmlQuery takes such a long time.

eekisa
8th April 2009, 12:07
I also noticed that it does not help to move the QXmlQuery to class member and pass that to the method as a varaible. This way it would only need to be deleted once after all method calls. Anyway, this is because then QXmlQuery's

"query->bindVariable("inputDocument", &sourceDocument);"

will hold on the second method call. Most likely as to documentation "If name has already been bound, its previous binding will be overridden". So it is the realising of something that is slowing it down..