The Ensure Calls

Another set of "hidden" operations added by the compiler is the Ensure String family of calls, added to check the code page of a string parameter, eventually triggering a conversion if it doesn't match. The most relevant of these calls is EnsureUnicodeString.

As it is very hard to achieve an incorrect situation in Delphi (short of a direct cast to the wrong string type), you might wonder why these were added. The reason is that when a UnicodeString is managed by C++Builder code, things can go wrong.

To make things safe, the compiler adds these extra checks in many different places, particularly when working on a string passed as parameter. While this is the default behavior, you can compile specific code (and libraries)

43 These "diagnostic warnings", like the pointer and type safety ones introduced to check for .NET compatibility, can be kept off by default. Even though I'm a proponent of the rule "keep all warnings on and try to compile programs with no hints and warnings" I agree that "diagnostic warnings" should be treated differently.

disabling this setting, using the --string-checks compiler options or the $stringchecks directive. Given that you have to use them at your own risk, these switches are not documented and not officially supported (although, oddly enough, they are directly available in the project options). Still as potential issues are very limited when using Delphi (and not C++Builder), you can probably turn them off as a regular speedup technique.

For example, if you write the following one liner:

function DoubleLengthUpperOn(

const S: UnicodeString): Integer; begi n

Result := Length(AnsiUppercase(S)); end;

and compile this and an identical version with a different name, with the string checks on and off, and later call them within a loop of 10 million iterations, you'll see the following timing:

UpperOn: 00:02.202 UpperOff: 00:02.l59

This seems very modest, and quite irrelevant. However, if you make the actual code of the functions blazingly fast (for example by removing the AnsiUpperCase call), the results for 10 times more iterations becomes:

In this case the difference is significant (ten times as much) and seems linear with the number of iterations of the loop. Even if similar cases will be marginal, this is probably a good reason to keep this setting turned off.

For more information on this setting and its effect in terms of generated assembly code, you can refer to the following blog entry by Jan Goyvaerts:

http://www.micro-isv.asia/2008/l0/needless-string-checks-with-ensureunicodestring

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment