Unusual C++ use of #include

Wanted to share with you an unusual way of using the very well-known #include C++ preprocessor directive.

"The #include directive causes a copy of a specified file to be included in place of the directive" – C++ How To Program

The usual use is to write it at the very top of your .h/.cpp files to include other header files like (Windows.h, iostream, cstdio, etc..) to use the what is defined inside in your .h/.cpp file.

The unusual use is to use it to initialize a data-structure like arrays by including a text file that contains the array initialization data between the array initializer list parentheses – e.g XX XXX[] = { <HERE> }. This is is illustrated in the sample program below. The same concept can be used to initialize an array of any dimension.

A Simple Sample

// main.cpp
#include <cstdio>
#include <Windows.h>

struct Person
{
    char        *pName;
    unsigned    Age;
    unsigned    Height;
    char        Gender;
};

// Declare and initialize a 1D array of Persons structs using PersonTableData text file
// #include "PersonsTableData" will be expanded at COMPILE TIME to the content of PersonsTableData file
Person PersonsTable[] = {
    #include "PersonsTableData"
};

int main()
{
    for (int i = 0; i < _countof(PersonsTable); ++i)
    {
        Person &person = PersonsTable[i];

        printf("%s\t%d\t%d\t%c\n", person.pName, person.Age, person.Height, person.Gender);
    }

    return 0;
}

 

PersonsTableData text file content

imageimage

Output

image

 

 

 

 

 

Remember

It is not about how many programming constructs you memorize of your preferred programming language, it is about how can you utilize the constructs you know for your own good.

C++ OOP Performance Tips–Part 1

We often encounter inheritance and composition implementations that are too flexible and too generic for the problem domain. They may perform computations that are rarely or never required. In practice, it is not surprising to discover performance overhead associated with inheritance and composition. This is a tradeoff between code reuse and performance. Oftentimes, reusable code will
compute things you don’t really need in a specific scenario. Any time you call functions that do more than you really need, you will take a performance hit. This article will highlight some of these scenarios.

This is not to say that inheritance is fundamentally a performance obstacle. We must make a distinction between the overall computational cost, required cost, and computational penalty. The overall computational cost is the set of all instructions executed in a computation. The required cost is that subset of instructions whose results are necessary. This part of the computation is mandatory; computational penalty = overall computational costrequired cost. This is the part of the computation that could have been eliminated by an alternative design or implementation. (e.g inheritance hierarchy, composition layout, etc…)

So we cannot make a blanket statement that complex inheritance designs are necessarily bad, nor do they always carry a performance penalty. All we can say is that overall cost grows with the size of the derivation tree. If all those computations are valuable then it is all required cost. In practice, inheritance hierarchies are not likely to be perfect. In that case, they are likely to impose a computational penalty.

Constructors and Destructors

Following is a snippet from Visual Studio 2008 Disassembly Window for a simple Foo() function creating Point object locally on the stack and destroying it on returning.

class Point
{
public:
    Point() : X(0), Y(0) { cout << "Hello!\n"; }
    ~Point() { cout << "Bye bye\n"; }
    int X;
    int Y;
};

void Foo()
{
010820B0  push        ebp  
010820B1  mov         ebp,esp 
010820B3  sub         esp,8 
    Point obj;
010820B6  lea         ecx,[obj] // Pass obj address to the constructor 
010820B9  call        Point::Point (10813ACh) // Call Constructor 
}
010820BE  lea         ecx,[obj] // Pass obj address to the destructor
010820C1  call        Point::~Point (10813B1h) // Call Destructor
010820C6  xor         eax,eax 
010820C8  mov         esp,ebp 
010820CA  pop         ebp  
010820CB  ret             

The translated assembly instructions of each C++ statement (in black) appears directly below it. As you can see, constructors and destructors (in Red) are normal functions like any C++ function. The compiler knows exactly where and when to call them. A constructor or destructor call costs 2 assembly instructions.

The following is the code generated for an empty Point constructor, total of 8 assembly instructions.

class Point
{
public:
    Point() 
00E12960  push        ebp  
00E12961  mov         ebp,esp 
00E12963  push        ecx  
00E12964  mov         dword ptr [ebp-4],ecx 
    {
        
    }
00E12967  mov         eax,dword ptr [this] 
00E1296A  mov         esp,ebp 
00E1296C  pop         ebp  
00E1296D  ret   

The following is the code generated for an empty Point destructor, total of 7 assembly instructions.

