Tuesday, March 10, 2009

Delphi Performance Tip

Here's a small little Delphi performance tip that I'm not sure many people are aware of. Putting the $EXTERNALSYM after the symbol has better compiler performance than placing it before the symbol. The reason for this is pretty simple. When the compiler encounters a $EXTERNALSYM it looks for the symbol. If the symbol isn't found then it must be stored and dealt with later. If the symbol is found it is used immediately.

Here's a small example:


const
MAPVK_VK_TO_VSC = 0;
{$EXTERNALSYM MAPVK_VK_TO_VSC}
MAPVK_VSC_TO_VK = 1;
{$EXTERNALSYM MAPVK_VSC_TO_VK}
MAPVK_VK_TO_CHAR = 2;
{$EXTERNALSYM MAPVK_VK_TO_CHAR}
MAPVK_VSC_TO_VK_EX = 3;
{$EXTERNALSYM MAPVK_VSC_TO_VK_EX}
MAPVK_VK_TO_VSC_EX = 4;
{$EXTERNALSYM MAPVK_VK_TO_VSC_EX}

2 comments:

Anonymous said...

I'm checking this out because it was in David I's latest emailing.

This tip sounds interesting, but it would be far more useful if it were explained what this is talking about and what it's for.

Chris Bensen said...

If you translate a C/C++ header file, as we often do with the Windows SDK or groups such as JEDI (http://www.delphi-jedi.org/) then you will probably use $EXTERNALSYM. This brief post and example illustrate, albeit to those who already understand and use $EXTERNALSYM, a small performance increase when locating it after the symbol vs before the symbol. So it really isn't a tip for everyone, just those who translate header files.

Post a Comment