C++/VB - dll loading time performance

Asked By Frank-O
16-Feb-08 04:48 AM
Hi all,

I got a problem, I want to speed up the startup of my huge application
(100 dlls)
the loading of one particular dll (gred.dll) takes about 10sec. one my
of my colleague suggested to load that dll in a background thread at
startup.
Here's my action i have created a class CTaskThreads

// in the constructor
this->m_Thread = AfxBeginThread(WorkerThreadLauncher, this);

UINT CTaskThreads::WorkerThreadLauncher(LPVOID pvParam)
{
CTaskThreads *pThreadInstance =
reinterpret_cast<CTaskThreads *>(pvParam);
return pThreadInstance->Execute();
}

UINT CTaskThreads::Execute()
{

DWORD start = ::GetTickCount();

do
{
::Sleep( 1000 );
HINSTANCE hInstLib = ::LoadLibrary(_T("GRED.DLL"));
if ( hInstLib == NULL)
{
ASSERT(FALSE);
}

} while ( this->m_StopThread == false ) ;

DWORD end = ::GetTickCount();

this->ExecutionTime = end - start;

return 0;
}

Then in my executable module, initinstance() i have declare  a
CTaskThreads object.
unfortunately in some dll it failed to load some xml file ressources.

Since I am a beginner in this area i have a few questions regarding
this issue :

Why loading a dll takes time ? it depends on what ?
Is it a good idea to load a library the way i did ?
Is there a better solution ?
why it is imposible to load some ressources in some linked dll ?

thk u
Franck-o
WorkerThreadLauncher
(1)
AfxBeginThread
(1)
GetTickCount
(1)
CTaskThreads
(1)
Database
(1)
PThreadInstance
(1)
ExecutionTime
(1)
CreateWindow
(1)
  Alf P. Steinbach replied...
14-Feb-08 04:34 AM
* Frank-O:

Reduce the number of DLLs drastically.

Consider splitting the single monlithic application into several programs.

For the rest, consider delay-loaded DLLs (directly supported by Visual C++).



You could check the dependencies, but since you're going to redesign
this thing anyway (aren't you?) there's not really much point in that.

Ditto for basing the DLLs (assigning preferred load addresses).



Uh.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Frank-O replied...
16-Feb-08 04:48 AM
).

Thx for your answer,
It seems ideal to redesign everything, but this is a very old
application i'll be too costly...for my boss

What's wrong with multithreading for that purpose ?

Frank-o
  Norbert Unterberg replied...
15-Feb-08 07:08 AM
Frank-O schrieb:


Before considerung solutions, you should analyse the problem.
The question is: Why does the DLL load so slow? Why do you think that multiple
threads would make loading faster?

When loading of a DLL takes a long time, it can be due to many reasons:
* The DLL is really huge, so the time is spent reading the hard disk.
* The DLL has a very complex DllMain that takes a long time to execute
* The DLL has many dependencies that need to be loaded as well.
* Windows needs to rebase the DLL
* The DllMain allocates a huge amount of memory so windows needs to re-arrange
its memory layout (trashing, paging)

There might be more reasons. But as long as the reason is not the busy CPU than
you won't gain much performance effect when loading the DLL in a separate
thread. All threads share the same hard disk, memory manager, process etc. And I
guess the main application still needs to wait until all DLLs have been loaded
before it can proceed.

What you actually can do is move the complete DLL loading funtion into a
separate thread. At least you can have the GUI responsive and the user gets the
feeling the application loads fast. You would still need to disable all commands
that rely on the not-yet-loaded DLLs.

Norbert
  natha replied...
15-Feb-08 12:58 PM
In article <fae29983-14e4-48b9-aa92-30fcade87a6e@d4g2000prg.googlegroups.com>,