    ~Point()
    {
00E12580  push        ebp  
00E12581  mov         ebp,esp 
00E12583  push        ecx  
00E12584  mov         dword ptr [ebp-4],ecx   
    }
00E12587  mov         esp,ebp 
00E12589  pop         ebp  
00E1258A  ret        

Let’s calculate a base-line for the number of assembly instructions required to construct/destruct a Point object:

  1. 2 instructions to call Point constructor Point::Point().
  2. 8 instructions to implement Point empty constructor body.
  3. 2 instructions to call Point destructor Point:~Point()
  4. 7 instructions to implement Point empty destructor body.
  5. Total of 2 + 8 + 2 + 7 = 19 instruction to construct/destruct a Point object!
    Enough theory, let’s get our hands dirty and write some code and figure out the effect of these extra 19 instruction.
    Consider the code snippet below (Version 0):
// Version 0
Point dummy
; for (int i = 0; i < 100000000; ++i) { Point p; p.X = p.Y = i; dummy.X = p.X; dummy.Y = p.Y; }

The above code doesn’t make sense in a real-life program, and it is meaningless, but it will help illustrate our theory.  We will focus only on the Point object construction/destruction. As you can see, each iteration costs 19 instruction (mentioned up) per Point construction/destruction, and the overall cost for construction/destruction in the previous for-loop is 20 multiplied by the number f iterations which is 100 million ~= 1900000000 instruction only for Point construction/destruction.  Note that we didn’t calculate the instructions required for p and dummy objects members initialization because they are irrelevant to our case-study. We care only for Point construction/destruction.

A make-sense optimization for the previous for loop is to bring the Point p; line outside the for loop so that the Point object is constructed/destructed only once. In theory, the overall construction/destruction should drop from 1900000000 to 20 ! We can call this overhead: “Silent Overhead”.

The optimized for loop will look as follows (Version 1):

// Version 1
Point p
; Point dummy; for (int i = 0; i < 100000000; ++i) { p.X = p.Y = i; dummy.X = p.X; dummy.Y = p.Y; }

Results

We chose number of iterations to be a rather very high 100000000 iteration to highlight the performance drop. Our experimental computer is equipped with a very fast Intel Core i7 processor with 8 logical processors. And 1 million iteration is too small for it, the i7 was able to execute 1 million iteration in 2 milliseconds in average !! That’s why we chose a very high number of iterations to scale this difference.

image

For Version 0 and Version 1, We ran the for-loop for 100 million iteration and take the average running time for 100 sample. The optimized Version 1 is approximately 3 times faster than Version 0 .

From the previous experiment, It is obvious that object construction/destruction can lead to a big drop in performance if they are called unnecessarily as in code snippet Version 0.

Key Points

  1. Watch out for the combinatorial explosion of created objects in complex hierarchies. The construction (destruction) of an object triggers recursive construction (destruction) of parent and member objects.
  2. Defer object construction (i.e local variable declaration, dynamic object allocation) to the scope in which it is manipulated. The object life cycle is not free of cost. At the very least, construction and destruction of an object may consume CPU cycles. Don’t create an object unless you are going to use it.
  3. Use the object constructor initialization list to complete the member object creation. Because compilers must initialize contained member objects prior to entering the constructor body. This will save the overhead of calling the assignment operator later in the constructor body. In some cases, it will also avoid the generation of temporary objects.

Do/ Don’t Do

Do this:

void Foo(int n)
{
    if (n == 0)
        return;
    else
    {
        HeavyClass heavyObj;
        heavyObj.DoWork();
    }
}

Don’t do this:

void Foo(int n)
{
    HeavyClass heavyObj;
    
    if (n == 0)
        return;
    else
    {
        heavyObj.DoWork();
    }
}

Do this:

class Person
{
public:
    Person(const char* name) : Name(name) {}
    string Name;
};

Don’t do this:

class Person
{
public:
    Person(const char* name) /* Name constructor called */
    {
        Name = name;
    }
    string Name;
};

Reference

