A Cuckoo hash optimization method based on coroutine

By using coroutines and data prefetching technology in the Cuckoo hash index, the parallelism of the hash index is optimized, solving the problem of insufficient concurrency of the Cuckoo hash index, achieving more efficient memory access and performance improvement, and reducing the cost development costs.

CN114116727BActive Publication Date: 2024-10-29HUNAN UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111460426.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-01
Publication Date
2024-10-29
Estimated Expiration
2041-12-01

AI Technical Summary

Technical Problem

The existing Cuckoo hash index has insufficient concurrency because queries rely on early data, resulting in a bottleneck problem in the database during memory access. In addition, the existing software prefetching technology requires asynchronous code reconstruction, which increases development costs.

Method used

Combined with the C++20 coroutine standard, coroutine and data prefetching technology are used to concurrently search for hash index paths through multiple coroutines, and memory-level hardware parallelism is used to optimize the Cuckoo hash index structure and search process to reduce memory access. Improve hash index performance.

Benefits of technology

It improves the database response speed, reduces the memory access frequency of the hash index, improves performance, adds almost no additional overhead, and reduces development costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114116727B_ABST
    Figure CN114116727B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of hash indexes, and pre-fetches a flag bit that indicates whether a hash bucket is fully occupied. Before the flag bit is used, data is pre-fetched into a cache, thereby avoiding frequent CPU access to memory; a coroutine is used to switch between multiple concurrent data streams, hiding the waiting time from pre-fetching data to returning data. The method proposed by the present invention can hide CPU stalls caused by frequent memory accesses during hash detection, thereby improving the insertion speed of the hash table. In view of the deficiencies of the prior art, the present invention provides a Cuckoo hash optimization method based on coroutines, which solves some of the problems existing in the prior art.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data indexing, and proposes an index optimization method, specifically, optimizing the common Cuckoo hash by using multi-coroutines to improve the insufficient concurrency caused by the query dependence on early data, thereby alleviating the memory bottleneck problem existing in the database. Background Art

[0002] As the world enters the era of big data, information technology has involved various industries and the amount of data is growing exponentially. How to build a more efficient database and quickly extract the target data from massive data is an important applied research direction.

[0003] Common databases include hierarchical databases, network databases, relational databases, and non-relational databases. Since hierarchical databases have many restrictions on insert and delete operations, and network databases have a complex structure, they are not easy for users to master. Therefore, there are two main types of databases that are most commonly used, namely relational databases and non-relational databases.

[0004] The most commonly used data index in non-relational databases is the hash index, which is represented by LibCuckoo. Storage objects are mapped to storage addresses through hash functions and are stored in hash tables. When searching for data or objects, the hash function is used to calculate the storage location of the element and quickly obtain the element.

[0005] However, the mapping of elements through the hash function is limited. There may be multiple elements mapped to the same hash bucket after the hash function, which will cause hash conflicts and reduce the insertion and search speed. Common methods for resolving hash conflicts include linked list method, open address method, and rehashing method.

[0006] LibCuckoo uses open addressing to solve the problem of hash conflicts. Specifically, Cuckoo hashing is used. The difference from other open addressing hashes is that Cuckoo hashing uses two hash functions to solve hash conflicts. The element X to be inserted passes through two hash functions h1 and h2 to obtain two candidate positions. As long as one of the two hash buckets is not empty, the key-value pair to be inserted is inserted into this candidate bucket. When both candidate buckets are occupied, the key-value pairs in any bucket are kicked out and the key-value pair to be inserted is inserted. For the kicked out key-value pairs, the hash function is reused to detect whether the other candidate hash bucket is empty, and the above steps are repeated until all the key-value pairs are inserted into the hash table. This kick-out path is called the Cuckoo path.

[0007] However, the existing LibCuckoo still has some problems. When inserting a key-value pair, it is necessary to find a Cuckoo path with a free hash bucket. Since the key-value pairs are scattered in the address space of the hash buckets mapped after the hash function, the information used recently or in the future is not adjacent to the information currently in use in the address space. When the CPU accesses the memory, there is no way to use the principle of spatial locality. The probability that the data to be accessed next time happens to be in the cache is very low, and it needs to be re-read from the memory to the cache. When querying certain data, the search for the target data depends on the acquisition of earlier data. The CPU cannot process searches on a Cuckoo path concurrently and needs to frequently wait for memory data to return.

