Skip to content

Instantly share code, notes, and snippets.

@ariya
Created November 12, 2011 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariya/1360955 to your computer and use it in GitHub Desktop.
Save ariya/1360955 to your computer and use it in GitHub Desktop.
Patch for QDom
Index: qdom.cpp
===================================================================
--- qdom.cpp (revision 599821)
+++ qdom.cpp (working copy)
@@ -40,6 +40,15 @@
#include <qdebug.h>
#include <stdio.h>
+/**
+ Additional feature by Ariya Hidayat (ariya@kde.org)
+
+ If this is defined, QDom tries to cache as many strings as possible.
+ Because QString is implicity shared, this reduced memory comsumption
+ for a very large XML document, e.g. files in OpenDocument format.
+ */
+#define QDOM_CACHE_STRING 1
+
/*
### old todo comments -- I don't know if they still apply...
@@ -520,6 +529,25 @@
// Variables
QDomImplementationPrivate* impl;
QDomDocumentTypePrivate* type;
+
+ // To cache tag name, prefix, attribute name
+#ifdef QDOM_CACHE_STRING
+ QMap<QString,int> stringHash;
+ QStringList stringList;
+ QString cacheString(const QString& str)
+ {
+ const int& ii = stringHash[str];
+ if(ii > 0)
+ return stringList.at( ii );
+
+ // not yet declared, so we add it
+ int i = stringList.count();
+ stringList.append( str );
+ stringHash.insert( str, i );
+
+ return str;
+ }
+#endif
};
/**************************************************************
@@ -4533,17 +4561,22 @@
{
QString prefix, localName;
qt_split_namespace(prefix, localName, qName, true);
+#ifdef QDOM_CACHE_STRING
+ QString val = ownerDocument()->cacheString(newValue);
+#else
+ QString val = newValue;
+#endif
QDomNodePrivate* n = m_attr->namedItemNS(nsURI, localName);
if (!n) {
n = new QDomAttrPrivate(ownerDocument(), this, nsURI, qName);
- n->setNodeValue(newValue);
+ n->setNodeValue(val);
// Referencing is done by the map, so we set the reference counter back
// to 0 here. This is ok since we created the QDomAttrPrivate.
n->ref.deref();
m_attr->setNamedItem(n);
} else {
- n->setNodeValue(newValue);
+ n->setNodeValue(val);
n->prefix = prefix;
}
}
@@ -6231,6 +6264,10 @@
delete type;
impl = 0;
type = 0;
+#ifdef QDOM_CACHE_STRING
+ stringHash.clear();
+ stringList.clear();
+#endif
QDomNodePrivate::clear();
}
@@ -6296,7 +6333,11 @@
QString fixedName = fixedXmlName(tagName, &ok);
if (!ok)
return 0;
-
+
+#ifdef QDOM_CACHE_STRING
+ fixedName = cacheString(fixedName);
+#endif
+
QDomElementPrivate *e = new QDomElementPrivate(this, 0, fixedName);
e->ref.deref();
return e;
@@ -6309,7 +6350,14 @@
if (!ok)
return 0;
- QDomElementPrivate *e = new QDomElementPrivate(this, 0, nsURI, fixedName);
+#ifdef QDOM_CACHE_STRING
+ fixedName = cacheString(fixedName);
+ QString ns = cacheString(nsURI);
+#else
+ QString ns = nsURI;
+#endif
+
+ QDomElementPrivate *e = new QDomElementPrivate(this, 0, ns, fixedName);
e->ref.deref();
return e;
}
@@ -6383,6 +6431,10 @@
if (!ok)
return 0;
+#ifdef QDOM_CACHE_STRING
+ fixedName = cacheString(fixedName);
+#endif
+
QDomAttrPrivate *a = new QDomAttrPrivate(this, 0, fixedName);
a->ref.deref();
return a;
@@ -6395,7 +6447,14 @@
if (!ok)
return 0;
- QDomAttrPrivate *a = new QDomAttrPrivate(this, 0, nsURI, fixedName);
+#ifdef QDOM_CACHE_STRING
+ fixedName = cacheString(fixedName);
+ QString ns = cacheString(nsURI);
+#else
+ QString ns = nsURI;
+#endif
+
+ QDomAttrPrivate *a = new QDomAttrPrivate(this, 0, ns, fixedName);
a->ref.deref();
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment