On Friday I was reading Jeff Atwood (of Coding Horror fame) post on micro-optimization, which is basically a post on using strings to create HTML. The core of the article revolves around the problem caused by the fact that "In most garbage collected languages, strings are immutable: when you add two strings, the contents of both are copied." This is certainly true for .NET. Jeff goes on covering Shlemiel the painter, showing how code can be improved, and realizing this his optimizations (for a limited set of string concatenations) made little difference. In the end, Jeff concludes "you should be more worried about the maintainability and readability of your code than its performance."

My first thought was, good luck I use Delphi, which is very fast on string concatenation, so I don't have to worry at all. And I can have code that is readable and fast at the same time. I partially disagree with the fact this optimizations are not relevant when, for example, you start cranking out XML (whcih I tend to do a lot). Think about 500 rows with 30 fields requiring each an open and closed node (so you have three strings for each node), and you are concatenating 50,000 strings. That might be for a single AJAX response...

In any case, what drew my attention was a comment: "Since the html/xml/etc explosion I've been waiting to see the emergence of a string-based language". In fact, there are other comments saying how the Java runtime will automatically optimize string concatenations, others claiming you don't worry about performance when using C or C++... and this made me realize, all of a sudden, we Delphi developer leave in a sort of string beauty world.

Strings in Delphi are a native and easy to use, unlike C or C++. Strings in Delphi have copy-on-write semantics. When passing strings as parameters you can pick standard reference counting and copy-on-write, opt for pass by reference, or choose pass-by-const to avoid the overhead of reference counting (and that's really a micro-optimization, but so nicely built into the language that it is worth using it). Strings concatenation in Delphi is so fast that the new optimized StringBuilder class in Delphi 2009 cannot beat it. Strings in Delphi 2009 are fully UTF-16 enabled, with a minimal overhead you can get rid of with a compiler setting. Strings in Delphi are really great.

What about other languages? Scripting langauge lack the speed of the compiled code for string manipulation (but have much larger libraries... when will we have a large string processing library in the core Delphi RTL?), garbage collected languages don't like concatenations and offer much less flexibility in terms of semantics, C and C++ require low-level pointer-based and far from safe code...

Is there any language out there that matches Delphi strings in terms of both native implementation, readability, flexibilitym and speed? I really doubt, but might be wrong, of course. We are so used to the power of the string type we have in Delphi that we often forget how great it is.