  • Efficient C++ Performance Programming Techniques book By Dov Bulka, David Mayhew

Roots of Software Inefficiency

Being a GEEK ACMer or TopCoder beast (i.e design and analysis of algorithms) is about writing an efficient algorithm in the scope of 3 or 2 functions doing a very specific and limited task. However, writing a software that runs on customers’ computers is bigger than this. A typical windows commercial mid-sized software consists of a set of Executable, Services and DLLs interacting with each other to shape what is called software. The efficiency of algorithms and data structures is necessary but not sufficient: By itself, it does not guarantee good overall program efficiency. It is important to know the roots of software inefficiency if we care about writing fast one.

What are the factors that affect efficiency? Efficient C++ Performance Programming Techniques book By Dov Bulka, David Mayhew made a very good high-level categorization to these factors:

Design Efficiency This involves the program’s high-level design. To fix performance problems at that level you must understand the program’s big picture.  We are talking here about software architecture, UML diagrams, pseudo codes, algorithms, data-structures, and anything you can consider it language independent.

Code Efficiency Small-to medium-scale implementation issues fall into this category. Fixing performance in this category generally involves local modifications. For example, you do not need to look very far into a code fragment in order to lift a constant expression out of a loop and prevent redundant computations. The code fragment you need to understand is limited in scope to the loop body.

Both these 2 levels can be broken down more:

1. Design

1.1 Algorithms and Data Structures Technically speaking, every program is an algorithm in itself. Referring to “algorithms and data structures” actually refers to the well-known subset of algorithms for accessing, searching, sorting, compressing, and otherwise manipulating large collections of data. Oftentimes performance automatically is associated with the efficiency of the algorithms and data structures used in a program, as if nothing else matters which is inaccurate.

1.2 Program Decomposition This involves decomposition of the overall task into communicating subtasks, object hierarchies, functions, data, and function flow. It is the program’s high-level design and includes component design as well as inter-component communication. Few programs consist of a single component. A typical Web application interacts (via API) with a Web server, TCP sockets, and a database, at the very least.

2. Coding

2.1 Language Constructs A programming language is a tool we use to express to computers how to do a specific task. Whether you use C++, C#, Java and you care about performance, you need understand the cost of your programming language constructs so not to be shocked at run time when you program scale. C++ adds power and flexibility to its C ancestor (i.e Object Oriented capabilities). These added benefits do not come for free—some C++ language constructs may produce overhead in exchange.

2.2 System Architecture System designers invest considerable effort to present the programmer with an idealistic view of the system: infinite memory, dedicated CPU, parallel thread execution, and uniform-cost memory access. Of course, none of these is true—it just feels that way. Developing software free of system architecture considerations is also convenient. To achieve high performance, however, these architectural issues cannot be ignored since they can impact performance drastically. When it comes to performance we must bear in mind that:

  • Memory is not infinite. It is the virtual memory system that makes it appear that way.
  • The cost of memory access is non-uniform. There are orders of magnitude difference among cache, main memory, and disk access.
  • Our program does not have a dedicated CPU. We get a time slice only once in a while.
  • On a uniprocessor machine, parallel threads do not truly execute in parallel—they take turns.

If you write Windows software, you need read well about WinAPI and dig deep in Windows programming world, to understand how your host operating system – Windows in this case – will execute your program. This applies if you write software for Linux, iOS or any operating system. Good understanding of the host operating system is a must.

2.3 Libraries The choice of libraries used by an implementation can also affect performance. For starters, some libraries may perform a task faster than others. Because you typically don’t have access to the library’s source code, it is hard to tell how library calls implement their services. For example, to convert an integer to a character string, you can choose between
sprintf(string, “%d”, i); or an integer-to-ASCII function call, itoa(i, string); Which one is more efficient? Is the difference significant?

There is also the option of rolling your own version even if a particular service is already available in a library. Libraries are often designed with flexibility and reusability in mind. Often, flexibility and reusability trade off with performance. If, for some critical code fragment, you choose to put performance considerations above the other two, it might be reasonable to override a library service with your own home-grown implementation. Applications are so diverse in their specific needs, it is hard to design a library that will be the perfect solution for everybody, everywhere, all the time.

2.4 Compiler Optimizations Simply a more descriptive name than “miscellaneous,” this category includes all those small coding tricks that don’t fit in the other coding categories, such as loop unrolling, lifting constant expressions out of loops, and similar techniques for elimination of computational redundancies. Most compilers will perform many of those optimizations for you. But you cannot count on any specific compiler to perform a specific optimization.For ultimate control, you have to take coding matters into your own hands.

I remember how Visual Studio saved my team in an Image Processing performance competition in my faculty. In this competition your image processing package has to run many image processing algorithms and your package timing in each algorithm is used to rank it among the others. We optimized some of our algorithms manually, but didn’t have much time to optimize the others. I got an evil idea of enabling Visual Studio code optimization, and I was shocked by the results. The running time of many algorithms dropped down greatly and I couldn’t believe how C++ code optimization held by the compiler can be that effective.

Conclusion