What's wrong? I'd say that it's wrong to blindly jump into a
solution without analyzing the problem first. Have you done any
benchmarks to see where the problem lies? Is the HD busy thrashing?
(Tip: make a ramdisk and load your app from that; I bet your boss's HD
wasn't been defragged in years.) Multithreading won't do a thing to
make your HD faster. If you're busy firing off HD access from multiple
threads, that can make things *slower* as your HD's read head
ping-pongs around. Sustained large sequential reads are better for
your HD than a lot of little ones all over the place. Alternatively,
is it the CPU that's busy? Multithreading may make the problem worse,
as that can pollute out your L2 cache.

time. However, debugging the last 10% can take a REALLY long time,
especially if this is a "very old application" that almost certainly
wasn't designed with multithreading in mind.

I really encourage you to try a profiler -- AMD's CodeAnalyst
is a free download and doesn't require an AMD CPU in the system:
http://developer.amd.com/tools/codeanalystwindows/Pages/default.aspx
Figure out the root of the problem. ***THEN*** implement a solution.

Nathan Mates
--
  Tim Roberts replied...
16-Feb-08 08:26 PM
I'll give you one example.  What if that DLL is creating GUI elements, like
windows?  Windows receive messages on the queue of the thread that created
them.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
  Frank-O replied...
17-Feb-08 10:20 AM
I have used Aqtime to analyse the application :
This particular DLL 's (Gred.dll ) size is 6MB and it also uses 15
medium-sized (3MB) static libraries.
Most of the time is spent loaded the dependents DLL , read records
from the huge data base, and drawing/creating the graphic user
interface components.


Yes the main application needs to wait until all DLLs have been
loaded, and disable all the commands that rely on it is simply not an
option since I am not even aware of the number of Commands that need
Gred.dll to be loaded dynamically.


My Ideas :
The DLL will not be loaded at startup instead it will be loaded the
first time a function is actually call.
I also started to optimize the code for reducing the load on the data
base...
since this a parameterized application, I will propose to enable the
user to select options to disable some tasks perform at start-up.

Concerning multithreaded issues :
-In a single processor architecture there will be no real gain in
time.
-It seeems that the DLLs are not thread safe

Could you explain your statement ?

Thx all
  Ben Voigt [C++ MVP] replied...
18-Feb-08 09:29 AM
Only if the process is performing independent I/O (i.e. over a network)
would a separate thread help.

However if the database access or dependent DLLs are loaded over a network,
then multithreading could give huge benefits by allowing multiple requests
to be sent at the same time.
  Tim Roberts replied...
18-Feb-08 11:12 PM
When you call CreateWindow, that window is "owned" by the thread that
created it, and messages for that window will be sent into the message
queue for that thread.  The normal message loop, which runs in the main
thread, will never see them.  In fact, if that second thread doesn't start
a message loop, the messages will never be processed.

That's why you read so many articles warning about combining GUI operations
with multithreading.

Now, if you know the DLL isn't going to create any windows, then it would
be fine to call it from a second thread.  I just presented one possible
counterexample.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
  Ben Voigt [C++ MVP] replied...
19-Feb-08 10:08 AM
Of course, since you are starting the second thread yourself and managing
the DLL load, running a message dispatch loop there is entirely possible.
help
AfxBeginThread failure - how to find cause? C++ / VB In an MFC pgm under VS2005sp1, I am creating a worker thread using AfxBeginThread. Under certain circumstances which I can't quite pin down, AfxBeginThread returns NULL, indicating it has failed. Is there any way I can find out the code for which I don't have the source. My specific code is CWinThread* pThread = AfxBeginThread(MyControlFunction, &ti, 0 / *priority* / , 230000000 / *stack size* / ); and a test for pThread = = NULL immediately following and use of the file open common dialog in some preceding code. VC MFC Discussions AfxBeginThread (1) McPhillips (1) MVP (1) GetLastError (1) CreateThread (1) MyControlFunction (1) VC (1) MFC (1 the code works. That's why I want some error code or explanation of the AfxBeginThread return. Further, there is no reason why a large stack size should fail; if I See previous reply; it does not matter. Did you try GetLastError after the call to AfxBeginThread. Honestly I have never had AfxBeginThread fail on me. AliR. I have never had it fail either. OP has said that
AfxBeginthread exception C++ / VB Hi Folks, In my application I have two threads one to read another one to send the data to the socket . I have created the threads using AfxBeginThread For eg. AfxBeginThread(&MyClass::ReadThread, &mStatusSocket, / / CEdit Ctrl THREAD_PRIORITY_NORMAL, 0, 0, NULL ); Everything works fine when I run some light on this . Thanks in advance JLD VC MFC Discussions CWinThread (1) CEditCtrl (1) AfxBeginthread (1) MStatusSocket (1) CrashPickup (1) PostMessage (1) ReadThread (1) MyClass (1) As I tried to www.flounder.com / mvp_tips.htm Given this additional info and your previous posting of your AfxBeginThread() call, please read: http: / / members.cox.net / doug_web / threads.htm Your problem suggests heap corruption modules, thread, call stack for every thread. Works for both debug and relese builds. keywords: AfxBeginthread, exception description: Hi Folks, In my application I have two threads one to read the
AfxBeginThread hangs, but not when the application is being debugged C++ / VB I have a very weird problem. . . In a dialog based app, when a button is clicked, I call AfxBeginThread() to create a new worker therad (not a UI one). The thread is created suspended and after that started. The problem is that AfxBeginThread hangs every single time (both debug and release). It never returns. To make this even though no breakpoints are set. I mention that I am absolutely sure that it is AfxBeginThread that hangs, I put some calls to MessageBox() and / or output to log files before but during an MFC event handler (dialog button being clicked). Thanks, Bogdan. VC MFC Discussions AfxBeginThread (1) DLLs (1) DllMain (1) Newcomer (1) Breakpoints (1) Mechanisms (1) Bfarcasanu (1) Presence (1 something wierd ? ) ismo * ** * This is suggestive of some fundamental design problem. First, when you say AfxBeginThread What is the thread doing? What shared state is involved? What synchronization mechanisms are involved and one of these events goes into an infinite loop, you will never return from AfxBeginThread. So what DLLs are you using (note that in general, handling DLL_THREAD_ATTACH events is a com Web: http: / / www.flounder.com MVP Tips: http: / / www.flounder.com / mvp_tips.htm keywords: AfxBeginThread, hangs, , but, not, when, the, application, is, being, debugged description: I have a very weird