Efficient Hash algorithm supporting progressive re-Hash

By adopting progressive rehash algorithm and memory pooling technology, the hash conflict and delay problems of hash tables when the data volume increases are solved, and more efficient hash table operations are achieved, ensuring the real-time and performance of the transaction system .

CN119513090APending Publication Date: 2025-02-25DALIAN FUTURES INFORMATION TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411571460.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-06
Publication Date
2025-02-25

AI Technical Summary

Technical Problem

In the prior art, the hash table will increase the hash conflict when the data volume increases, automatically expand and rehash, which will take a long time and affect the real-time and performance of the trading system.

Method used

Using progressive rehash algorithm, disperses rehash work across multiple insert data steps, processes rehash work for only one hash bucket at a time, and improves hashing through memory pooling technology and improved chain structure Efficiency of conflict avoidance.

Benefits of technology

It effectively reduces the delay in the trading system during the rehashing process, improves the efficiency of insertion and query, and ensures the smooth operation and user experience of the trading system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure FT_1
    Figure FT_1
  • Figure FT_2
    Figure FT_2
Patent Text Reader

Abstract

The invention discloses an efficient Hash algorithm supporting progressive re-Hash, in order to make full use of a first-level cache, a plurality of key values with the same Hash value are placed in a cache line, access to a memory is reduced, the utilization rate of a CPU (central processing unit) is improved, the progressive re-Hash algorithm does not perform re-Hash operation at one time any more, and the Hash efficiency is improved. Instead, the re-hash work is dispersed into a plurality of data insertion steps, and the re-hash work of only one hash bucket is performed each time, so that the one-time re-hash work is smoothed, the whole process can be smoothed by adopting a progressive hash algorithm, the delay of a transaction system is controlled within an acceptable range, an alarm is not triggered, and the transaction efficiency is improved. The method has no obvious influence on the stable operation of the whole transaction system, improves the search speed and insertion speed of the whole transaction system, reduces the delay of each order, efficiently utilizes the memory, and provides better use experience for users.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to software which needs to use a hash table and has relatively high real-time requirements for re-hashing operations in software development. Background Art

[0002] In the trading system, the unordered associative containers (std::unordered_map) provided by the C++ standard library are widely used to store orders, contracts, transactions and other information, and are frequently queried during the trading process. For example, in the contract table, we need to query by contract number to find the specific information of the contract. In the actual application process, we found that the unordered associative containers in the standard library have some problems:

[0003] 1. When the amount of data in the container reaches a certain amount, resulting in an increase in hash conflicts, the capacity will be automatically and dynamically expanded, and the standard library unordered associative container will be rehashed once, that is, the capacity of the container will be expanded, and the original data will be recalculated at the position where it should be inserted under the new capacity, and then reinserted. Since the above process is carried out during the processing of an order, and the rehashing work is time-consuming, sometimes exceeding 1 second depending on the actual amount of data, at peak times, this order and the hundreds of thousands of orders after this order will be delayed by more than 1 second, triggering a warning from the trading system, and also affecting the timely update of the trading system market, affecting the smooth operation of the entire futures market.

[0004] 2. When a hash conflict occurs, that is, two inserted data are inserted into the same position after calculation, hash conflicts need to be avoided, and a chain structure is used to prevent the two newly inserted data from being in the same linked list. When querying, it is necessary to traverse this linked list. Due to the characteristics of the linked list, the storage position dispersion of the two nodes on the linked list in the physical memory far exceeds the length of a cache line. The cache failure is relatively serious, and the memory will be frequently accessed. The efficiency of accessing the memory is much lower than that of accessing the cache, which seriously affects the performance.

[0005] 3. Every time a new piece of data is inserted, the standard library unordered associative container will dynamically create an object with forward and backward bidirectional pointers to assist in inserting the new data into the corresponding linked list. Too much dynamic allocation and release of internal small object memory will lead to performance degradation. Summary of the invention

[0006] The purpose of the present invention is to solve the problem in the prior art that when the amount of data in the container reaches a certain amount, hash conflicts increase, the capacity will be automatically and dynamically expanded, and the standard library unordered associative container will perform a one-time re-hashing, that is, the capacity of the container is expanded, and the original data is recalculated at the position where it should be inserted under the new capacity, and reinserted. The storage position discreteness of the two nodes on the linked list in the physical memory far exceeds the length of a cache, and the cache failure is relatively serious, and the memory will be frequently accessed, and the efficiency of accessing the memory is much lower than that of accessing the cache, which seriously affects the performance. Each time a new data is inserted, the standard library unordered associative container will dynamically create an object with forward and backward bidirectional pointers to assist the new data in inserting the corresponding linked list. The internal small object memory is dynamically allocated and released too much, which will lead to the problem of performance degradation.