  1. Teach yourself how to design and analyze algorithms and practice well (i.e problem solving through ACM Online Judge and TopCoder)
  2. Pick a programming language and master it (i.e read about its internals and understand the scary dark side of it).
  3. Know the internals of a certain operating system on which you prefer to write your software (e.g Windows, Linux or Mac OS programming).
  4. Write big multi-file, multi-module projects with real requirements.

Changing Thread Path of Execution

Every thread has a context structure, which is maintained inside the thread’s kernel object. This context structure reflects the state of the thread’s CPU registers when the thread was last executing.

Every 20 milliseconds or so (as returned by the second parameter of the GetSystemTimeAdjustment function), Windows looks at all the thread kernel objects currently in existence. Of these objects, only some are considered schedulable. Windows selects one of the schedulable thread kernel objects and loads the CPU’s registers with the values that were last saved in the thread’s context. This action is called a context switch.

The code primary thread (main function) below creates a new thread where its entry point is ThreadFunc1, and while it is running it suspends this secondary and changes its path of execution to the address of another function.

Code

DWORD WINAPI ThreadFunc1(PVOID pvParam)
{
    _tprintf_s(_T("I am ThreadFunc1\n"));
    while(1)
    {

    }
    _tprintf_s(_T("Exiting ThreadFunc1\n"));

    return 0;
}

DWORD WINAPI ThreadFunc2(PVOID pvParam)
{
    _tprintf_s(_T("I am ThreadFunc2\n"));
    while(1)
    {

    }
    _tprintf_s(_T("Exiting ThreadFunc2\n"));
   
    return 0;
}

int _tmain(int argc, TCHAR* argv[])
{
    // create a new thread with ThreadFunc1 as its entry-point
    HANDLE hThread = chBEGINTHREADEX(NULL, 0, ThreadFunc1, NULL, 0, NULL);

    if(!hThread)
        PrintLastError();

    // lets give the thread some time to do some work
    Sleep(2000);

    SuspendThread(hThread);

    CONTEXT cThread;
    // get control registers such as EIP (instruction pointer)
    cThread.ContextFlags = CONTEXT_CONTROL;
    GetThreadContext(hThread, &cThread);

    // change the target thread path of execution to ThreadFunc2
    cThread.Eip = (DWORD)ThreadFunc2;
    SetThreadContext(hThread, &cThread);

    ResumeThread(hThread);

    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);

    return 0;
}

Output

image

Events and Delegates: A C++ Implementation

Hi geeks! Hope for you a good coding and debugging day, free of Access Violation and Linkage errors.

First of all, Who should read this article?

  • C++ geek, who dreams of pointers, watches "Bug attack" movies, and prefers to trace the call stack all the night rather than going out with some friends.
  • C# cool dude, sick of Microsoft babysitting, and willing to move to C++, and aware of the trade-offs.
  • Java bean, that never heard of pointer to function concept, and wants to know how C++/C# guys suffer.

I have been using C# for around 2 years. I was totally fascinated by C# object oriented architecture. Events and delegates are of the most C# constructs I liked. However, there is a big trade-off here i.e using delegates in C#, it is flexibility and performance. Delegates in C# are no function to pointers rather they are implemented somehow using reflection. I was shocked when I read an article called Writing Faster Managed Code: Know what things cost, this article is talking about the cost of most common C# operations. You will be surprised when you find that in C# a function call using a delegate is slower 6 times than simply calling a function using its signature. The function call using its signature costs around 6 ns (nano second), while using delegate costs around 40 ns! If you are a normal geek then you should be shocked now. As a consequence to this shock, I don’t know how it happened, but months ago I felt that C++ is calling me. I started to code everything using C++. The geek inside me is controlling! Help me.

Coding C++ again after 1 year of pure C#ing is not easy. I was used to delegates and events heavily in my code to maintain a decoupling between classes. I couldn’t find something like: Click += new Handler(ClickHandler); The only way to achieve something near this in C++ is through function to pointers. A first attempt was done here in this article Meet Functors. The first attempt was about implementing generic delegates, but unfortunately it was over complex, and requires a lot of code to deal with. A second attempt was made, and it is about implementing Events and Delegates in C++ like those in C#. That is what this article is all about.

We are not going to explain the concept here. We will pose the code here, and any explanation required will be in the discussion.

