atk > documentation > techniques used to optimize the attack tool kit

Techniques used to optimize the Attack Tool Kit (draft)

This little paper should guide core developers of the Attack Tool Kit (ATK) how to write the source code of the software very efficiently. Interessted users will find background information how the utility is written and they will see, that there was spent a large amount of ressources to keep the software small and fast.

Checking for empty strings with LenB

It is often needed to check if a string is empty or not. Most people do this with the following if's:

If Variable = "" Then

If Variable <> "" Then

Well, it is much faster to use the len command to check the length of a string. If this length is zero the string must be empty.

There are to different commands available for checking the length of a string. Len checks the length of a string in characters. This means

Len("Test")
Would be 4 because the string "Test" has 4 characters.

Much faster is the binary length checking. The command

LenB("Test")
Does the same as the usual Len command. But the result of this len checking is twice the count of the characters. The result of the LenB would be 8 because the string "Test" has 4 characters and these were counted twice.

The replacement is essentially risk-free; especially if the Len is used to check the emptyness of a string. Everywhere where the faster LenB command could replace the traditional Len command the replacement is done. But you have to keep in mind that the result of LenB is always multiplied with two.

VB3 and VB.NET don't have the LenB alternative, in these languages you should use Len.

Check for input before work with it

There are some very ressource intensive functions and other that don't use much ressources. Everytime when a ressource intensive function has to be used, it should first be checked, if this function is really necessery. A good example is checking the existence of a string with InStrB before replacing a substring with Replace:

If InStrB("Test", "est") <> 0 Then
   String = Replace("Test", "est", "ime")
End If
Compare in binary

There are two different ways how comparisation of two strings could be done. [...]

Use vbTextCompare instead of using UCase/LCase

[...]

Using nullstrings instead of empty strings

If a variable should be resettet mostly the command

String = ""
is used. The empty string "" takes 6 bytes of RAM each time you use it. This is absolutly wasted because the command
String = vbNullString
has the same effect, but don't wastes ressources and is much faster. vbNullString is a special VB constant that denotes a null string. An empty string is a real string. A null string is not. It is just a zero. If you know the C language, vbNullString is the equivalent of NULL.

The web site http://www.aivosto.com/vbtips/stringopt.html gives an very important note on using null strings:

If you call some non-VB API or component, test the calls with vbNullString before distributing your application. The function you're calling might not check for a NULL string, in which case it might crash. Non-VB functions should check for NULL before processing a string parameter. With bad luck, the particular function you're calling does not do that. In this case, use "". Usually APIs do support vbNullString and they can even perform better with it!
Don't declare variables as variants

[...]

Don't use variant functions

[...]

Work with unicode

[...]

Use variables and don't refer to elements

[...]