From 8d553fdf952b79dc79447392161f97ff866cfcb9 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Wed, 3 Dec 2014 15:58:41 +0100 Subject: Optimize: Remove temporary allocations in QMeshData::attributeNames. This is called frequently and was previously triggering many temporary data allocations. By using a separated QStringList and QVector, we can keep the behavior of the QHash and can remove the temporaries. Change-Id: Idd7db916341037e98b78730d4f599990041ec706 Reviewed-by: Paul Lemire Reviewed-by: Sean Harmer --- src/render/io/qmeshdata.cpp | 24 +++++++++++++++++------- src/render/io/qmeshdata.h | 6 +++--- src/render/io/qmeshdata_p.h | 3 ++- 3 files changed, 22 insertions(+), 11 deletions(-) (limited to 'src/render/io') diff --git a/src/render/io/qmeshdata.cpp b/src/render/io/qmeshdata.cpp index af26770a2..08cd1da09 100644 --- a/src/render/io/qmeshdata.cpp +++ b/src/render/io/qmeshdata.cpp @@ -71,13 +71,19 @@ QMeshData::QMeshData(QMeshDataPrivate &dd) { } -void QMeshData::addAttribute(const QString &name, QAbstractAttributePtr attr) +void QMeshData::addAttribute(const QString &name, const QAbstractAttributePtr &attr) { Q_D(QMeshData); - d->m_attributes.insert(name, attr); + const int i = d->m_attributesNames.indexOf(name); + if (i != -1) { + d->m_attributes[i] = attr; + } else { + d->m_attributesNames.append(name); + d->m_attributes.append(attr); + } } -void QMeshData::setIndexAttribute(QAbstractAttributePtr attr) +void QMeshData::setIndexAttribute(const QAbstractAttributePtr &attr) { Q_D(QMeshData); d->m_indexAttr = attr; @@ -86,13 +92,17 @@ void QMeshData::setIndexAttribute(QAbstractAttributePtr attr) QStringList QMeshData::attributeNames() const { Q_D(const QMeshData); - return d->m_attributes.keys(); + return d->m_attributesNames; } -QAbstractAttributePtr QMeshData::attributeByName(QString name) const +QAbstractAttributePtr QMeshData::attributeByName(const QString &name) const { Q_D(const QMeshData); - return d->m_attributes.value(name); + const int i = d->m_attributesNames.indexOf(name); + if (i != -1) + return d->m_attributes[i]; + else + return QAbstractAttributePtr(); } QAbstractAttributePtr QMeshData::indexAttribute() const @@ -134,7 +144,7 @@ QList QMeshData::buffers() const if (d->m_indexAttr) r.insert(d->m_indexAttr->buffer()); - Q_FOREACH (QAbstractAttributePtr v, d->m_attributes.values()) + Q_FOREACH (const QAbstractAttributePtr &v, d->m_attributes) r.insert(v->buffer()); return r.toList(); diff --git a/src/render/io/qmeshdata.h b/src/render/io/qmeshdata.h index 613f1190a..20b4008fb 100644 --- a/src/render/io/qmeshdata.h +++ b/src/render/io/qmeshdata.h @@ -78,11 +78,11 @@ public: explicit QMeshData(PrimitiveTypes primitiveType = Triangles); virtual ~QMeshData(); - void addAttribute(const QString& name, QAbstractAttributePtr attr); - void setIndexAttribute(QAbstractAttributePtr attr); + void addAttribute(const QString &name, const QAbstractAttributePtr &attr); + void setIndexAttribute(const QAbstractAttributePtr &attr); QStringList attributeNames() const; - QAbstractAttributePtr attributeByName(QString name) const; + QAbstractAttributePtr attributeByName(const QString &name) const; QAbstractAttributePtr indexAttribute() const; static const QString defaultPositionAttributeName() { return QStringLiteral("vertexPosition"); } diff --git a/src/render/io/qmeshdata_p.h b/src/render/io/qmeshdata_p.h index 4774d5915..6e74b2370 100644 --- a/src/render/io/qmeshdata_p.h +++ b/src/render/io/qmeshdata_p.h @@ -66,7 +66,8 @@ public: Q_DECLARE_PUBLIC(QMeshData) QMeshData *q_ptr; - QMap m_attributes; + QStringList m_attributesNames; + QVector m_attributes; QAbstractAttributePtr m_indexAttr; QAxisAlignedBoundingBox m_bbox; int m_verticesPerPatch; -- cgit v1.2.3