Application of Static linked list in Optimization

Source: Internet
Author: User

The basic structure of the static linked list is everywhere on the Internet, and the university textbooks are also very detailed. If you are not familiar with it, please refer to the courseware below to capture the picture. Thank you, Lou. (To be honest, there are so many university courses that only you have heard of carefully :-))

The Data Structure Course Design helped students solve several static linked lists. I did not expect that I have used this item for several optimizations over the past two years after graduation, as shown below:

1. Des key Cache

Financial Transaction Security Project stores a common key-value structure, key is the key name, and value is the des key, all of which are short strings. The original solution is to create an array of shared memory, sorting by key, half-lookup is still in the past, but insertion and deletion are O (N), so it is a tragedy during business peaks ......

Some may wonder, "why do I need shared memory? Use an stl map/unordered_map directly. Why not use the existing memcached/redis ...... Solution ......", I remember I had an email with Mr. Chen Hao of coolshell. He also had such questions. There are several reasons:

    • Is a complete pure C Project.
    • For some historical reasons, the original sys V shared memory method must be used; on the other hand, the original shared memory is also used to preventProgramBusiness data will not be lost after the device fails.
    • People who have worked in banking and telecommunications may know that banks cannot trust memcached/redis ......, They are more concerned with minicomputers/AIX/DB2/oracle ...... Even MySQL does not believe it, so it is not possible to use popular Internet solutions.

 

Therefore, it can only be optimized in the original scheme, and the idea is to store the key data separately from the index of the query mechanism used; probably as follows:

1. Open two shared memory segments A and B. A is used to store index information, and B is used to store actual key data.

2. A is a normal hash, and the linear re-Hash method stores the key corresponding to the key name in the static linked list of B.Tag Value.

3. Traditional linked list structures cannot be used in the shared memory, so two-way static linked list is used in B to store key data.

 

In a, the hash table stores the data in the static linked list of B as the key to achieve O (1) access. If key data and index data are not stored separately, you can directly use the storage key without B's shared memory.

 

This is also the first time I used static linked lists in applications. The textbook tells us that static linked lists are generally used in languages without pointers, such as basic/FORTRAN. They are the same as common linked lists. inserting and deleting do not require moving elements and cannot be stored randomly ...... What? Cannot be randomly accessed? If we directly use the subscript of the data in the static linked list, can we directly access it?

 

2. communication layer and application layer ing

Some students generate a readable client_id value after accepting the connection at the communication layer as the interaction ing between the communication layer and the application layer. The hash ing method is implemented by querying the hash map, however, the query here is very frequent, and even hash map will bring a certain amount of efficiency loss.

Students familiar with the reactor mechanism know that the underlying communication can use an array of max_fd size as the eventing between FD and event_handler structures. The method in C ++ is as follows:

STD: vector <event_handler *> fd_map (max_fd (), (event_handler *) 0 );

The FD set returned by epoll_wait can be directly mapped to the corresponding event_handler with O (1) efficiency. Both FD and client_id exist in event_handler.

Okay. Now the FD ing between FD-> event_handler-> client_id is solved. What about client_id-> event_handler-> FD?

The simple method for generating client_id may be that an int increments from 0, and the client_id is used to query event_handler by hash map; it may be in the following structure:

STD: unordered_map <int, event_handler *> fd_client_id_map (max_fd ());

However, even hash map is more efficient than array subscript access. You may also know that if you store event_handler * in a static linked list *, client_id is the subscript of event_handler * in a static linked list. Then, event_handler * can be directly obtained through the subscript, and the path client_id-> event_handler-> FD can be connected through O (1.

Event_handler * is stored in a static linked list. Its subscript is used as its client_id. This static linked list is implemented using STL vector, which can be made to dynamically increase or decrease the size. fixed size is not required,

 

There are several other static linked lists related to specific applications.

 

Summary:

Later I thought about it carefully. In factIf you need to dynamically generate an ID and use this ID to map other things, you can use the static linked list as above to implement true O (1 ).

ArticleDownload http://files.cnblogs.com/logicbaby/%E9%9D%99%E6%80%81%E9%93%BE%E8%A1%A8%E5%BA%94%E7%94%A8.pdf

 

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.