Sunday, 9 December 2012

When performance matters: string concat



Due to the memory optimization in the StringBuilder class, the concatenation of strings can be 10 times faster or more than using the “+” operator of strings.
One of the reasons the + operator for strings is not the fastest way to perform this operation is because it does not actually conctatenetes two strings at the same time but allocates a new memory space for the previous stirng “Plus” the new one. That is say:

Text = “Hello”

This means the memory allocated an array of chars with the following content:
Memory Address X à{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}

However, when we perform the following operation:
Text += “, world.”

This time, the memory will look like follows:
Memory Address X à{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}
Memory Address Y à{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘,’, ‘ ’, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’, ‘\0’}

The original string is kept in its orginal memory position while the concat operation allocated a new memory space to put the new resulting string.

On the other hand, the StringBuilder class has an optimized use of memory allocation. As a result, it improves the Append algorithm by extenteding the allocated memory.
StringBuilder sb = new StringBuilder();
sb.Append(“Hello”);
Memory Address X à{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}
sb.Append(“Hello, world”);
Memory Address X à{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘,’, ‘ ’, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’, ‘\0’}

As a result, due to the better memory management of the StringBuilder class when concatenating strings, this is the best way to address any performance issue when concatenating.


Sunday, 29 April 2012

.Net assemblies' strong sign


This note is intended to help you on how to prevent third-party applications from accesing the features developed by us. This could be useful in the following cases:


We have a class that encrypts and decrypts files (which would prevent a third party to add our DLL as a reference to access these methods).


· Access to databases.
· Recovery of users' passwords.
· Etc ...


Generally, these points have the following specifications:


1. They must be public for all the assembly / executable.


2. It should not be possible to use these features just adding them as a reference.


As the solution, .Net Framework provides the internal keyword to make public methods, variables or classes but only within the assembly.
Although this can not be visible from "outside", there should be a way of declaring what are these projects likely to access the internals of this DLL / EXE.
Solution: Let's suppose we want to make visible all the internals of  Business.dll to Business.Test.dll since we will develop unit testing on the first. It should take the following steps:


1. Sign the Business assembly doing right click the 
Project --> Properties --> Signing --> tick the "Sign the assembly" --> In the drop-down combo select "New" and type in the signature file in our case can be "MySnk " will generate a file with a hex code, then you should always use this file to sign the assemblies of the company.


2. Sign the assembly Business.Test, but this time with the new file we created in the previous step. (MySnk.snk)


3. Now, both assemblies are signed with what is known as "Strong Sign". Keep the .snk file only in the development environment, because distributing it to any third party that could use it to sign your assemblies and lose the benefits of all the work that has being done. 
By signing with this file both assemblies are identical public key that gives them the fact of signing with the same file MySnk.snk What is the public key like?


3.1 Open the DOS console Visual Studio and we are positioned in the directory where the assembly we want to obtain the public key.


3.2 We use the command sn-Tp <assembly name>


4. Business within the Properties folder is a file called AssemblyInfo.cs. Add the following line:


[assembly: InternalsVisibleTo("Business.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010091b453266aaae77c66c5d412c0e1c1c8d10ab6321e6b9b51c8f3ab854d0b5f6194efc87076416c83624fc42975fe87e507acf96112865d01691a132813a8d63861085e879960cdff223cfd59b7fc9a3c7667f7870ecf432f4e205724815c1ada5fd684209c446c9c4ae16f17d064c7de4a960edb3a31124d916ec6ea07191ad0")]


Thus we declare the assembly Business with its visible internals to a certain assembly (Business.Test in this case) containing the same public key. In this, the will only be used by Business.Test and no other.