Solved

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

Posted on 2003-10-21
9
1,330 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:muskad202
  • 4
  • 3
  • 2
9 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 20 total points
ID: 9590499
Change constructor definition to:

FF(LPCSTR  FN,LPCSTR ,LPCSTR  E,LPCSTR  S)

This should improve performance significantly. Don't case about allocation spped in CString - it is quite good and you have no much chances to improve it. FreeExtra calls are also unnecessary.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9590530
Correction:
Don't care about allocation speed in ...

Typo, as usual. Sorry.
0
 
LVL 2

Author Comment

by:muskad202
ID: 9590556
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
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Expert Comment

by:stsanz
ID: 9590717
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();
    }
};



0
 
LVL 6

Accepted Solution

by:
stsanz earned 30 total points
ID: 9590751
Oops! correction:

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:
  LPSTR FileName ;
  LPSTR FilePath ;
  LPSTR Ext ;
  LPSTR Size ;

    FF(char* FN, char* FP, char* E,LPCSTR S)
    {
       FileName = strdup(FN) ;
       FilePath = strdup(FP) ;
       Ext = strdup(E) ;
       Size = strdup(S) ;
    }
};

This way you create only one object: the FF object, and not multiple CString objects.

0
 
LVL 6

Expert Comment

by:stsanz
ID: 9590785
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.
0
 
LVL 2

Author Comment

by:muskad202
ID: 9591324
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
0
 
LVL 2

Author Comment

by:muskad202
ID: 9591416
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 ??
0
 
LVL 6

Expert Comment

by:stsanz
ID: 9591752
>>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.

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question