The big goal is the line of code below, i.e approaching C# syntax as much as possible:

p_server->MessageSent += new ClientDelegate(this, &Client::MessageSentHandler);
driver.cpp

class Server;
class Client;

typedef Event<Server> ServerEvent;
typedef Delegate<Client, Server> ClientDelegate;

class Server
{
public:
    ServerEvent MessageSent;

    void SendMessage(char* p_msg)
    {
        MessageSent(this, p_msg);
    }
};

class Client
{
    static int s_id;
    int m_id;
    void MessageSentHandler(const Server* p_sender, void* p_parameter)
    {
        cout << "Client" << m_id << ": received: " << (char*)p_parameter << endl;
    }
public:
    Client(Server* p_server)
    {
        m_id = ++s_id;
        p_server->MessageSent += new ClientDelegate(this, &Client::MessageSentHandler);
    }
};
int Client::s_id = 0;

int main()
{
    Server server;
    Client client1(&server), client2(&server), client3(&server);

    server.SendMessage("Hello");

    return 0;
}

Console Output

Client1: received: Hello 
Client2: received: Hello

Client3: received: Hello

Press any key to continue . . .

Delegate.h

template<class TSender>
class BaseDelegate
{
public:
    virtual bool Equals( const BaseDelegate<TSender>* p_other) = 0;
    virtual void operator()( const TSender* p_sender, void* p_parameter) = 0;
    virtual void Call( const TSender* p_sender, void* p_parameter) = 0;
};

template<class TReciever, class TSender>
class Delegate : public BaseDelegate<TSender>
{
private:
    typedef void (TReciever::*PTF)(const TSender*, void* p_parameter);
    PTF         m_ptr2Func;
    TReciever*  m_ptr2Object;

public:
    Delegate(TReciever* p_ptr2Object, PTF p_ptr2Func)
    {
        m_ptr2Func      = p_ptr2Func;
        m_ptr2Object    = p_ptr2Object;
    }

    bool Equals(const BaseDelegate<TSender>* p_other)
    {
        const Delegate<TReciever, TSender>* other;

        other = static_cast<const Delegate<TReciever, TSender>*>(p_other);

        assert(other != NULL);
        assert(m_ptr2Object != NULL);

        return other->m_ptr2Object == m_ptr2Object && other->m_ptr2Func == m_ptr2Func;
    }

    virtual void operator()(const TSender* p_sender, void* p_parameter)
    {
        assert(p_sender != NULL);
        (m_ptr2Object->*m_ptr2Func)(p_sender, p_parameter);
    }

    virtual void Call(const TSender* p_sender, void* p_parameter)
    {
        assert(p_sender != NULL);
        (m_ptr2Object->*m_ptr2Func)(p_sender, p_parameter);
    }
};

Event.h

template<class TSender>
class Event
{
    list< BaseDelegate<TSender>* > m_observers;
    void Register( const BaseDelegate<TSender>* p_handler);
    void Unregister( const BaseDelegate<TSender>* p_handler);
public:
    void operator += ( const BaseDelegate<TSender>* p_handler);
    void operator -= ( const BaseDelegate<TSender>* p_handler);
    void operator () ( const TSender* p_sender, void* p_parameter);
    void Call( const TSender* p_sender, void* p_parameter);
    ~Event();
};

template<class TSender>
void Event<TSender>::operator += (const BaseDelegate<TSender>* p_handler)
{
    Register(p_handler);
}

template<class TSender>
void Event<TSender>::operator -= (const BaseDelegate<TSender>* p_handler)
{
    Unregister(p_handler);
}

template<class TSender>
void Event<TSender>::operator ()(const TSender* p_sender, void* p_parameter)
{
    Call(p_sender, p_parameter);
}

template<class TSender>
void Event<TSender>::Register(const BaseDelegate<TSender>* p_handler)
{
    assert(p_handler != NULL);

    for(list< BaseDelegate<TSender>* >::iterator itr = m_observers.begin();
       itr != m_observers.end();
       itr++)
    {
        if((*itr)->Equals(p_handler))
            return;
    }

    m_observers.push_back(const_cast<BaseDelegate<TSender>*>(p_handler));
}

template<class TSender>
void Event<TSender>::Unregister(const BaseDelegate<TSender>* p_handler)
{
    assert(p_handler != NULL);

    vector< BaseDelegate<TSender>* >::iterator where;
    for(list< BaseDelegate<TSender>* >::iterator itr = m_observers.begin();
        itr != m_observers.end();
        itr++)
    {
        if((*itr)->Equals(p_handler))
        {
            where = itr;
            break;
        }
    }

    m_observers.erase(where);
}


