i have a function who’s job is to convert an ADO Recordset into html:
class function RecordsetToHtml(const rs: _Recordset): WideString;
And the guts of the function involves a lot of wide string concatenation:
while not rs.EOF do
begin
Result := Result+CRLF+
'<TR>';
for i := 0 to rs.Fields.Count-1 do
Result := Result+'<TD>'+VarAsWideString(rs.Fields[i].Value)+'</TD>';
Result := Result+'</TR>';
rs.MoveNext;
end;
With a few thousand results, the function takes, what any user would feel, is too long to run. The Delphi Sampling Profiler shows that 99.3% of the time is spent in widestring concatenation (@WStrCatN and @WstrCat).
Can anyone think of a way to improve widestring concatenation? i don’t think Delphi 5 has any kind of string builder. And Format doesn’t support Unicode.
And to make sure nobody tries to weasel out: pretend you are implementing the interface:
IRecordsetToHtml = interface(IUnknown)
function RecordsetToHtml(const rs: _Recordset): WideString;
end;
Update One
I thought of using an IXMLDOMDocument, to build up the HTML as xml. But then i realized that the final HTML would be xhtml and not html – a subtle, but important, difference.
Update Two
Microsoft knowledge base article: How To Improve String Concatenation Performance
i found the best solution. The open source HtmlParser for Delphi, has a helper
TStringBuilderclass. It is internally used to build what he callsDomStrings, which is actually an alias ofWideString:With a little bit of fiddling of his class:
The guts of the routine becomes:
The code then feels to run infinitely afaster. Profiling shows much improvement; the
WideStringmanipulation and length-counting became negligible. In its place was FastMM’s own internal operations.Notes
VarAsStringrather thanVarAsWideString)