[0008] To address this problem, the academic community has proposed many software prefetching-based technologies to alleviate the problem of CPU stalling caused by memory access. The main method is to concurrently prefetch memory data for multiple independent instruction streams, thereby utilizing the computer's memory-level hardware parallelism. However, the above technical solutions all require asynchronous reconstruction of the original synchronous code, which increases the development cost of developers.

[0009] Recently, coroutines have received increasing attention from academia and industry. Compared with existing technologies, coroutines can be used to write asynchronous code in the same way as synchronous code; and the pause and resume functions are completely defined by the user, and coroutine switching operations can be performed in user mode; coroutines can also be executed serially in threads, and there is no competition for critical section data, which greatly reduces the use of locks and improves program efficiency.

[0010] The present invention utilizes the characteristics of lightweight switching of coroutines, interleaves multiple search instruction streams, and fully utilizes memory-level hardware parallelism, thereby improving the database response speed. Summary of the invention

[0011] The present invention optimizes Cuckoo hash index in combination with the new coroutine standard proposed in C++20. The coroutine is combined with data prefetching, and the lightweight characteristics of coroutine switching are effectively utilized. While improving the parallelism of hash index, almost no additional overhead is added. By concurrently prefetching memory data, the access of memory to memory is reduced, and the hash index performance is improved. The implementation method is specifically described below from three aspects: index structure, Cuckoo path search, and multi-coroutine prefetching.

[0012] Index structure. To reduce the possibility of hash conflicts, our index structure expands each hash bucket into multiple slots, each of which can be used to store a key-value pair. By increasing the number of slots, we can overcome the disadvantage of high time complexity of chain reading and improve space utilization by using open addressing.

[0013] Cuckoo path search. During the data insertion process, when a hash conflict occurs in the inserted key-value pair, a breadth-first search (BFS) is used to find a path with free slots. This process uses multiple independent instruction streams to concurrently search for multiple paths with free slots. After finding the best available path, the hash bucket is kicked out mutually exclusively. In addition, during the search process, in order to avoid processor stalls, the memory data to be used is pre-fetched, and the upper-level memory with faster reading speed is used as a buffer for the lower-level memory, thereby better utilizing the memory-level hardware parallelism.

[0014] Multi-coroutine prefetching. For the search of the above multiple paths, we use a multi-coroutine approach. When the instruction stream is about to read the flag used to identify whether there are any free slots in the candidate bucket, a prefetch instruction for the memory address is issued in advance. Subsequently, the CPU switches the context to the next independent instruction stream, and when the expected data returns, it resumes the operation of the current instruction stream. This method creates the possibility of instruction parallelism between prefetching the memory address and using the memory address. When the data is actually used, the data is very likely to be in the CPU cache. Use coroutines to switch between multiple concurrent data streams to hide the waiting time from prefetching data to the return of data. The implementation of this method includes the following steps (such as Figure 1 shown):

[0015] (1) The key-value pair Key-value uses two hash functions and is mapped to two candidate buckets.

[0016] (2) Use a coroutine to issue a prefetch instruction to the array used to identify whether there are any free slots in the candidate pass.

[0017] (3) If any hash bucket has a free slot, the key-value pair Key-value is inserted into the hash bucket. Otherwise, a key-value pair Key-value is randomly kicked out and stored in its alternative position. At this time, the kicked out hash bucket has a free slot, and the target data is inserted.

[0018] (4) After issuing the prefetch instruction, set the coroutine pause point and hand over the CPU usage rights to other coroutines.

[0019] (5) Wait for the scheduler to schedule this coroutine again and continue to execute commands from the pause point.

[0020] (6) Repeat the above steps until all key-value pairs are inserted into the hash bucket. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 Diagram of a coroutine responding to an insert request.

[0022] Figure 2 Schematic diagram of the index data structure search Cuckoo path.

[0023] Figure 3 Schematic diagram of the relationship between process and thread coroutine. DETAILED DESCRIPTION

[0024] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings in the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0025] The present invention proposes a Cuckoo hash optimization method based on coroutine, which solves the problem of Cuckoo path search for data storage location (Cuckoo path search process is as follows Figure 2 ), the storage units are not gathered in a continuous space. Since the next element that the CPU wants to access is often not in the cache, the CPU utilization rate is low. In specific implementation, we use a constructed coroutine class to add prefetch instructions at locations where cache misses are prone to occur, and set the coroutine temporary point co_await after the prefetch instruction is issued. After that, the compiler automatically generates all the code required for suspension and resumption. Unlike asynchronous methods, this does not require rewriting a lot of code, saving developers' work costs and having better readability and maintainability.

