Proposal KLatin1String

Dr. Juergen Pfennig info at j-pfennig.de
Sun Mar 28 19:45:11 CEST 2004


Please wait a second and consider a marriage!

I once started another little thread "String Cache" where some people said 
that would look like StaticString or whatever. I have already written some 
code and I have been benchmarking things. My new class would be called 

KStringBuilder

and yes, it partially follows the "StringBuilder" pattern from Java/.NET. This 
could nicely be married with a "KLatin1String" class, but compared to 
QCString the KStringBuilder has more functionality (but not in the sense of a 
general purpose string class unless married to KLatin1String).  With GNU3.x + 
Intel P IV KStringBuilder can in all situations be slightly faster than 
QString::fromLatin1() - but that's simply the result of carefull coding. 

The real benefit of the KStringBuilder comes when programmers do operations 
while building a QString from Latin1 fragments using sprintf or 
concatenation.  The magic "woodo" here is a zero-malloc garbage-collection 
like implementation.

The advanced features of KStringBuilder would be optional transparent Latin1 
string caching so that  attempts to create QStrings with identical Latin1 
contents would result in identical QStrings (saves malloc/free calls and 
saves memory). KStringBuilder has very little overhead which makes it 
effective in cases with >= 5% hit rate. Applications that create tables or 
listings or parse things like INI files or XML files could use this.

Another feature is that KStringBuilder might be used to build String/Atom 
Tables (do you remember Windows Atoms?). Such Atoms are nice if you are 
building parsers. This feature comes for free - almost no code to be added.

KStringBuilder itself is a lightweight object, it works with one or two memory 
blocks. If used in cache or atom mode it does very few mallocs and uses some 
garbage collection like memory management. Programmers use one KStringBuilder 
instance to create one or more QStrings. A KStringBuilder can be created on 
stack without pain. Consider the example:

  if( bla )
  {  KStringBuilder sb;
     sb.text("Ottos Mops");
     sb +=  32;
     sb += "kotzt";
     list.append( sb.qstring() );

     sb.sprint("Ottos Mops %s", kotzt);
     list.append( sb.qstring() );
  }

In the example the two QString objects passed to append will refer to the same 
share data.

The traditional KDE StaticString could be reimplemented using a KStringBuilder 
and would become more efficient.

Jürgen


More information about the Kde-optimize mailing list