[0007] In order to solve the above problems, the present invention provides an efficient hash algorithm that supports progressive rehashing, and the specific steps are as follows:

[0008] S1: The progressive rehashing algorithm distributes the rehashing work into several data insertion steps, and only rehashes one hash bucket at a time. The specific steps of the algorithm are as follows:

[0009] S1-1, when the filling factor is greater than 1, rehashing is triggered, creating a new hash bucket with doubled capacity. Under the current data volume, the filling factor can be halved to 0.5;

[0010] S1-2, each time a new piece of data is inserted, the new data is inserted into the new hash table while the rehashing work is being done;

[0011] S1-3, when S1-2 is inserted, a hash bucket is randomly taken from the old hash table, the linked list is traversed to recalculate the hash value of each key value data of each extended hash node, and then inserted into the new hash table, and the nodes originally in the same hash bucket in the old hash table are dispersed into two different hash buckets in the new hash table, and the insertion of a new data is completed;

[0012] S1-4. As data is continuously inserted, the data in the old hash table gradually decreases, and eventually all of it is transferred to the new hash table, completing the entire re-hashing operation.

[0013] S2: Improve CPU utilization;

[0014] The progressive rehashing algorithm uses the first-level cache to put multiple keys with the same hash value in one cache line. During the search process, the cache loads key values ​​from the memory in units of lines, and reads 64 bytes of memory at a time. The key values ​​that need to be compared will be pre-read into the cache during this memory read operation.

[0015] The specific steps for key-value comparison are as follows:

[0016] S2-1: When inserting a new data, first use the modulo operation to get the position of the key value in the hash table. After a hash conflict occurs, determine whether the last extended hash node in the hash table is full by determining whether the special identification byte is set in the last extended hash node in the hash table.

[0017] If not, copy the new data key value to the extended hash node;

[0018] If it is full, allocate a new extended hash node from the memory pool, write the new data into it, and connect the entire new extended hash node to the end of this linked list;

[0019] S2-2: When using the key value to call the query interface to find the corresponding data, calculate its position in the hash table through modulo operation, traverse each extended hash node in sequence along the linked list, and compare each key value in it with the key value to be searched. If the comparison matches, the query is successful and the traversal of the linked list ends. If the match is not successful, the query fails.

[0020] In a preferred embodiment, the progressive rehashing algorithm uses a memory pool technique to pre-allocate memory during the startup phase of the trading system and to stop the dynamic allocation of small objects when inserting data.

[0021] In a preferred embodiment, the progressive rehashing algorithm adopts an improved chain structure when avoiding hash conflicts, and each node of the chain list stores the key values ​​of several newly inserted data.

[0022] Modular arithmetic is a mathematical operation that is mainly used to process the remainder in integer division. Modular arithmetic has a wide range of applications in number theory and computer science, including but not limited to parity determination, prime number detection, greatest common divisor calculation, encryption algorithms, etc.

[0023] The present invention can achieve the following beneficial effects: 1. The progressive hash algorithm can be used to smooth the entire process, control the delay of the trading system within an acceptable range, and not trigger warnings, without significantly affecting the smooth operation of the entire trading system.

[0024] 2. The new algorithm adopts an improved chain structure to avoid hash conflicts. Each node of the linked list stores the key values ​​of several newly inserted data. Since the key values ​​of several new data are stored continuously in a node, they are physically continuous, and the first-level cache can be efficiently utilized, which improves the insertion and search efficiency. This can improve the search and insertion speed of the entire trading system, reduce the delay of each order, and provide users with a better user experience.

[0025] 3. The original new algorithm uses memory pool technology to pre-allocate memory at the startup stage of the trading system. It does not dynamically allocate small objects when inserting data, which improves efficiency and moves this part of the time overhead of each order to the transaction startup stage. Through the entire design, the latency of each order in the trading system is reduced, memory is used efficiently, and users have a better experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0026] Figure 1 This is a schematic diagram of the internal structure of a hash table that effectively utilizes the cacheline feature.

[0027] Figure 2 This is the schematic diagram of the progressive rehashing algorithm. DETAILED DESCRIPTION

[0028] Embodiment 1:

[0029] like Figure 1 Figure 2 As shown, the present invention provides an efficient hash algorithm that supports progressive rehashing, and the specific steps are as follows:

