aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmltooling/packetprotocol
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@theqtcompany.com>2016-03-03 13:38:27 +0100
committerUlf Hermann <ulf.hermann@theqtcompany.com>2016-03-11 09:19:01 +0000
commit6300f4dde0ab3279b18a0fb29f94cfe142557cba (patch)
tree4158af34ad0e812fa50f8b046f54439411ebc151 /src/plugins/qmltooling/packetprotocol
parent9ca9de7cbc8b045c19899987d6666576399af33d (diff)
QmlDebug: Reuse packets instead of deleting and recreating them
This reduces memory churn as we don't have to reallocate the various QPacket, QBuffer, QByteArray objects and their private classes all the time. Also, subsequent packets often have similar sizes, which makes it beneficial to preallocate the underlying byte array to the size of the previous packet on clear(). In order not to carry the extra reserved space in the lists passed to the debug server, we squeeze the byte arrays before inserting them into the lists. That detaches and copies them, which would have been done anyway when reusing the same packet for the next message. The result is a reduction in the number of temporary memory allocations from QQmlProfilerServiceImpl::sendMessages() by about 75%. Change-Id: Ief98cd7a93a2e8e840e111dee6b346cae00e06bf Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'src/plugins/qmltooling/packetprotocol')
-rw-r--r--src/plugins/qmltooling/packetprotocol/qpacket.cpp27
-rw-r--r--src/plugins/qmltooling/packetprotocol/qpacket_p.h4
2 files changed, 28 insertions, 3 deletions
diff --git a/src/plugins/qmltooling/packetprotocol/qpacket.cpp b/src/plugins/qmltooling/packetprotocol/qpacket.cpp
index c42288e920..fab0a5b189 100644
--- a/src/plugins/qmltooling/packetprotocol/qpacket.cpp
+++ b/src/plugins/qmltooling/packetprotocol/qpacket.cpp
@@ -108,11 +108,34 @@ QPacket::QPacket(int version, const QByteArray &data)
}
/*!
- Returns raw packet data.
+ Returns a reference to the raw packet data.
*/
-QByteArray QPacket::data() const
+const QByteArray &QPacket::data() const
{
return buf.data();
}
+/*!
+ Returns a copy of the raw packet data, with extra reserved space removed.
+ Mind that this triggers a deep copy. Use it if you anticipate the data to be detached soon anyway.
+ */
+QByteArray QPacket::squeezedData() const
+{
+ QByteArray ret = buf.data();
+ ret.squeeze();
+ return ret;
+}
+
+/*!
+ Clears the packet, discarding any data.
+ */
+void QPacket::clear()
+{
+ buf.reset();
+ QByteArray &buffer = buf.buffer();
+ // Keep the old size to prevent unnecessary allocations
+ buffer.reserve(buffer.capacity());
+ buffer.truncate(0);
+}
+
QT_END_NAMESPACE
diff --git a/src/plugins/qmltooling/packetprotocol/qpacket_p.h b/src/plugins/qmltooling/packetprotocol/qpacket_p.h
index a079b244e8..b6fda2411d 100644
--- a/src/plugins/qmltooling/packetprotocol/qpacket_p.h
+++ b/src/plugins/qmltooling/packetprotocol/qpacket_p.h
@@ -61,7 +61,9 @@ class QPacket : public QDataStream
public:
QPacket(int version);
explicit QPacket(int version, const QByteArray &ba);
- QByteArray data() const;
+ const QByteArray &data() const;
+ QByteArray squeezedData() const;
+ void clear();
private:
void init(QIODevice::OpenMode mode);