Pay attention to memory improvement software performance (3)

zhaozj2021-02-16  45

II. How to improve the efficiency of the use of memory

I have seen two of the following:

1. Design of the 2-level configurator in STL. I have implemented a simple vector, efficiency and STL Vector in accordance with the thinking of STL. The difference is far;). STL in memory allocation (alloc) The block has great effort (of course, not just memory usage). It is great to improve efficiency. Oh, I want to see more details, I will go to the teacher's << STL source analysis >> Chapter 2

2. SLAB algorithm, (The following is some of the information I collected)

3. The following is a description of the SLAB algorithm:

4. Assign objects: IF (free object position in the cache area corresponding to the object) {

5. Get this object without initialization}

6. Else {

7. Assign memory

8. Initialization object}

9. Release the object: Simply use the corresponding position to be idle without in the cache, without having to analyze.

10. If the system resources are insufficient: some unused object spaces are obtained from the cache, they analyze these objects, and release the space occupied by these objects to the system.

The following is a schematic diagram of the data structure of SLAB

11.

Cache_cache

Cache1 SLAB1 SLAB2 SLAB3 SLAB4

Cache2

C_firstp c_freep c_lastp

Cache3

Cache4

Cache5 s_freep

It is very simple to understand the code of the SLAB algorithm or the use of your own program. As long as you add 2 macro. If you don't want to download Myicq code, you can go here.

http://yj000885.sosoo.net/3ddoc/testslab.htm

It is to be explained. In general, it is not necessary to define the management method of memory. Generally, a large project, which is relatively high in performance (such as game engine). Generally, the way you define your own definition.

There is also an introduction to << Windows Advanced Programming >>. I think it can also improve performance to some extent.

1. This is a platform-related approach. Suitable for Win32 platforms.

We know, when calling new, the object is allocated on the heap. If there is a heap of each object, then create an object, destroy an object, then you need to assign a heap again, then It is more destroyed. The overhead is relatively large.

You can use the following ideas to reduce overhead. Create a heap for each class, all instances of this class are allocated on that heap. This can reduce overhead. Look at the following code:

Class testclass

{

Private:

Static hheap s_heap;

STATIC uint s_unumallosinheap;

PUBLIC:

// Reserve Operator New, Delete

Void * Operator New (size_t size)

{

IF (S_HHEAP == NULL)

{

_Try {

S_HHEAP = HeapCreate (/ * Stack of initial size * /, 0, 0);

}

_EXCEPT (Exception_Handler)

{

Return NULL;

}

............ .....

Void * p = (void *) HeapAlloc (S_HHEAP, 0, size); / / olive the processing exception code

IF (p) s_unumallocsinheap ;

Return P;

}

Void Operator Delete (Void * P)

{

IF (HEAPFREE (S_HHEAP, 0, P)

S_unumallocsinheap -;

IF (0 == s_unumallocsinheap) destroyed

S_HHEAP = NULL;

}

}

HHEAP TESTCLASS :: S_HHEAP = NULL;

UINT TESTCLASS :: S_unumallocsinheap = 0;

You can save overhead when there are multiple objects of multiple objects.

I have written so much. I am a bit sleepy. There are still many problems in my code. Please ask everyone to advice. Thank you!

转载请注明原文地址:https://www.9cbs.com/read-25179.html

New Post(0)