Solved

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

Posted on 2003-10-21
9
1,342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

732 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