KGlobal::staticQString current recomendation harmful?
Roger Larsson
kde-optimize@mail.kde.org
Sun, 12 Jan 2003 04:48:25 +0100
--Boundary-00=_JWOI+aE/+64zwP0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Hi,
=46rom the documentation.
/**
* Creates a static QString.
*
* To be used inside functions(!) like:
* <pre>
* static const QString &myString =3D KGlobal::staticQString("myText");
* </pre>
*
[CONTINUED later]
"Use inside of functionS"
OK, so what if I have 100 functions that uses the same string?
then the function staticQString need to run 100 times at startup.
later when running it is no problem but... startup time is an issue too!
[Current CVS with my run have 251 misses and 171 hits=20
- pretty bad hit rate that is, patch included]
I have noticed that most uses are local inside a specific file, and many a=
re
only used once.
An alternative is to put
static const QString myString("myText");
in one global location in the file.
Or if it needs to be used from outside of the file it can be put in the
class, even as reference.
KGlobal is probably OK for frequently used strings that really are spread o=
ut=20
all over the place... but still it should only be used in one place per fil=
e.
* !!! Do _NOT_ use: !!!
* <pre>
* static QString myString =3D KGlobal::staticQString("myText");
* </pre>
* This creates a static object (instead of a static reference)
* and as you know static objects are EVIL.
A static object in each function can be EVIL, but I fail to see why static
objects in general are EVIL.
/RogerL
=2D-=20
Roger Larsson
Skellefte=E5
Sweden
--Boundary-00=_JWOI+aE/+64zwP0
Content-Type: text/x-diff;
charset="iso-8859-1";
name="kglobal-hitrate.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="kglobal-hitrate.patch"
Index: kglobal.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kglobal.cpp,v
retrieving revision 1.66
diff -u -3 -p -r1.66 kglobal.cpp
--- kglobal.cpp 26 Jan 2002 12:14:35 -0000 1.66
+++ kglobal.cpp 12 Jan 2003 03:41:22 -0000
@@ -145,7 +145,12 @@ KGlobal::staticQString(const QString &st
{
result = new QString(str);
_stringDict->insert(str, result);
+ qWarning("staticQString: cache miss (%s)", str.latin1());
}
+ else
+ {
+ qWarning("staticQString: cache hit (%s)", str.latin1());
+ }
return *result;
}
--Boundary-00=_JWOI+aE/+64zwP0--