[0030] In the current computer architecture, the CPU frequency is very high, and the performance of the memory has become a bottleneck. In order to efficiently utilize the CPU, the first, second, and third level caches are generally provided, and the data in the memory is stored in these three levels of cache. The time overhead of memory access is dozens of times that of accessing the first level cache. Accessing the memory will cause the CPU pipeline to pause (that is, the CPU cannot get data in time and has to stop and wait for data), affecting performance. In order to make full use of the first level cache, this hash algorithm puts multiple key values ​​with the same hash value in one cache line as much as possible. In this way, during the search process, since the cache line loads key values ​​from the memory in units of lines, and reads 64 bytes of memory at a time, the key values ​​that need to be compared are likely to be pre-read into the cache in this memory read operation. In this way, the access to the memory is reduced and the performance is improved. When the standard library's unordered associative container searches for keys with the same hash value but different keys, there is a very high probability that these key values ​​are not in the first-level cache. Especially when the amount of data is large, these key values ​​may be in the memory. In this way, the memory is constantly accessed, and the advantages of the first-level cache cannot be used, which will cause the first-level cache to overflow and fail continuously, causing the CPU pipeline to pause, seriously affecting performance.

[0031] For specific algorithms, see Figure 1

[0032] 1. When inserting a new piece of data, first calculate the position of its key value in the left "hash bucket". After a hash conflict occurs, we check whether the "extended hash node" at the end is full. If not, copy the key value of the new data into it. If it is full, allocate a new "extended hash node" from the memory pool, write the new data into it, and then connect the entire new "extended hash node" to the end of the linked list.

[0033] 2. When querying the data corresponding to a key value, first calculate its position in the "hash bucket" on the left, then traverse each "extended hash node" in sequence along the linked list, and compare each key value with the key value with the search. If the comparison matches, the query is successful and the traversal of the linked list ends. If the entire linked list is traversed and no match is found, the query fails.

[0034] The futures trading system can be regarded as a soft real-time system. The time from the exchange receiving an order request to the time an order response is sent needs to be controlled within a certain range. Any factors that may cause large fluctuations need to be avoided. When the load factor (load factor: the ratio of the amount of data to the number of hash buckets) of the standard library unordered associative container exceeds a certain value, it will trigger rehashing. During rehashing, all data will be hashed once and then reinserted into the new hash table. Only after this work is completed will other business processes be performed. During the rehashing process, the entire trading system does not provide services to the outside world. This operation will cause the processing time fluctuation of the trading system to increase. To address this problem, this algorithm adopts a new progressive rehashing algorithm. Instead of performing a one-time rehashing operation, the rehashing work is dispersed into several data insertion steps, and only one hash bucket is rehashed each time, so that the one-time rehashing work is smoothed.

[0035] For specific algorithms, see Figure 2 :

[0036] 1. When the fill factor is greater than 1, the progressive rehashing process is triggered, creating a new hash bucket and doubling its capacity. This way, the fill factor can be halved to 0.5 based on the current amount of data. As shown in the figure, the number of hash buckets increases from 8 to 16.

[0037] 2. Every time a new piece of data is inserted, if it is found that the current rehashing work is being done, the new data is inserted into the new hash table. As shown in the figure, it is inserted into hash bucket 0.

[0038] 3. After the insertion in step 2 above is completed, as shown in the figure, a hash bucket data linked list labeled 5 is taken out from the old hash table, and then the linked list is traversed to recalculate the hash value of each key value data of each "extended hash node", and then inserted into the new hash table. The figure shows hash bucket No. 13 and hash bucket No. 14 of the new table, thus completing the insertion operation of a new data.

[0039] 4. As data is continuously inserted, the data in the old hash table gradually decreases, and eventually all of it is transferred to the new hash table.

[0040] 5. Delete the old hash table and complete the entire rehashing operation.

[0041] Embodiment 2:

[0042] An efficient hashing algorithm that supports progressive rehashing, the specific steps are as follows:

[0043] S1: The progressive rehashing algorithm distributes the rehashing work into several data insertion steps, and only rehashes one hash bucket at a time. The specific steps of the algorithm are as follows:

[0044] S1-1, when the filling factor is greater than 1, rehashing is triggered, creating a new hash bucket with doubled capacity. Under the current data volume, the filling factor can be halved to 0.5;

[0045] S1-2, each time a new piece of data is inserted, the new data is inserted into the new hash table while the rehashing work is being done;

[0046] S1-3, when S1-2 is inserted, a hash bucket is randomly taken from the old hash table, the linked list is traversed to recalculate the hash value of each key value data of each extended hash node, and then inserted into the new hash table, and the nodes originally in the same hash bucket in the old hash table are dispersed into two different hash buckets in the new hash table, and the insertion of a new data is completed;