[0026] The basic hardware for the implementation is: Intel(R) Xeon(R) Gold 5218R CPU. The operating system is: Ubuntu 18.04, and the kernel version is 5.0.0-23-generic.

[0027] 1. Data prefetching

[0028] The present invention uses the _mm_prefetch instruction to prefetch the data to be used into the cache. This instruction has two parameters: prefetch address and prefetch mode. The size of the prefetched data is the size of a cache line, and the prefetch mode is T0, that is, the data is prefetched into all levels of cache including L0. In order to prevent the required data from being replaced by the data of other instruction streams in the CPU cache, we collected the time from issuing the prefetch instruction to rescheduling the instruction stream through experiments, and found a suitable switching time. Doing so can optimize the response time of data indexing, reduce CPU pauses caused by memory access, and realize batch processing of data.

[0029] 2. Coroutine Implementation

[0030] Currently, the coroutines supported by C++ only provide users with a framework for asynchronous writing in a synchronous style, so we re-implemented a coroutine class Promise and used the Awatiable class to wrap all asynchronous functions. Three new keywords, co_await, co_return, and co_yield, are used to suspend the current coroutine, return a result from the current coroutine, and return a result from the current coroutine and suspend it.

[0031] In order to manage the coroutines, we defined and implemented multiple interfaces (get_return_object, initial_suspend, initial_suspend, return_value) as the medium for interacting with the coroutines. They are used to obtain the handle of the coroutine, define the operations to be performed at the start and end of the coroutine, and return the return value to be returned after the coroutine is completed. In addition, we have also implemented the three interfaces of await_suspend, await_resume, and await_ready to define when the coroutine can be awakened after being suspended, what value is used as the result returned by the coroutine when the coroutine is awakened, and to determine the current state of the coroutine.

[0032] The Cuckoo hash index is improved by using coroutines. The main process is as follows: after the current coroutine pre-fetches the flag that indicates whether there are free slots in the candidate bucket, it suspends execution and hangs up. It then returns to the caller of the coroutine function and hands over the CPU control to the next coroutine. The previous coroutine resumes execution from the paused position after the memory data it is waiting for is returned. While waiting for the memory data to be fetched into the cache, the CPU can handle other operations. In this process, there is no need to suspend the thread where the coroutine is located (the relationship between the thread and the coroutine is shown in the attached figure). Figure 3 As shown in the figure, it simply pauses and resumes the function specified by the user. By using the coroutine to prefetch and switch between multiple concurrent instruction streams, scheduling can be made faster and more efficient.

[0033] The pseudo code for the method implementation is as follows:

[0034]

[0035]

Claims

1. A coroutine-based Cuckoo hash optimization method, Features The following steps are involved: (1) The key-value pair Key-value is mapped to two candidate storage buckets through two hash functions; (2) Use a coroutine to issue a prefetch instruction to the flag bit that indicates whether there are any free slots in the candidate bucket; (3) If any of them has an empty slot, insert the key-value pair Key-value into the hash bucket; otherwise, randomly kick out a key-value pair Key-value and put it in its alternative position; (4) The user defines the pause point and gives the CPU usage rights to other coroutines. (5) Wait for the scheduler to schedule this coroutine again and continue to execute commands from the pause point; (6) Repeat the above steps until all key-value pairs are inserted into the hash bucket.

2. A coroutine-based Cuckoo hash optimization method according to claim 1, Features This method specifically performs the following operations: (1) Defines multiple interfaces get_return_object, initial_suspend, and return_value as media for interacting with coroutines. They are used to obtain the handle of the coroutine, define the operations required at the start and end of the coroutine, and return the return value required after the coroutine is completed. (2) The three interfaces await_suspend, await_resume, and await_ready are implemented to define when the coroutine can be awakened after being suspended, what value is used as the result returned by the coroutine when it is awakened, and to determine the current state of the coroutine.

3. A coroutine-based Cuckoo hash optimization method according to claim 1, Features The _mm_prefetch instruction is used to supplement the data fetched by the CPU in the cache, and the following operations are performed: (1) Set the prefetch instruction address and the prefetch data size to the size of a cacheline; (2) Set the prefetch mode to T0 and prefetch data into all levels of cache including L0; (3) Collect the time from when the prefetch instruction is issued to when the instruction stream is switched back to the instruction stream, and find a suitable coroutine switching time.

Citation Information

Patent Citations

  • Chain log storage structure, hash index structure and data operation method thereof, server and medium

    CN107515901A

  • Data calling method and device

    CN108628611A