summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilian Wolff <mail@milianw.de>2012-03-15 19:01:43 (GMT)
committerMilian Wolff <mail@milianw.de>2012-03-15 19:12:01 (GMT)
commit4be5dad13fb8058e38245554f0ae6dbeb96526b9 (patch)
treeefb56a1294af3c5d1feae7c8a6717a9a73d57049
parent77d9a14802a5d2e4e1e15eed336be0a44f907959 (diff)
optimize: leverage implitic QByteArray sharing to further reduce memory consumption
store labels on first encounter in a QHash and reuse copies of the labels there, which are just shallow, implicitly shared copies. further decreases the memory consumption considerably for my test case, where the consumption goes down by about 50%. Many thanks to André Wöbbeking for the suggestion. CCMAIL: Woebbeking@kde.org
-rw-r--r--massifdata/parserprivate.cpp15
-rw-r--r--massifdata/parserprivate.h7
2 files changed, 21 insertions, 1 deletions
diff --git a/massifdata/parserprivate.cpp b/massifdata/parserprivate.cpp
index a90142a..f825c03 100644
--- a/massifdata/parserprivate.cpp
+++ b/massifdata/parserprivate.cpp
@@ -332,6 +332,19 @@ struct SaveAndRestoreItem
TreeLeafItem* m_oldVal;
};
+QByteArray ParserPrivate::getLabel(const QByteArray& original)
+{
+ QSet<QByteArray>::const_iterator it = m_labels.constFind(original);
+ if (it != m_labels.constEnd()) {
+ // reuse known label to leverage implit sharing
+ return *it;
+ } else {
+ m_labels.insert(original);
+ return original;
+ }
+}
+
+
bool ParserPrivate::parseheapTreeLeafInternal(const QByteArray& line, int depth)
{
VALIDATE_RETURN(line.length() > depth + 1 && line.at(depth) == 'n', false)
@@ -354,7 +367,7 @@ bool ParserPrivate::parseheapTreeLeafInternal(const QByteArray& line, int depth)
return true;
}
- const QByteArray label = line.mid(spacePos + 1);
+ const QByteArray label = getLabel(line.mid(spacePos + 1));
bool isCustomAlloc = false;
diff --git a/massifdata/parserprivate.h b/massifdata/parserprivate.h
index 65ecd12..d23a45e 100644
--- a/massifdata/parserprivate.h
+++ b/massifdata/parserprivate.h
@@ -25,6 +25,7 @@
#include <QtCore/QByteArray>
#include <QtCore/QStringList>
+#include <QSet>
class QIODevice;
@@ -74,6 +75,8 @@ private:
void parseHeapTreeLeaf(const QByteArray& line);
bool parseheapTreeLeafInternal(const QByteArray& line, int depth);
+ QByteArray getLabel(const QByteArray& original);
+
Parser* m_parser;
QIODevice* m_file;
FileData* m_data;
@@ -119,6 +122,10 @@ private:
/// list of custom allocator wildcards
QList<QRegExp> m_allocators;
+ /// improve memory consumption by re-using known labels
+ /// and making use of the implicit sharing of QByteArrays
+ QSet<QByteArray> m_labels;
+
int m_expectedSnapshots;
};