template<class TSender>
void Event<TSender>::Call(const TSender* p_sender, void* p_parameter)
{
    for(list< BaseDelegate<TSender>* >::iterator itr = m_observers.begin();
        itr != m_observers.end();
        itr++)
    {
        (*itr)->Call(p_sender, p_parameter);
    }
}

template<class TSender>
Event<TSender>::~Event()
{
    for(list< BaseDelegate<TSender>* >::iterator itr = m_observers.begin();
        itr != m_observers.end();
        itr++)
    {
        assert(*itr != NULL);
        delete (*itr);
    }
}

I leave you with the code, feel it and test it. If you detect any bug please don’t hesitate to fire it here. Any questions are very welcomed.

Unions, a powerful “C” construct

steve-urkel-geek

Once upon a Geek, there were some guy sitting on his Visual Studio writing a program using Winsock library (Windows Sockets API). That guy is so geek that he sneaked in MS Winsock header files and watched the definition of some used structures in the library. It was the moment he discovered how MS geeks utilize the use of their language and write the ultimate geek code especially when it comes to Windows API. Our poor guy didn’t imagine that unions are ….. arggg!! enough talk, lets see.

We will try to discovered the power of C Unions. Here is what Wiki says about unions, You should be geek enough to search about unions if you don’t know them.

We are going to demonstrate a very powerful usage of unions. Suppose we have a 32-bit number and we sometimes want to treat it on byte-basis (4 bytes) or on word-basis (2 words) or as Double Word (the whole 32-bit), then one may implement such thing using shifts (C right and left shit operator >> and <<), but we know that shift is an assembly instruction which cost cycles and also the shift implementation will worth some geek coding, however there is a very smart solution using Unions and here it is:

#include <iostream>
using namespace std;

struct Bit32
{
    union
    {
        struct
        {
            unsigned char B1, B2, B3, B4;
        }Byte;

        struct
        {
            unsigned short W1, W2;
        }Word;

        struct
        {
            unsigned int DW;
        }DWord;
    };
};


int main()
{
    //uninitialized 32bit [XX][XX][XX][XX]
    //each X represents 4bits
    Bit32 bin;

   
    //after assigning first byte: [XX][XX][XX][FF]
    bin.Byte.B1 = 0xFF;

    //after assigning second byte: [XX][XX][FF][FF]
    bin.Byte.B2 = 0xFF;

    //after assigning second word: [FF][FF][FF][FF]
    bin.Word.W2 = 0xFFFF;
   
    //this outputs 2^32
    //which is the equivalent to 0xFFFFFFFF
    cout << bin.DWord.DW;

    return 0;
}

Note that this code and implementation assumes a little-endian byte ordering (used in Intel processors) which means that the order of the each member in Byte and Word struct will be reversed if it is written to run on big-endian processor.

If you are curios about how this post relates to Winsock library you just need look at in_addr struct found in inaddr.h which is part of Winsock library.

Conclusion: Our poor geek is now sure that Winsock authors are the real geeks!.

