Speedup Execution

Parent Previous Next

Speedup Execution.

When a VM executes a virtual assembler instruction, it uses more processing power than the equivalent native assembler instruction as described in Speed or Security. The loss of speed cannot be avoided due to the amount of security related clock cycles.


However, as a programmer you can reduce the impact, if you program with the following tips in mind.


Working With Encrypted Values.

SCRAMBLECODE has the ability to execute certain operations without having to decrypt the involved variables or operands, making the operations both fast and secure.


Simple assignments are part of those operations. If you have a simple assignment  A := B  then the VM can just copy the bytes of B. Decryption is not needed. The VM trusts the compiler and knows, that even if the VM does not know the types of A or B, the content of B will be acceptable to A.


This is not the case for the assignment  A += B  which actually is the same as  A := A + B  that will require both A and B to be decrypted in order to be added. The result is finally encrypted and stored in A.


The impact of this is particular important when working with variables containing either a big string or blob. If such a big variable is used repeatedly, then you should consider breaking it into smaller pieces, and work with the smaller pieces individually, e.g breaking a big file text into an array of text lines.


When you add a value to an array like  Arr[].Add(B)  it is not necessary for the VM to decrypt B. Again the VM trusts the compiler and simply copies the encrypted bytes of B.


Example:

Here is demonstrated, how to create a big string in a loop, getting the same result using two different methods. In this example method 2 is approx. 10 times faster than method 1. This speed-relationship is not a fixed factor. If the size of the resulting string and/or the number of loop repetitions are increased, this speed-relationship will increase as well, but for smaller strings or fewer repetitions it will decrease, because the array and the library function add some overhead.



   Integer I, RepMax := 10000;

   String  A, strArr[].New();

   String  B := "B";


   //Method 1:

   //Slow execution because A and B are decrypted and

   //A is encrypted on each loop repetition, and as A

   //gets bigger, so does the processing time for A.

   For I := 1 To RepMax

   {

     A += B;

   }


   //Method 2:

   //Faster execution because A is not constantly

   //being decrypted and encrypted. Actually A is

   //never decrypted and only encrypted once.

   For I := 1 To RepMax

   {

     strArr[].Add(B);

   }

   A := @StringBuild(strArr[], "");



Please note: This example is constructed to demonstrate a point. Speed differences between different algorithms in real life applications are usually harder to pinpoint and explain. However, if you experience slow execution in some part of your code, you should always be on the lookout for variables containing large strings (or blobs) that are being used in a very repetitive manner, as the repeated decryption and encryption can impact performance severely.


TIP: When working with big blobs, it can be advantageous to split it into smaller blobs - just like working with strings. But you should not replace a blob with an array of Byte items like  Byte[] b; . A blob usually performs much faster than a byte array and has many library functions available.