Solved

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

Posted on 2003-10-21
9
1,317 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now