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,S ize);
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
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,S
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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,S ize);
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
--------------------
FF* X = new FF(FileName,FilePath,Ext,S
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();
}
};
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
In your case, this is not so easy, since the size of the strings is not predictible.
ASKER
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
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
ASKER
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/Hea pCreate ??
>>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/Hea pCreate ??
No, because you also need to allocate memory for the strings (strdup job). This memory size is not fixed.
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/Hea
No, because you also need to allocate memory for the strings (strdup job). This memory size is not fixed.
Don't care about allocation speed in ...
Typo, as usual. Sorry.