View Full Version : HPC - Dynamic Code Switching

2012-Aug-19, 04:36 PM
This is a more advanced HPC optimization, that can only be done from languages that supports OO recasting, or calling function pointers. It is useful to replace lenghty switch or if-else if conditions. Or even non so lenghty ones when dealing with an often invoked portion of code.

It is easiest to accomplish in C# or Java, so will explain it from the C# point of view.

After identifying code of this nature that is a performance problem, or may become one, the idea is to reorganize into a set of objects sharing the same base object, with a common abstract method name, the build a collection of these into a dictionary with a lookup key (Uses B-Tree algorithym).

For this example, say you are dealing with code that needs to format adresses for display or printing differently for specific contries. The if-else if for this can be reorganized into objects then a dictionary with the following techique.

Definine your base object as a static abstract, with a static abstract method that will be common for all child objects. For this senerio abstract class AddressHandler abstract method String FormatAdress(type field1, ...., type field#).

Then define your static objects that will implement static FormatAddress and parse the fields into a countries specific format. class USAAddress, class CanadaAddress, ..., class UKAddress

In the code that will be using these, define a dictionary object with a lookup key of the contry code, and the formater for that code as the return type, using the base abstract class type as follows. I usualy define these types of dictionaries in a class library and the objects it uses, for reuse in main code.

Dictionary<string, AddressHandler> AddrFormatList = new Dictionary<string, AddressHandler>()
{"USA", (AddressHandler)USAAddress},
{"CA", (AddressHandler)CanadaAddress},
{"UK", (AddressHandler)UKAddress}

Then replace the long if-else if switching with just a few lines of code where needed.

AddressHandler Address = AddrFormatList.Item(CountryCode);
String FormattedAddress = Address.FormattAddress(field1, ..., field#);

The end result of this, is a significat increase in performance, as instead of going through a long list of condition in a switch like construct, in a serial manner, the specific code is found for a country, by a b-tree search on the dictionary. The longer the switch like construct is the more benefit you get from this technique.

However, this also is a case there the optimization, is also more maintainable, in that if you have to change one contries address format, you just alter that object, and don't have to search though a huge switch like construct for it.

Additionaly in the code that uses this, you've eleminated sometimes hundreds of lines of code, with just two. Making it much more readable for others coming after you that need to maintain it.

2012-Aug-19, 04:56 PM
One other small thing, the key for the dictionary can also be the types in an predefined enum as well.

You can also use this technique to improve converting from the types in a enum, to a matching string value and back, without using refelction.

2012-Aug-21, 04:59 PM
If you have a relatively small number of options, a hash table can be a better choice of an associative structure, and if you are limited to a known range of integers, you can use a simple flat array and almost completely remove the lookup overhead (compilers can do something very similar with a jump table to optimize switch statements). You can also use symbols/selectors/interned strings as keys, which map each unique string to a simpler ID. This requires a lookup at compile or startup time to get this identifier, but the overhead can then be amortized over the entire program run. In C/C++, it's simple to implement them as mapping strings to string pointers, other languages have built in support.

Either way, this is simple and has some benefits for code organization (it makes it simple to add handlers with a plugin, for example), but is not exactly high performance...you require a query to that handler dictionary every time you need to perform an operation. It would be more efficient to pass the handler as a parameter, or keep it in a variable somewhere. It is a useful technique for reading data from outside sources which contains its own specifications of which handlers are needed (for deserializing objects, responding to command messages sent over a network, etc). (And if you do that, make sure to check that there actually is a handler for the given key...)

2012-Dec-29, 07:15 PM
Csharp has a Generic called HashSet which I've used just recently. I haven't done a perfomance mark of it, but it's build with a binary-tree like dictionaries, so on larger sets, it should be a bit faster on lookup then hash tables.