Windows Program startup performance optimization (loaded EXE, loaded DLL, only limited code loaded into memory, the CPU IP point to the entry point of the program)

Source: Internet
Author: User

First, re-positioning
Link-time Relocation:
The target file is typically made up of multiple sections, and the compiler typically generates code from 0 addresses when compiling each target file. When multiple code sections are composited into a piece of code, adjustments need to be made based on their position in the final code snippet. At the same time, the linker needs to assign a run-time address to the parsed symbol. This process is re-positioning.

Load-time Relocation:
DLL may be called in the program, because EXE is the first to be loaded, so it is generally able to load to its desired memory address, and DLL loading generally after the EXE, you need to re-locate the address in the DLL.

Second, the procedure of the start-up process
The WIN32 program startup process includes:

(1) The operating system to load the program into memory, and establish a corresponding operating environment
(2) initialization process of the application itself

Note: The application must conform to the PE format, consisting mainly of: data (segment),. Text (code snippet). After the data and code are loaded into memory (in memory, the application's code behaves as a series of ordered sets of instructions), the CPU extracts each instruction in sequence from the entrance of the program and executes it.

Loader: The task of the operating system loader is to read the physical file of the executable program in the disk into memory and convert it into a representation of the program's in-store.

Third, compile the link and start
Compile Link:

(1) Precompiled expansion macro
(2) A. obj file is generated for each. cxx file, and the destination file contains at least data segments and code snippets; The destination file also contains a symbol table that records the symbols that you reference, as well as the symbols that are provided to external references.
(3) The compiler synthesizes these target files into a library file (. lib), while parsing the symbol references that can be found
(4) The linker links the target library file with all static and dynamic libraries that need to be referenced to generate the final executable file. (First, you need to synthesize the dependent static library into the executable file, and secondly to ensure that the symbol of the dependent dynamic library file exists) the symbol table of the executable file only needs to record the import symbol table.


Start:
Programs that do not rely on DLLs:

(1) The operating system creates the process and allocates private process space;
(2) The loader maps the data segment and code snippet of the executable file to the process virtual memory;
(3) prefetch a limited code snippet into the actual memory, the CPU IP point to the program entry point, you can start execution


DLL-dependent programs:

(1) Ibid, create process, map virtual memory;
(2) The loader reads the import symbol table of the executable file, thus finding the dependent DLL
(3) The loader calls loadlibrary,loadlibrary for each DLL that relies on it to handle the following:
A, the loader determines a suitable base address (relocation) b for the DLL
, reads the import and export symbol tables, compares the import symbols of the application with the exported symbols of the DLL to match
C, and through the DLL import symbol table determines the DLL Other DLLs that are dependent, also load up
D, call the DLL's initialization function
(4) Initializing the application's global variables
(5) Enter the application entry point function to start execution


Iv. factors that affect the start-up performance:
(1) The performance of the program cold start mostly depends on the time consumed by IO operation;
(2) DLL export too many symbols, will cause the loader to consume too much CPU events and IO to handle this symbol table;
(3) disk fragmentation problem: In an ideal state, sequential reads of a file do not require a head seek operation. However, the operating system's file system, in blocks to manage the physical disk space, when the disk after continuous deletion and modification operations, may no longer have continuous storage space. Causes a logically contiguous file to be composed of a number of discontinuous fragments on the disk, resulting in excessive seek time to execute the IO.

Five, optimize the starting process method:
(1) Reduce the number of dynamic libraries;
(2) Reduce the size of the dynamic library: can be compiled to optimize the options, clear redundant code;
(3) Optimize the code layout of the executable file and library file: The function of the library file is more tightly arranged, so as to reduce the IO;
Step: Get the order file for the function call (. PRF); If you pass these PRF to the linker, the linker will automatically reorder the files in the dynamic library according to the PRF file (try it out)
(4) Lazy initialization: You can postpone some initial initialization work that is not needed at startup to enhance the start-up experience, and delay the time: it can be processed when the program is idle and processed when the message loop is idle.
(5) Multithreading start: With the following characteristics, suitable for multithreading start
A large number of dynamic libraries need to be loaded at startup, triggering a large number of IO operations, and these dynamic library initialization functions require intensive operations that consume CPU time. The IO wait time and CPU run time can be interleaved to shorten the startup time.

Note: IO is implemented by the CPU after the command is issued by the motherboard DMP complete, triggered after completion of the interrupt, and then the CPU continues to process, so IO consumes little CPU time.

70676563

Windows Program startup performance optimization (loaded EXE, loaded DLL, only limited code loaded into memory, the CPU IP point to the entry point of the program)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.