Link to home
Start Free TrialLog in
Avatar of muskad202
muskad202

asked on

(Windows) improving malloc performance using HeapAlloc, VirtualAlloc..etc

Hi !!
my code :
----------------------------------
//class definition
class FF
{
public:
      CString FileName,FilePath,Ext,Size;
      FF(CString FN,CString FP,CString E,LPCSTR S)
      {
            FileName=FN;
            FileName.FreeExtra();
            FilePath=FP;
            FilePath.FreeExtra();
            Ext=E;
            Ext.FreeExtra();
            Size=S;
            Size.FreeExtra();
      }
};
---------------------------------------
i have the foll. code that creates objects of class FF (the code is called from a number of threads)
---------------------------------------
FF* X = new FF(FileName,FilePath,Ext,Size);
SomeCollection->Add(X);
-----------------------------------
the above code might be called nearly 200,000 times (from all the threads put together).

my question :
i came across an article on MSDN (http://msdn.microsoft.com/library/techart/optcode.htm). It was mentioned tht the performance of malloc can be increased using VirtualAlloc in some cases, or HeapCreate and HeapAlloc in some other cases ... i didn't understand in which cases exactly .. I was hoping someone could explain it .. or show me some way to optimize the above code..

thanks
muskad202
SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

Correction:
Don't care about allocation speed in ...

Typo, as usual. Sorry.
Avatar of muskad202

ASKER

the first 3 parameters to the constructor are CString coz, in the code where the objects of the class are created, the parameters passed to the constructor are CString .., i.e, in
--------------------
FF* X = new FF(FileName,FilePath,Ext,Size);
SomeCollection->Add(X);
-----------------
FileName,FilePath,Ext are CStrings..
so, was thinking, if the constructor definition expects LPCSTR, then the parameters have to be converted to LPCSTR from CString, and then again from LPCSTR to CString when they get stored (FileName=FN; FilePath=FP;  Ext=E;Size=S;)

....
muskad202
Write the constructor like this:
FF(CString FN,CString FP,CString E,LPCSTR S)
  : FileName(FN), FilePath(FP), Ext(E), Size(S)
    {
    }

This will prevent the program to create first an empty CString for each member, and then affect them.
This way, the CString creation is made only once


Now if you really want to improve performance, don't use CStrings at all.
Use char buffers instead, as AlexFM said.

class FF
{
public:
    char* FileName ;
FilePath,Ext,Size;
    FF(CString FN,CString FP,CString E,LPCSTR S)
    {
         FileName=FN;
         FileName.FreeExtra();
         FilePath=FP;
         FilePath.FreeExtra();
         Ext=E;
         Ext.FreeExtra();
         Size=S;
         Size.FreeExtra();
    }
};



ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now, the Microsoft article about VirtualAlloc, HeapCreate and HeapAlloc says that when you need to allocate multiple small blocks of memory, it's faster to allocate one big block once, and then share the memory between all the small blocks.

In your case, this is not so easy, since the size of the strings is not predictible.
the strdup function .. it allocates memory also ?? if yes, how much does it allocate?? just enough for the string..hopefully, coz i can't afford extra memory for each object..

also, i was using the FF class mainly becoz i needed the constructor to be called which in turn would call the CString construtors .. if i now decide to use only LPCSTRs instead, can i improve the code by using a struct FF incase of the class FF?

thanks
muskad202
and now, if i do use a struct, with 4 LPCSTRs, then the size of each struct becomes fixed (4 * sizeof(char*)). in this case, would i benefit by using VirtualAlloc/HeapAlloc/HeapCreate ??
>>how much does it allocate?? just enough for the string?
Yes. It allocates strlen(str)+1 for terminal \0.
(by the way it will be slightly less than a CString object).

>>can i improve the code by using a struct FF incase of the class FF?
No. There is no performance tradeoff between struct and class.

>>the size of each struct becomes fixed (4 * sizeof(char*)). in this case, would i benefit by using VirtualAlloc/HeapAlloc/HeapCreate ??
No, because you also need to allocate memory for the strings (strdup job). This memory size is not fixed.