I am trying to pass multiple images (actually a video) between two processes using QSharedmemory. Currently I am serializing a QVector of QImages and copying this to the memory. This works, but the serialization steps takes about 99% of the time. How could I do this faster in a platform independent manner?
My code for sending:
int main(int argc, const char* argv[])
{
QObject *parent;
QString program = "python";
QStringList arguments;
QString programPath(PYSOURCE);
arguments << "-u" << programPath+QString("/test.py");
qDebug() << arguments;
QProcess *pyProcess = new QProcess();
pyProcess->start(program, arguments);
QVector<QImage> images;
//.. fill with some images
auto start = std::chrono::system_clock::now();
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
// this takes extremely long ~44s for 80mb
out << images;
int size = buffer.size();
QSharedMemory sharedMemory("process_example");
if (!sharedMemory.create(size)) {
qDebug() << "Unable to create shared memory segment.";
return 0;
}
qDebug() << "sizeof mem: " << sharedMemory.size() << "bufsize:" << buffer.size();
sharedMemory.lock();
char *to = (char*)sharedMemory.data();
const char *from = buffer.data().data();
memcpy(to, from, qMin(sharedMemory.size(), size));
sharedMemory.unlock();
qDebug() << "image copied to shared memory";
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start);
qDebug() << "duration:" << duration.count() << "ms";
while (pyProcess->waitForReadyRead(-1)){
QByteArray newData = pyProcess->readAllStandardOutput();
QString result = QString::fromLocal8Bit(newData);
qDebug(qPrintable(QString("py:")+result));
}
sharedMemory.detach();
return 0;
}
I think the main problem for your buffer is that it need resizing many times. Try set big size for underling byte array to bigger size then your data need.
Call this just before serialization of images.