Wednesday, December 12, 2007

Reducing Heap Fragmentation with Low Fragment Heap Featuer


Today most pc have enough memory . But there is still chance to occur fragmentation by continuously allocating and freeing small chunk of memory.

Windows Xp , Windows Server 2003 has a new feature called Low fragment heap(LFH).
If you enable this when creating heap , this can reduce heap fragmentation.The LFH has a predefines set of memory chunks of different sizes. it called this chunk as buckets , so when we request for a a few bytes , the LFH returns the bucket of smallest size.  

The LFH option can be enable by the API HeapSetInformation. For example it may look like
long HeapFragValue = 2;

HeapSetInformation( GetProcessHeap(),HeapCompatibilityInformation,HeapFragValue,sizeof(HeapFragValue) );

2 indicates the information is about LFH.
This feature is very useful. Otherwise you may need to write your own allocators and handle the memory pool.
When writing allocator we need to think about the page size , number of page faults etc. and certainly it takes some time.   So if you are in hurry try the LFH option. :)

Thursday, November 15, 2007

How to make application Faster C++/C

Following small optimaization can make applications more faster . I think.


1.Make small functions  as inline functions.


2. If you are  using variable to hold constant data , make in const.
like int i = MAX_VAL; can be changed to const int i = MAX_VAL


3.Creating simple expressions
some exmaples like belowif(i == MAX_VAL) return 1;
else return 0 ; 
can be replaced with return ( i==MAX_VAL); 

another one if( i ==0 ) return nVal; else returrn 0; can be replaced with  return (i==0) ?nVal : 10   Ternary operator is fast than if else.
   
4.When function calling don't pass long arrays . Use pointers or reference.

5.For copying array use api , like CopyMEmory , memcpy.
(They uses asm codes to get the tas done)


6. similarly for initializing memset also can be used .


7. When increamenting class objects uses ++obj , rather than obj++. 
(because obj++ might create  temporary objects).


8. Too much object oriented approch will also affect speed.


9. when one function called ,  It is wise to return first if any argument is invalid , rather than do it later.
like
fun()
{
   if(param == 0 || param1 = 0 || n!=V) return 0;
   return 1;
}


10. Allocating memory several times wil cause fragmentation, so allocate a buffer pool first.


11. Similarly try to avoid creating mutiple files in Hard disk .It will also cause fragmentation.
      Try to do the work with one file, otherwise think about databases.

    
12.  Use int , as loop couner etc. Because int size same as the processor register size, so it is fast than others.


13.  Exception catching is expensive , it will cause application slower , and will increase the size of  EXE.


Do if you are not using exception handleres , you can turn it off in compiler settings
Also avoid putting exception handling inside repeatedlycalling functions .
Put exception handling on in the main parts of program.


 14. you can use __fastcall to make functions calls faster , it wil try to put arguments in registers._cdecl will increase the size of EXE.


 15. Don't load all DLLs on application loading . Most of the cases applications slowing down due to these reasons.
 DLL as name implies is for dynamic loading.


16. Most important , Before copying code from somwhere Understand it :)

Monday, October 29, 2007

C,C++ Geek's way to access array/ pointer elements.


After a short gap , i am back. This is post is from old memories. I don't know how many people know this ?
I don't want to explain much. Look the following codes

int nArCounts[3]   = {30,20,50};

Normally people accessing array element like nArCounts[i].

It is also possible to access it like this i[nArcounts] ,Both are converted to nArcounts + i .
Thats all , But if a guy don't know this , when he/she sees these type of notations he will get mad. I am sure.