Visual Studio user-defined keywords highlighting

  • What it is all about ?
    • Have you ever coded in assembly language ? for me the answer is “yes I did”, I used to write assembly programs using MASM through Visual studio 2008 which is a very rich IDE, unfortunately Visual Studio supports highlighting C/C++ , .NET languages, XML only, this mean that it doesn’t support assembly code highlighting which may be very important for a readable assembly code in order to distinguish between  an instruction and its operands (MOV EAX, 8).
    • There is no doubt that code highlighting makes your eyes more comfortable and debugging operation easier, but how to highlight keywords that Visual Studio cannot recognize ?  this suggests 2 ways to achieve so,  lets call them “Newbie” and“Visual Studio Geek” way.
  • The “Newbie” way:
    • This suggests using an open source text editor like “Notepad++” which supports code highlighting for a variant of languages including (C/C++, C#, Assembly, Java, PHP, Python).
    • One of “Notepad++” hottest features is that it allows for a user defined syntax highlighting, where you can define your own language and its highlighting, this sounds awesome !
    • Advantage:
    • Disadvantage:
      • Each time you try to compile your code you need to do the following time wasting operation:
        1. Press Ctrl+A, Ctrl+C,  which means copying your code from your text editor.
        2. Press Alt+Tab to navigate to visual studio, Pres Ctrl+V to paste your code in Visual Studio code area, then Compile.
      • The separation between the area where the code is being compiled and the area where the code get highlighted is not so far from a real programmer nightmare, which is the “Debugging monster” .. not scared yet ? watch this: 
        • you are compiling a mass of code, a “syntax error” appears, simply you go to the error pane and navigate to the error line through clicking on the error description.
        • Reaching the error line, you find a mass of black text, and you should scan it with your eyes up and down to figure out the error and fix it, which is somehow scary.
        • It gets worse when you need to debug a logical error.
      • Now we will strive to getting our anonymous code highlighted and compiled in the same place, and here shines a light, a“Geek light”.
  • The “Visual Studio Geek” way:
    • We will not be able to do exactly what “Notepad++” does, but we will try to get closer as much as we can.
    • For assembly language case:
    • Hey ! enough bullets, I don`t write assembly, this post is useless.
      • It is expected that one will say so, assembly case was just an example that gets us closer to the solution.
      • Lets not talk about assembly anymore, I will talk about a personal experience with Windows Programming through Win32 API which I hope will make everything clear.
  • Win32 API nightmare
    • The code below is no more than the a main function in Windowns Programming environment with a message box saying “Hello World!”.
    • int WINAPI WinMain(
                         HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR lpCmdLine,
                         int nShowCmd)
      {
          MessageBox(0, TEXT("Hello World!"), TEXT("Hello"), 0);
      
          return 0;
      }

    • WINAPI, HINSTANCE, LPSTR as well as the TEXT macro are all defined types in Windows Programming environment.
      • LPSTR is a pointer to a long string of characters, you needn’t know about Windows Programming right now, we just focus on the bigger concept right here.
    • The main problem here is that your eyes get tired of all this black text, that is hard to distinguish LPSTR from lpCmdLine, HINSTANCE from hInstance
    • Next we will try to highlight the most used API keywords, but before we continue, be sure that you succeeded to highlight assembly code using the provided Irvine link, or at least read the steps.
    • We will try to produce the code below using Visual Studio user-defined keywords highlighting.
      int WINAPI WinMain(
                         HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR lpCmdLine,
                         int nShowCmd)
      {
          MessageBox(0, TEXT("Hello World!"), TEXT("Hello"), 0);
      
          return 0;
      }
    • Important information about usertype.dat:
      • this is the file which Irvine told to put in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
      • Open this file using Notepad, Surprise ! the file is no more than assembly keywords, and here is a screen shot
      • image
      • For those who are not familiar with assembly “ah, al, ax, bh” are names of assembly registers used in the code.
      • This file should contain the user-defined keywords which Visual Studio should highlight.
      • Each keyword should be provided on a separate line, with no spaces inside the keyword.
    • Visual Studio user-defined keywords highlighting:
      • Assume that we want Visual Studio to highlight the following C++ class names when they appear in our code: cin, cout, string
      • Do the following:
        1. Open a Visual Studio C++ console application project and write the following:
          int main()
          {
               string str;
          
               getline(cin, str);
               cout << str;
          
               return 0;
          }

        2. Open “usertype.dat” using Notepad.
        3. Write cin, cout, string each on a seperate line.
        4. Press Ctrl+S to save changes to the text file and close it.
        5. Open Visual Studio, then from the main menu choose Tools –> Options –> Fonts and Colors.
        6. From the “Display items” list, choose “User Keywords”, now you should see the following.
        7. image
        8. Change the color of user keyword from “Item foreground” list box, and then press OK.
        9. If you are using a custom color with Red = 43, Green = 145, Blue = 175 then your code should now looks like the following:
          int main()
          {
              string str;
          
              getline(cin, str);
              cout << str;
          
              return 0;
          }

      • The same can be done with Windows Programming environment, you can define  keywords and types such as HINSTANCE, WINAPI or LPSTR.
    • Advantage:
      • We can now see our keywords highlighted and complied at the same place.
      • You can highlight the keywords of a library you use (e.i Windows programming, STL).
    • Disadvantage:
      • It is easy to notice that Visual Studio doesn’t provide a powerful custom highlighting capabilities compared to those provided by “Notepad++”.
      • User defined keywords are to be added manually, and this is very boring for large libraries to include all their types.
  • Conclusion: There is always a Tradeoff:
    • There is always a decision to make, most of the time we find that not all choices are equal, and this is life.
    • Here we have to choose between 2 things:
      • Localizing code highlighting and compiling inside Visual Studio.
      • Code highlighting and compiling are separated, you compile in Visual Studio and watch your code in “Notepad++”, but you get more powerful highlighting capabilities.
    • Don’t let others decide for you,  simply because in each situation you know what is the most proper choice, and this what is called decision making.

Effecient form background

I was working on a C# WinFroms project, in which on of its forms has a lot of movable controls and a background image, the problem was that “Each time I try to move the controls in run time, the form background flickers and some controls disappear and then appear after I finish moving the control”.

At first glance I though that my implementation for the control movements is not efficient, I started to tweak my implementation and remove useless routines, but unfortunately the form still flickers, I tried to double buffer the form but it failed to improve performance.

After a lot of Googling and MSDNing, I found 2 blogs talking about the same issue,  after studying what they say I found that the problem was that the form spends a lot of time drawing its controls plus drawing the background image.

I didn’t know that drawing a background image on a form is an issue of performance, but now I have to say that it is, lets analyze what happen to draw an image in a very abstract way.

  • Suppose we want to put a JPEG image of size 1024 * 768 to be the background of a Form of size 800 * 600, each time the form has to draw itself it will do the following:
    1. Uncompress the JPEG image to a raw Bitmap image, as we know the JPEG is a compressed Bitmap.
    2. The resulted Bitmap image will be scaled to fit the 800 * 600 form.

Those 2 steps are what cause the form to flicker, each time the from has to redraw itself it needs to draw its controls and, uncompress the JPEG image then scaling it, so the key to improve the performance is to get rid of these redundant steps.

Following the “Render once, show a lot” principle – this is a principle from my space – we want to do those steps only once when the form is created, but how ?

The key is to provide our background image in a proper format, why not providing our image in a 800*600 Bitmap instead of 1024*768 JPEG ? so to achieve this there is 2 ways, "By logic" and the "C# geek" way, lets examine each and know the advantage and disadvantage of each.

  • "By logic" way:
    • This is no more than editing the desired background image in any photo editor (Photoshop, MS Paint) and rescale your image to 800*600 and save it as bitmap image, a peace of cake.
    • Advantage:
      • Easy, straight forward, doesn’t require any programming skills.
    • Disadvantage:
      • Each time you need to change the background image you should have to open your photo editor scale and resave the image as bitmap, this is a time wasting routine process, for me a “True programmer” is the one who utilize his programming skills to develop a solution for life.
  • "C# geek" way:
    • Why geek solution?
    • Knowing the "By logic" way we are a 1 step far from developing our elegant solution, we can make use of C# image processing capabilities (scaling, compressing and uncompressing images), so we let C# do the job for us.
    • Each form has a property named BackgroundImage of type Image, it is used to set or get the background of the form.
    • Our geek solution will be as follows:
      • We should first derive a new Form from C# WinForms Form class
      • We override the property BackgroundImage so we implement the uncompressing and scaling stuff there.
      • Look at the code below, I think the code is self-explaining.
        using System.Windows.Forms;
        using System.Drawing;
        
        class TweakedForm : Form
        {
            /// <summary>
            /// Gets or sets the background of the form
            /// </summary>
            public override Image BackgroundImage
            {
                get
                {
                    return base.BackgroundImage;
                }
                set
                {
                    if (value != null)
                    {
                        //Create a new bitmap image has same same size of the form
                        Bitmap m_bmp = new Bitmap(
                            this.Width,
                            this.Height,
                            System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        
                        //Make a graphics instance that can draw on our bitmap
                        Graphics g = Graphics.FromImage(m_bmp);
        
                        //Here is the magic
                        //We want to render our compressed image to a raw bitmap image
                        //This line is like uncompressing our image and scaling it
                        g.DrawImage(value, 0, 0, m_bmp.Width, m_bmp.Height);
        
                        //Don`t forget to release your resources
                        g.Dispose();
        
                        //Assign our bitmap to the base form
                        base.BackgroundImage = m_bmp;
                    }
                    else
                    {
                        base.BackgroundImage = null;
                    }
                }
            }
        }
    • Advantage:
      • If you intend to modify the form background in future, you just need to provide the background path.
      • Generic, can be applied on any image supported by C#.
    • Disadvantage:
      • Require some geek C# knowledge, which is not that bad.

Conclusion: Always try to stick to geeks solutions, they are always elegant, this my answer to the "Why geek solution?".