Solved

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

Posted on 2003-10-21
9
1,351 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
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!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

717 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