Previously, we introduced allocator of c ++. Since the c ++ standard library already has a standard implementation, why should we implement allocator on our own? For performance considerations, many programs implement their own allocator to avoid using the standard library allocator. The standard library allocator will affect program performance in those aspects. The following is a one-to-one analysis:
1. Standard allocator involves system call, resulting in user-kernel-user Switching in the program. An additional cpu cycle is required for mode switching. Frequently use the standard allocator to allocate memory (for example, in a while loop), the cpu cycle consumed by mode switching is superimposed. Custom allocator uses a pre-allocated memory pool to avoid system call and improve memory allocation and recovery efficiency.
2. Standard allocator initiates system call. In addition to causing mode switching, it will also lead to another more serious problem: thread status switching-switching from execution to waiting (blocking ). First, system call is an entry point for the operating system to control thread status switching. As long as the thread initiates the system call, it tells the operating system that status switching can be performed, it depends on the scheduling policy of the operating system. Second, if the system call initiates a long operation, the thread will inevitably switch to the blocking state. Do not forget that memory allocation may be a long operation. For example, if the current physical memory is insufficient and you need to switch pages (causing disk IO ). When the code executed by a piece of key code (in the critical section) or a piece of interface thread is switched to a blocking state due to standard allocator, the consequences can be imagined.
3. Standard allocator may cause memory fragmentation. After fragmentation, some chain reactions may occur in extreme cases: first, frequent page changes may affect the memory allocation efficiency (blocking may occur, as shown at). Second, when the fragmentation continues to deteriorate, the swap-out memory may include the working set of the program in addition to the heap memory. When the working set of the program is switched out, a jmp of the thread may be switched to the blocking state (the target address of the jmp has been switched out), and the program execution efficiency is greatly reduced.