[0047] S1-4. As data is continuously inserted, the data in the old hash table gradually decreases, and eventually all of it is transferred to the new hash table, completing the entire re-hashing operation.

[0048] S2: Improve CPU utilization;

[0049] The progressive rehashing algorithm uses the first-level cache to put multiple keys with the same hash value in one cache line. During the search process, the cache loads key values ​​from the memory in units of lines, and reads 64 bytes of memory at a time. The key values ​​that need to be compared will be pre-read into the cache during this memory read operation.

[0050] The specific steps for key-value comparison are as follows:

[0051] S2-1: When inserting a new data, first use the modulo operation to get the position of the key value in the hash table. After a hash conflict occurs, determine whether the last extended hash node in the hash table is full by determining whether the special identification byte is set in the last extended hash node in the hash table.

[0052] If not, copy the new data key value to the extended hash node;

[0053] If it is full, allocate a new extended hash node from the memory pool, write the new data into it, and connect the entire new extended hash node to the end of this linked list;

[0054] S2-2: When using the key value to call the query interface to find the corresponding data, calculate its position in the hash table through modulo operation, traverse each extended hash node in sequence along the linked list, and compare each key value in it with the key value to be searched. If the comparison matches, the query is successful and the traversal of the linked list ends. If the match is not successful, the query fails.

[0055] The progressive rehashing algorithm uses memory pool technology to pre-allocate memory during the trading system startup phase and stop the dynamic allocation of small objects when inserting data.

[0056] The progressive rehashing algorithm uses an improved chain structure to avoid hash conflicts, and each node of the linked list stores several key values ​​of newly inserted data.

[0057] The above are only preferred specific implementation modes of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can make equivalent replacements or changes according to the technical solutions and inventive concepts of the present invention within the technical scope disclosed by the present invention, which should be covered by the protection scope of the present invention.

Claims

1. An efficient hashing algorithm supporting progressive rehashing, characterized in that The specific steps are as follows: S1: The progressive rehashing algorithm distributes the rehashing work into several data insertion steps, and only rehashes one hash bucket at a time. The specific steps of the algorithm are as follows: S1-1, when the filling factor is greater than 1, rehashing is triggered, creating a new hash bucket with doubled capacity. Under the current data volume, the filling factor can be halved to 0.5; S1-2, each time a new piece of data is inserted, the new data is inserted into the new hash table while the rehashing work is being done; S1-3, when S1-2 is inserted, a hash bucket is randomly taken from the old hash table, the linked list is traversed to recalculate the hash value of each key value data of each extended hash node, and then inserted into the new hash table, and the nodes originally in the same hash bucket in the old hash table are dispersed into two different hash buckets in the new hash table, and the insertion of a new data is completed; S1-4. As data is continuously inserted, the data in the old hash table gradually decreases, and eventually all of it is transferred to the new hash table, completing the entire re-hashing operation. S2: Improve CPU utilization; The progressive rehashing algorithm uses the first-level cache to put multiple keys with the same hash value in one cache line. During the search process, the cache loads key values ​​from the memory in units of lines, and reads 64 bytes of memory at a time. The key values ​​that need to be compared will be pre-read into the cache during this memory read operation. The specific steps for key-value comparison are as follows: S2-1: When inserting a new data, first use the modulo operation to get the position of the key value in the hash table. After a hash conflict occurs, determine whether the last extended hash node in the hash table is full by determining whether the special identification byte is set in the last extended hash node in the hash table. If not, copy the new data key value to the extended hash node; If it is full, allocate a new extended hash node from the memory pool, write the new data into it, and connect the entire new extended hash node to the end of this linked list; S2-2: When using the key value to call the query interface to find the corresponding data, calculate its position in the hash table through modulo operation, traverse each extended hash node in sequence along the linked list, and compare each key value in it with the key value to be searched. If the comparison matches, the query is successful and the traversal of the linked list ends. If the match is not successful, the query fails.

2. The efficient hashing algorithm supporting progressive rehashing according to claim 1, characterized in that: The progressive rehashing algorithm uses memory pool technology to pre-allocate memory during the trading system startup phase and stop the dynamic allocation of small objects when inserting data.

3. The efficient hashing algorithm supporting progressive rehashing according to claim 1, characterized in that: The progressive rehashing algorithm uses an improved chain structure to avoid hash conflicts, and each node of the linked list stores several key values ​​of newly inserted data.