Optimization method and device of embedded linked list container, electronic equipment and storage medium
By embedding linked list nodes into the host data structure and constructing a doubly linked list using offset operations, the problem of iterator failure in high-concurrency scenarios of traditional linked list containers is solved, achieving more efficient and secure memory management and data access.
Patent Information
- Application Number
- CN202511229193.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-29
- Publication Date
- 2025-10-31
AI Technical Summary
In high-concurrency scenarios, iterator failures in traditional linked list containers may lead to undefined behavior, affecting the system's security and operational reliability. Furthermore, the discontinuous physical distribution of nodes can cause cache misses, increasing access latency.
The linked list nodes are embedded in the host data structure as member variables. The offset is calculated by a predefined member offset calculation macro and stored in the linked list container. The address of the host data structure is obtained based on reverse pointer operation. A doubly linked list is constructed by using the predecessor and successor pointers of intrusive nodes. Atomic operations are used to maintain the integrity and security of the linked list.
It reduces memory overhead, lowers access latency, and improves system security and operational reliability.
Smart Images

Figure CN120872251A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to optimization methods, apparatus, electronic devices and storage media for embedded linked list containers. Background Technology
[0002] Linked lists, as an important component of data structures, are widely used in operating systems, embedded systems, and high-performance computing, playing an irreplaceable role, especially in scenarios requiring frequent insertion and deletion operations. However, with the increasing demands for memory efficiency and access performance in embedded systems, traditional linked list containers are gradually revealing their limitations in memory management and caching optimization.
[0003] Traditional linked list nodes typically contain data fields and pointer fields. The pointer field occupies additional memory space, and when the data field has a complex structure, the node's memory overhead increases significantly, impacting overall performance. Furthermore, because nodes are not contiguously distributed in physical memory, consecutive access to linked list elements can easily lead to cache misses, increasing access latency. In high-concurrency scenarios, iterator invalidation issues can also lead to undefined behavior, affecting the system's exception handling and operational reliability. Summary of the Invention
[0004] This application provides optimization methods, apparatus, electronic devices, and storage media for embedded linked list containers, in order to at least solve the problem in related technologies that in high-concurrency scenarios, iterator failure may also lead to undefined behavior, affecting the abnormal security and operational reliability of the system.
[0005] This application provides an optimization method for embedded linked list containers, including:
[0006] The linked list node is embedded as a member variable of the host data structure;
[0007] The offset of the linked list node in the host data structure is calculated using a predefined member offset calculation macro, and the offset is stored in the linked list container.
[0008] The address of the host data structure is obtained using reverse pointer arithmetic based on the offset.
[0009] A doubly linked list is constructed based on the predecessor and successor pointers of intrusive nodes, and the integrity and exception safety of the linked list are maintained through atomic insertion and deletion operations.
[0010] Optionally, embedding the linked list node as a member variable of the host data structure includes:
[0011] The linked list node is defined as an embedded linked list node unit type in the host data structure and is directly included in the host data structure as a structure member variable;
[0012] The host data structure and the linked list nodes are arranged consecutively in memory through a memory alignment mechanism.
[0013] Optionally, the step of calculating the offset of the linked list node in the host data structure using a predefined member offset calculation macro and storing the offset in the linked list container includes:
[0014] The calculated offset is stored in the node offset attribute of the unsigned integer type of the linked list container; wherein, the node offset attribute is used for reverse pointer operation and host data access.
[0015] Optionally, obtaining the address of the host data structure based on the offset using reverse pointer arithmetic includes:
[0016] Using the offset as a parameter, the address of the host data structure is calculated in reverse using pointer arithmetic; wherein, the host data is directly accessed through the pointer arithmetic.
[0017] Optionally, obtaining the address of the host data structure based on the offset using reverse pointer arithmetic further includes:
[0018] Establish the association between the iterator and the linked list container;
[0019] Get and store the offset of the linked list node in the host data structure within the linked list container;
[0020] The target intrusive node in the linked list is located based on the traversal mechanism of the iterator.
[0021] Based on the iterator and the offset, reverse pointer operation is performed to calculate the address of the host data structure to which the target intrusive node belongs. The iterator includes an automatic repair mode, a traditional traversal mode, and a pre-cached next node mode.
[0022] Optionally, the linked list container is used to perform at least one of the following operations: inserting a node at the head of the linked list, inserting a node at the tail of the linked list, removing the head node of the linked list, removing the tail node of the linked list, accessing the head node data of the linked list, accessing the tail node data of the linked list, determining whether the linked list is empty, and clearing all nodes in the linked list.
[0023] Optionally, the method further includes:
[0024] The intrusive node is directly embedded as a member variable of the host data structure, so that the intrusive node is completed synchronously with the memory allocation of the host data structure.
[0025] By embedding the intrusive nodes with the host data structure, the intrusive nodes and the host data form a compact and continuous layout in memory;
[0026] By embedding nodes directly into the host data structure in an intrusive manner, the linked list nodes and the host data form a compact and continuous memory layout. This avoids the memory access dispersion problem caused by the random scattering of nodes in different memory locations in traditional linked lists, reduces the memory page cache miss rate, improves the memory access hit rate, and optimizes memory locality.
[0027] This application also provides an optimized apparatus for an embedded linked list container, comprising:
[0028] An embedding unit is used to embed a linked list node as a member variable of the host data structure into the host data structure;
[0029] The calculation unit is used to calculate the offset of the linked list node in the host data structure through a predefined member offset calculation macro, and store the offset in the linked list container;
[0030] The acquisition unit is used to obtain the address of the host data structure based on the offset by using reverse pointer arithmetic;
[0031] The building unit is used to construct a doubly linked list based on the predecessor and successor pointers of intrusive nodes, and maintains the integrity and exception safety of the linked list through atomic insertion and deletion operations.
[0032] Optionally, the embedding unit is further configured to:
[0033] The linked list node is defined as an embedded linked list node unit type in the host data structure and is directly included in the host data structure as a structure member variable;
[0034] The host data structure and the linked list nodes are arranged consecutively in memory through a memory alignment mechanism.
[0035] Optionally, the computing unit is further configured to:
[0036] The calculated offset is stored in the node offset attribute of the unsigned integer type of the linked list container; wherein, the node offset attribute is used for reverse pointer operation and host data access.
[0037] Optionally, the acquisition unit is further configured to:
[0038] Using the offset as a parameter, the address of the host data structure is calculated in reverse using pointer arithmetic; wherein, the host data is directly accessed through the pointer arithmetic.
[0039] Optionally, the acquisition unit is further configured to:
[0040] Establish the association between the iterator and the linked list container;
[0041] Get and store the offset of the linked list node in the host data structure within the linked list container;
[0042] The target intrusive node in the linked list is located based on the traversal mechanism of the iterator.
[0043] Based on the iterator and the offset, reverse pointer operation is performed to calculate the address of the host data structure to which the target intrusive node belongs. The iterator includes an automatic repair mode, a traditional traversal mode, and a pre-cached next node mode.
[0044] Optionally, the linked list container is used to perform at least one of the following operations: inserting a node at the head of the linked list, inserting a node at the tail of the linked list, removing the head node of the linked list, removing the tail node of the linked list, accessing the head node data of the linked list, accessing the tail node data of the linked list, determining whether the linked list is empty, and clearing all nodes in the linked list.
[0045] Optionally, the device further includes:
[0046] The processing unit is used to directly embed the intrusive node as a member variable of the host data structure, so that the intrusive node is completed synchronously with the memory allocation of the host data structure.
[0047] The association unit is used to create a compact and continuous layout of the intrusive node and the host data in memory through the embedded association between the intrusive node and the host data structure.
[0048] The processing unit is also used to create a compact, contiguous memory layout between linked list nodes and host data by directly embedding nodes into the host data structure through intrusive methods. This avoids the memory access dispersion problem caused by nodes being randomly scattered in different memory locations in traditional linked lists, reduces memory page cache miss rate, improves memory access hit rate, and optimizes memory locality.
[0049] This application also provides an electronic device, including: a memory for storing a computer program; and a processor for implementing the steps of any of the above-described optimized methods for embedded linked list containers when executing the computer program.
[0050] This application also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of any of the above-described optimized methods for embedded linked list containers.
[0051] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of any of the above-described optimized methods for embedded linked list containers.
[0052] By embedding linked list nodes into the host data structure as member variables, this application eliminates the extra memory usage of pointer fields in traditional linked lists. Furthermore, the physical contiguous storage of nodes and the host data structure reduces cache misses. Simultaneously, atomic insertion and deletion operations ensure the stability of the linked list under high concurrency, rather than using traditional independent node structures and non-atomic operations. Therefore, it solves the technical problems in existing technologies, such as increased overhead due to extra memory usage of pointer fields in traditional linked list nodes, increased access latency due to cache misses caused by discontinuous node physical distribution, and the impact of iterator failures on system anomaly security and operational reliability under high concurrency. This achieves the technical effects of reducing memory overhead, lowering access latency, and improving system anomaly security and operational reliability. Attached Figure Description
[0053] To more clearly illustrate the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0054] Figure 1 A flowchart illustrating an optimization method for an embedded linked list container provided in an embodiment of this application;
[0055] Figure 2 A schematic diagram of the structure of an optimization device for an embedded linked list container provided in an embodiment of this application;
[0056] Figure 3 This is a schematic diagram of the structure of another optimized device for an embedded linked list container provided in an embodiment of this application. Detailed Implementation
[0057] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the protection scope of this application.
[0058] It should be noted that, in the description of this application, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. The terms "first," "second," etc., in this application are used to distinguish similar objects and are not used to describe a specific order or sequence.
[0059] To enable those skilled in the art to better understand the present application, the present application will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0060] The embodiments of this application provide an optimization method for an embedded linked list container. The method is described in detail below, taking into account the execution flow of the optimization method for the embedded linked list container. Figure 1 This is a flowchart illustrating an optimization method for an embedded linked list container provided in an embodiment of this application.
[0061] like Figure 1 As shown, the method includes the following steps:
[0062] Step 101: Embed the linked list node as a member variable of the host data structure into the host data structure;
[0063] A linked list node is the basic functional unit that makes up a linked list. Internally, it maintains a predecessor pointer (prev) and a successor pointer (next) for establishing relationships between nodes. It also has functions to determine whether a node is in the linked list, to remove itself from the linked list, and to insert itself before or after other nodes, providing basic support for the connection and management of the linked list. The host data structure, on the other hand, is the carrier of actual business data, used to store various types of business information that need to be organized and managed through the linked list, such as integers, characters, and custom structures.
[0064] Specifically, in defining the host data structure, the aforementioned fully functional linked list nodes are directly declared and defined as internal member variables of the host data structure. This allows the linked list nodes and the business data stored in the host data structure to form a complete and indivisible memory unit. This embedding method differs from the traditional linked list approach of storing nodes and data separately and then establishing a connection through pointers. Instead, it allows the linked list nodes and the business data of the host data structure to share the same contiguous memory space. In this way, the host data structure retains its own business data storage capabilities while also directly possessing the functional attributes of linked list nodes through its embedded linked list nodes. There is no need to allocate separate memory resources for the linked list nodes or construct complex mapping relationships between nodes and host data. When the host data structure needs to be incorporated into the linked list for management later, connections between different host data structures can be achieved simply by using the embedded linked list nodes within the host data structure and manipulating the predecessor and successor pointers within the nodes. This provides a foundation for efficient linked list construction and subsequent insertion, deletion, and traversal operations, while also creating conditions for reducing memory fragmentation and optimizing memory layout.
[0065] Step 102: Calculate the offset of the linked list node in the host data structure using a predefined member offset calculation macro, and store the offset in the linked list container;
[0066] This function calculates the offset of a linked list node within the host data structure using a predefined member offset calculation macro and stores this offset in the linked list container. The predefined member offset calculation macro is a tool specifically designed to calculate the offset of a specific member within a structure or class relative to the starting address of its parent object. Its core logic involves constructing a virtual host data structure pointer pointing to a fixed base address, obtaining the address of the linked list node member under this virtual pointer, and then obtaining the offset through address difference operations (i.e., member address minus base address). This ensures that the calculation result accurately reflects the positional relationship of the linked list node within the host data structure, avoiding positional errors caused by differences in memory layout.
[0067] Once the type definition of the host data structure and the declaration of the linked list node members are determined, the predefined macro is invoked, passing in the type identifier of the host data structure and the name of the linked list node member. The macro automatically executes its internal address calculation process, generating an unsigned integer offset value. This value represents the number of bytes from the starting memory address of the host data structure object to the starting memory address of the linked list node member, serving as the key basis for subsequent reverse location of the host data through the linked list node. The linked list container, as the core component managing all host data structures embedded with linked list nodes, maintains key attributes supporting linked list operations. Storing the calculated offsets in the linked list container avoids recalculating the offsets for each subsequent node operation (such as data access and node traversal), reducing unnecessary computational overhead. Simultaneously, the stored offsets become the core parameters for the linked list container to deduce the starting address of the host data structure from the linked list node address, providing fundamental data support for achieving zero-copy data access and ensuring the efficiency of linked list operations. This ensures that the linked list container can accurately establish the association between linked list nodes and host data when managing different host data structures, maintaining the stable operation of the entire linked list system.
[0068] Step 103: Obtain the address of the host data structure based on the offset using reverse pointer arithmetic;
[0069] Based on the offset already stored in the linked list container, the address of the host data structure is obtained through reverse pointer arithmetic. Reverse pointer arithmetic refers to the operation method of deriving the starting memory address of the object by starting from the known memory address of the member variable (i.e., the linked list node embedded in the host data structure) and combining it with the fixed offset of the member in its parent object (host data structure). Its core value lies in the fact that it can directly locate the parent object through the member address without establishing an additional mapping relationship, avoiding the additional memory overhead and efficiency loss in the data association process.
[0070] Since the offset of the linked list node in the host data structure has already been obtained through the predefined member offset calculation macro, and this offset is stored in the linked list container, when performing reverse pointer operations, this offset can be directly called from the linked list container as the core parameter, eliminating the need to repeat the offset calculation process and effectively reducing redundant operations. In the specific calculation process, the actual memory address of the linked list node to be operated on is first obtained, and then this address is converted into a character pointer—because the address increment and decrement operations of character pointers are performed on a single byte basis, they can accurately match the byte-based numerical definition of the offset, ensuring calculation precision. Next, using the converted character pointer as a base, the offset value stored in the linked list container is subtracted, and the result is the starting memory address of the host data structure.
[0071] This entire operation requires no copying of business data within the host data structure, nor does it require additional memory allocation. It achieves zero-copy location from the linked list node to the host data structure, ensuring efficient address acquisition while avoiding memory waste. The host data structure address obtained through reverse pointer arithmetic is a crucial prerequisite for subsequent access to business data and execution of data read / write operations. It also provides the data association foundation for subsequent node traversal and data retrieval functions, ensuring that the embedded generic linked list container system can accurately and efficiently establish the correspondence between linked list nodes and host data when managing different types of host data structures, thus guaranteeing the stability and efficiency of data access throughout the entire linked list system.
[0072] Step 104: Construct a doubly linked list based on the predecessor and successor pointers of the intrusive node, and maintain the integrity and exception safety of the linked list through atomic insertion and deletion operations.
[0073] A doubly linked list is constructed based on the predecessor and successor pointers of intrusive nodes, and atomic insertion and deletion operations are used to maintain the integrity and exception handling of the linked list. An intrusive node refers to a linked list node embedded in the host data structure. Its pre-maintained predecessor pointer (_prev) and successor pointer (_next) are the core support for constructing the doubly linked list—the predecessor pointer is used to associate the current node with the previous node, and the successor pointer is used to associate the current node with the next node. Through the bidirectional binding of these two pointers, multiple host data structures with embedded linked list nodes can be linked together into a logically coherent doubly linked list: the predecessor pointer of each node points to its predecessor node, and the successor pointer of the predecessor node points back to the current node; the successor pointer of each node points to its successor node, and the predecessor pointer of the successor node also points back to the current node. This bidirectional association not only enables bidirectional traversal of the linked list from front to back and from back to front, but also provides a structural foundation for the rapid location and operation of subsequent nodes.
[0074] In the daily maintenance of doubly linked lists, both insertion and deletion operations employ atomic design. The core characteristic of atomic operations is that the operation process is indivisible; it either succeeds completely or fails completely, preventing the linked list from being in a half-finished, invalid state due to intermediate exceptions. Taking the insertion operation as an example, when a node needs to be inserted after or before a specified node, an assertion mechanism is first used to verify that the node to be inserted is in an empty state without any associated linked list, avoiding pointer confusion caused by repeated insertions. Then, the predecessor and successor pointers of the node to be inserted are set sequentially, so that they point to the specified node and its original adjacent node, respectively. Then, the corresponding pointers of the specified node and its original adjacent nodes are updated synchronously, smoothly incorporating the node to be inserted into the linked list structure. The entire process is without intermediate breakpoints, ensuring that the linked list remains intact throughout the insertion process. The deletion operation is implemented through the node's own removal function. Before deletion, the node's conditional function is used to confirm that it is in the linked list, ensuring the legality of the deletion operation.
[0075] Then, the successor pointer of the predecessor node and the predecessor pointer of the successor node of the current node are updated to establish a direct association between them. Finally, the predecessor and successor pointers of the current node are reset to point to themselves, completing the safe detachment of the node from the linked list. This atomic operation logic effectively avoids problems such as linked list breakage, node loss, or dangling pointers caused by improper pointer update order in traditional linked list operations. At the same time, it ensures the safety of operations in case of anomalies—even if unexpected situations occur during the operation, the linked list can always maintain its structural integrity and will not have undefined data access behavior, thereby ensuring the stable operation of the entire embedded generic linked list container system.
[0076] In some embodiments, embedding the linked list node as a member variable of the host data structure includes:
[0077] The linked list node is defined as an embedded linked list node unit type in the host data structure and is directly included in the host data structure as a structure member variable;
[0078] The host data structure and the linked list nodes are arranged consecutively in memory through a memory alignment mechanism.
[0079] When embedding a linked list node as a member variable of the host data structure, two core operations are involved: First, the linked list node must be defined as an embedded linked list node unit type within the host data structure. This unit type is not an external component independent of the host data structure, but rather a specific type with complete linked list node functionality. It pre-integrates a predecessor pointer (prev) and a successor pointer (next) for establishing bidirectional associations between nodes. It also includes logic for determining whether a node is in the linked list, logic for removing a node from the list, and logic for inserting a node before or after other nodes, directly supporting the subsequent connection and management needs of the linked list. After defining this unit type, it must be directly declared and included as a structure member variable of the host data structure. This allows the host data structure to naturally integrate the functional attributes of the linked list node while storing business data (such as numerical, character, or custom data information in various business scenarios). The two form an inseparable whole, unlike the traditional linked list model where nodes and data are stored separately and require additional pointers to establish associations. This structurally eliminates the mapping overhead between nodes and data.
[0080] Secondly, a memory alignment mechanism is needed to ensure the contiguous arrangement of the host data structure and linked list nodes in memory. This mechanism is a memory layout optimization method based on hardware architecture and compiler rules. Its core purpose is to avoid fragmented memory gaps within the host data structure caused by the memory usage characteristics of different data types. During the definition of the host data structure, the compiler automatically adjusts the arrangement order and memory allocation of the business data and embedded linked list node units within the host data structure according to the memory alignment standard of the target runtime environment (e.g., 4-byte alignment in 32-bit systems and 8-byte alignment in 64-bit systems). This ensures that the starting memory address of the linked list node unit immediately follows the ending memory address of the business data in the host data structure, with no extra free bytes in between, forming a contiguous memory block. This continuous arrangement not only avoids wasting memory space, but also allows access to business data and linked list nodes in the host data structure to be concentrated in adjacent memory areas. Subsequent program access is more likely to hit the CPU cache, reducing cache misses caused by scattered memory addresses and optimizing memory access efficiency. At the same time, it lays a compact memory layout foundation for locating host data and building a complete linked list structure by reverse locating the linked list nodes. This ensures that after the embedding operation, the host data structure can not only carry business data, but also seamlessly participate in the construction and management of the linked list.
[0081] In some embodiments, calculating the offset of the linked list node in the host data structure using a predefined member offset calculation macro and storing the offset in the linked list container includes:
[0082] The calculated offset is stored in the node offset attribute of the unsigned integer type of the linked list container; wherein, the node offset attribute is used for reverse pointer operation and host data access.
[0083] The offset is stored in the unsigned integer node offset attribute of the linked list container. This node offset attribute plays a crucial role in both reverse pointer operations and accessing the host data. As the core module of the host data structure that manages all embedded linked list nodes, the linked list container predefines a dedicated node offset attribute, and the type of this attribute is explicitly set to unsigned integer. This is because the offset is essentially the number of bytes from the starting memory address of the host data structure to the starting memory address of the linked list node; its value is always non-negative. The unsigned integer type can accurately match the numerical characteristics of the offset, avoiding interference from the sign bit in address calculation, and can also adapt to the value range of the offset under different memory sizes, preventing storage errors caused by numerical overflow and ensuring the integrity of the offset data.
[0084] Once the calculated offset is stored in the node's offset attribute, this attribute becomes the core data support for subsequent operations of the linked list container. In reverse pointer operations, there is no need to call the member offset calculation macro again to recalculate the offset before each operation. Instead, the stored value can be read directly from the node's offset attribute. Combined with the address of the currently operated linked list node, the starting address of the host data structure can be quickly and accurately derived through character pointer subtraction (i.e., converting the linked list node address to a character pointer and subtracting the offset). This process eliminates redundant calculation steps, significantly reducing the time overhead of address derivation. At the same time, it ensures that the offset used in each reverse pointer operation is consistent, avoiding deviations that may occur due to repeated calculations and ensuring the accuracy of address association.
[0085] In host data access scenarios, the offset stored in the node offset attribute also plays a crucial role: when it is necessary to read or modify business data in the host data structure, based on the offset provided by this attribute, the starting address of the host data structure can be located through reverse pointer arithmetic. Data operations can then be performed directly on the memory region pointed to by that address, without the need to construct an additional mapping relationship between linked list nodes and host data, or to perform data copying. This achieves zero-copy host data access, reducing memory resource consumption and improving data access efficiency. Furthermore, storing offsets centrally in the node offset attribute of the linked list container ensures that the linked list container always obtains offsets through a unified attribute when managing different types of host data structures, guaranteeing the standardization and consistency of linked list operations. This provides a fundamental data guarantee for the stable and efficient operation of the entire embedded generic linked list container system.
[0086] In some embodiments, obtaining the address of the host data structure based on the offset using reverse pointer arithmetic includes:
[0087] Using the offset as a parameter, the address of the host data structure is calculated in reverse using pointer arithmetic; wherein, the host data is directly accessed through the pointer arithmetic.
[0088] After storing the offsets of the linked list nodes within the host data structure in the linked list container, to establish a direct association between the linked list nodes and the host data for data access, it is necessary to obtain the address of the host data structure using reverse pointer arithmetic based on these offsets. This pointer arithmetic allows direct access to the host data. The core logic of reverse pointer arithmetic is to deduce the starting memory location of the host data structure by relying on the numerical meaning of the offset—that is, the number of bytes from the starting address of the host data structure to the starting address of the linked list node. When performing this operation, the actual memory address of the linked list node to be operated on is first obtained; this address is the starting point of the operation. Since the offset is measured in bytes, the address of the linked list node needs to be converted to a character pointer. Character pointers move in units of 1 byte by default when incrementing or decrementing addresses, which accurately matches the measurement standard of the offset and avoids calculation errors caused by mismatches between the address movement units of other data type pointers (such as integer pointers) and the offset units.
[0089] Subsequently, based on the converted character pointer, the offset value stored in the linked list container is subtracted, and the result is the starting memory address of the host data structure. This operation does not rely on additional mapping tables or intermediate data structures; it can be completed solely through address difference calculations, ensuring the efficiency and accuracy of obtaining the host data structure address. After obtaining the host data structure address through this pointer operation, the host data can be accessed directly based on this starting address: there is no need to copy the host data or allocate additional memory space to store data copies. Simply by combining this starting address with the internal data layout of the host data structure, business data can be directly read and written—for example, accessing integer values, character arrays, or custom structure members stored in the host data structure. This direct access method eliminates intermediate steps in the data transfer process, reducing memory resource consumption and avoiding the time overhead of data copying. At the same time, because the linked list nodes and the host data structure are arranged continuously in memory (previously guaranteed by the memory alignment mechanism), the address obtained by the reverse pointer operation can accurately point to the valid data area of the host data structure, ensuring the accuracy and stability of data access. This provides key support for quickly obtaining business data in subsequent linked list traversal, data retrieval and other operations, and further improves the data operation efficiency of the entire embedded generic linked list container system.
[0090] In some embodiments, obtaining the address of the host data structure based on the offset using reverse pointer arithmetic further includes:
[0091] Establish the association between the iterator and the linked list container;
[0092] Get and store the offset of the linked list node in the host data structure within the linked list container;
[0093] The target intrusive node in the linked list is located based on the traversal mechanism of the iterator.
[0094] Based on the iterator and the offset, reverse pointer operation is performed to calculate the address of the host data structure to which the target intrusive node belongs. The iterator includes an automatic repair mode, a traditional traversal mode, and a pre-cached next node mode.
[0095] Establishing a connection between the iterator and the linked list container is crucial. The iterator, as the core tool for traversing linked list nodes and accessing data, is not an independent component. It needs to be bound to the linked list container that manages the linked list nodes. Through this connection, the iterator can obtain key information maintained in the linked list container (such as the address of the linked list head node, node management rules, etc.), ensuring that the iterator can accurately identify the boundaries and node distribution of the linked list. This provides basic support for locating target nodes and performing address calculations, avoiding node positioning deviations or operation failures caused by the iterator being disconnected from the container.
[0096] The iterator retrieves and stores the previously calculated offsets of the linked list nodes within the host data structure from the established linked list container. These offsets are the core parameters for subsequent reverse pointer operations, stored in a dedicated attribute within the iterator. This eliminates the need to repeatedly retrieve the offsets from the linked list container during each address calculation, reducing data interaction overhead and ensuring the iterator can access the offsets at any time during traversal, thus enabling rapid address deduction.
[0097] Three traversal modes based on iterators are used to locate the target intrusive node in a linked list: In automatic repair mode, if the iterator encounters an invalid node that has been deleted from the linked list during traversal, it will automatically skip it and switch to the pre-stored next valid node, avoiding traversal anomalies caused by node deletion; in traditional traversal mode, the iterator visits nodes sequentially according to the predecessor and successor pointers, consistent with the logic of regular linked list traversal, adapting to basic traversal scenarios; in pre-caching the next node mode, the iterator simultaneously stores the address of the next node when accessing the current node, reducing latency caused by pointer chasing in high-concurrency scenarios and improving traversal efficiency. Through the traversal logic of the corresponding modes, the iterator can accurately locate the target intrusive node to be operated on and obtain the actual memory address of that node.
[0098] Based on the offset stored in the iterator and the located address of the intrusive node, a reverse pointer operation is performed: the address of the intrusive node is converted into a character pointer (ensuring that address operations are performed in bytes, matching the offset measurement standard), and then the stored offset value is subtracted from this character pointer. The result is the address of the host data structure to which the intrusive node belongs. This process relies on the association between the iterator and the container, the pre-stored offset, and flexible traversal positioning. It ensures the accuracy of address calculation, improves the efficiency of address acquisition in different scenarios through the pattern adaptability of the iterator, and provides an accurate address basis for subsequent direct access to business data in the host data structure.
[0099] In some embodiments, the linked list container is used to perform at least one of the following operations: inserting a node at the head of the linked list, inserting a node at the tail of the linked list, removing the head node of the linked list, removing the tail node of the linked list, accessing the head node data of the linked list, accessing the tail node data of the linked list, determining whether the linked list is empty, and clearing all nodes in the linked list.
[0100] As the core component of the host data structure that manages all embedded linked list nodes, the linked list container not only stores node offsets to support reverse pointer operations, but also provides diverse node operation capabilities to meet the linked list management needs of different business scenarios. Specifically, it can perform at least one of the following operations: inserting a node at the head of the linked list, inserting a node at the tail of the linked list, removing the head node of the linked list, removing the tail node of the linked list, accessing the data of the head node of the linked list, accessing the data of the tail node of the linked list, determining whether the linked list is empty, and clearing all nodes in the linked list.
[0101] When inserting a node at the head of a linked list, the linked list container first obtains the address of the head node it maintains. By manipulating the predecessor and successor pointers of the node to be inserted, it makes the successor of the node to be inserted point to the original successor of the head node, and simultaneously makes the predecessor of the original successor of the head node point to the node to be inserted. Then, it makes the successor of the head node point to the node to be inserted and the predecessor of the node to be inserted point to the head node, thus completing the embedding of the node at the head. The entire process relies on direct manipulation of the pointers of the intrusive node, without the need for additional memory allocation. The logic for inserting a node at the tail of the linked list is similar. It is only necessary to locate the tail node of the linked list (quickly obtained through the predecessor pointer of the head node), adjust the pointer association between the node to be inserted and the tail node, and the original predecessor of the tail node, to achieve the addition of the node at the tail.
[0102] When removing the head node of a linked list, the linked list container first confirms that the list is not empty, then locates the successor node of the head node (i.e., the first valid node at the head), and re-establishes a direct association between the predecessor pointer and the successor pointer of that node (the successor of the head node points to the successor of this node, and the predecessor of this node points to the head node). Then, the predecessor and successor pointers of the node to be removed are reset to point to itself, ensuring that it is removed from the linked list. When removing the tail node of a linked list, the tail node is located through the predecessor pointer of the head node, and the pointer association between the predecessor of the tail node and the head node is adjusted to complete the removal of the tail node.
[0103] When accessing data at the head node of a linked list, the linked list container first locates the successor node of the head node (the valid head node), and then, in conjunction with the stored node offset, obtains the corresponding host data structure address through reverse pointer arithmetic (converting the node address to a character pointer and subtracting the offset), thereby directly accessing the business data within it without the need for data copying. Accessing data at the tail node of the linked list is similar; the tail node is located through the predecessor pointer of the head node, and then reverse pointer arithmetic is performed.
[0104] When determining if a linked list is empty, the linked list container checks if both the predecessor and successor pointers of the head node point to the head node itself (because the linked list uses circular association logic, the head node has no valid child nodes in an empty linked list, and the pointers are self-associative). Only one pointer comparison is needed to quickly obtain the result. When clearing all nodes in the linked list, the linked list container traverses all valid nodes, performing node removal operations one by one (resetting the pointer associations of each node) until the head node returns to an empty state where both its predecessor and successor pointers point to itself. This entire process only breaks the connections between nodes and does not affect the storage of the host data structure itself, ensuring that nodes can be re-inserted into the linked list later. These operations are all based on the pointer characteristics of intrusive nodes and pre-stored offsets, ensuring both operational efficiency and maintaining the integrity of the linked list and the accuracy of data access.
[0105] In some embodiments, the method further includes:
[0106] The intrusive node is directly embedded as a member variable of the host data structure, so that the intrusive node is completed synchronously with the memory allocation of the host data structure.
[0107] By embedding the intrusive nodes with the host data structure, the intrusive nodes and the host data form a compact and continuous layout in memory;
[0108] By embedding nodes directly into the host data structure in an intrusive manner, the linked list nodes and the host data form a compact and continuous memory layout. This avoids the memory access dispersion problem caused by the random scattering of nodes in different memory locations in traditional linked lists, reduces the memory page cache miss rate, improves the memory access hit rate, and optimizes memory locality.
[0109] In constructing an embedded generic linked list container system based on offset calculation, the method further includes operations to achieve memory allocation synchronization, layout optimization, and memory locality improvement through a specific embedding method of intrusive nodes: Intrusive nodes are directly embedded as member variables of the host data structure, so that the memory allocation of intrusive nodes does not need to be executed independently, but is completed synchronously with the memory allocation of the host data structure. When the host data structure is allocated memory due to business needs (whether through stack memory allocation, static memory allocation, or pre-planned heap memory allocation), the intrusive node, as its member variable, directly occupies a portion of the host data structure's memory space. There is no need to separately request additional memory for the linked list node after the host data structure's memory allocation is completed. This not only eliminates the frequent dynamic memory allocation operations when nodes and data are stored separately in traditional linked lists, but also avoids memory fragmentation problems that may be caused by independent memory allocation of nodes. Simultaneously, it ensures that the lifecycle of the intrusive node remains consistent with the host data structure, eliminating the need for additional management of node memory release processes and simplifying the overall memory management logic.
[0110] Through the embedded association between intrusive nodes and the host data structure, the two naturally form a compact and continuous layout in memory. Since the intrusive node is an internal component of the host data structure, rather than an independent external node entity, when processing the memory layout of the host data structure, the compiler will automatically adjust the arrangement order of the internal business data and the intrusive node according to the memory alignment rules of the target runtime environment (such as 4-byte or 8-byte alignment standards based on the hardware architecture). This ensures that the starting memory address of the intrusive node immediately follows the ending memory address of the business data in the host data structure, and there is no free memory gap between them due to the independent storage of nodes. This allows the host data and the intrusive node to jointly form a continuous memory block, breaking the pattern of nodes and data being scattered in discrete memory addresses in traditional linked lists.
[0111] This intrusive embedding of nodes directly into the host data structure ultimately creates a compact, contiguous memory layout between the linked list nodes and the host data. This effectively solves the problem of scattered memory access in traditional linked lists: In traditional linked lists, each node is randomly scattered across different memory locations. When a program accesses adjacent nodes, it often needs to traverse different memory pages, leading to high CPU cache miss rates, frequent memory page swapping, and severely impacting data access efficiency. In contrast, the contiguous memory layout in this method concentrates the program's access to host data and operations on linked list nodes in adjacent memory regions. Adjacent access requests are more likely to fall within the same memory page or adjacent memory pages. The CPU cache can more efficiently capture and cache this contiguous memory data, thereby significantly reducing the memory page miss rate, greatly improving the memory access hit rate, and optimizing memory locality. This optimization reduces the time the CPU waits for memory data, speeds up the response time of data read and write operations such as linked list insertion, deletion, and traversal, and provides crucial support for the stable and efficient operation of embedded generic linked list container systems in high-performance, memory-constrained scenarios.
[0112] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method.
[0113] Embodiments of this application also provide an optimization apparatus for embedded linked list containers. Figure 2 This is a schematic diagram of the structure of an optimization device for an embedded linked list container provided in an embodiment of this application, as shown below. Figure 2 As shown, it includes:
[0114] Embedding unit 21 is used to embed a linked list node as a member variable of the host data structure into the host data structure;
[0115] The calculation unit 22 is used to calculate the offset of the linked list node in the host data structure through a predefined member offset calculation macro, and store the offset in the linked list container;
[0116] Acquisition unit 23 is used to obtain the address of the host data structure based on the offset by using reverse pointer operation;
[0117] Construction unit 24 is used to construct a doubly linked list based on the predecessor and successor pointers of intrusive nodes, and maintain the integrity and exception safety of the linked list through atomic insertion and deletion operations.
[0118] Furthermore, in one possible implementation of this application embodiment, the embedding unit 21 is further configured to:
[0119] The linked list node is defined as an embedded linked list node unit type in the host data structure and is directly included in the host data structure as a structure member variable;
[0120] The host data structure and the linked list nodes are arranged consecutively in memory through a memory alignment mechanism.
[0121] Furthermore, in one possible implementation of this application embodiment, the computing unit 22 is further configured to:
[0122] The calculated offset is stored in the node offset attribute of the unsigned integer type of the linked list container; wherein, the node offset attribute is used for reverse pointer operation and host data access.
[0123] Furthermore, in one possible implementation of this application embodiment, the acquisition unit 23 is further configured to:
[0124] Using the offset as a parameter, the address of the host data structure is calculated in reverse using pointer arithmetic; wherein, the host data is directly accessed through the pointer arithmetic.
[0125] Furthermore, in one possible implementation of this application embodiment, the acquisition unit 23 is further configured to:
[0126] Establish the association between the iterator and the linked list container;
[0127] Get and store the offset of the linked list node in the host data structure within the linked list container;
[0128] The target intrusive node in the linked list is located based on the traversal mechanism of the iterator.
[0129] Based on the iterator and the offset, reverse pointer operation is performed to calculate the address of the host data structure to which the target intrusive node belongs. The iterator includes an automatic repair mode, a traditional traversal mode, and a pre-cached next node mode.
[0130] Furthermore, in one possible implementation of this application embodiment, the linked list container is used to perform at least one of the following operations: inserting a node at the head of the linked list, inserting a node at the tail of the linked list, removing the head node of the linked list, removing the tail node of the linked list, accessing the head node data of the linked list, accessing the tail node data of the linked list, determining whether the linked list is empty, and clearing all nodes in the linked list.
[0131] Furthermore, in one possible implementation of the embodiments of this application, such as Figure 3 As shown, the device further includes:
[0132] Processing unit 25 is used to directly embed the intrusive node as a member variable of the host data structure, so that the intrusive node is completed synchronously with the memory allocation of the host data structure.
[0133] Association unit 26 is used to make the intrusive node and the host data form a compact and continuous layout in memory through the embedded association between the intrusive node and the host data structure;
[0134] Processing unit 25 is also used to create a compact, contiguous memory layout between linked list nodes and host data by directly embedding intrusive nodes into the host data structure. This avoids the memory access dispersion problem caused by nodes being randomly scattered in different memory locations in traditional linked lists, reduces memory page cache miss rate, improves memory access hit rate, and optimizes memory locality.
[0135] For a description of the features in the embodiment corresponding to the optimization device for the embedded linked list container, please refer to the relevant description in the embodiment corresponding to the optimization method for the embedded linked list container, which will not be repeated here.
[0136] Embodiments of this application also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above-described embodiments of the optimized method for embedded linked list containers.
[0137] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above-described optimized method embodiments of the embedded linked list container at runtime.
[0138] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.
[0139] Embodiments of this application also provide a computer program product, which includes a computer program that, when executed by a processor, implements the steps in any of the above-described optimized methods for embedded linked list containers.
[0140] Embodiments of this application also provide another computer program product, including a non-volatile computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps in any of the above-described optimized methods for embedded linked list containers.
[0141] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0142] The above provides a detailed description of the optimization method, apparatus, electronic device, and storage medium for an embedded linked list container provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are merely for the purpose of helping to understand the method and core ideas of this application. It should be noted that those skilled in the art can make various improvements and modifications to this application without departing from its principles, and these improvements and modifications also fall within the protection scope of the claims of this application.
Claims
1. An optimization method for an embedded linked list container, characterized in that, include: The linked list node is embedded as a member variable of the host data structure; The offset of the linked list node in the host data structure is calculated using a predefined member offset calculation macro, and the offset is stored in the linked list container. The address of the host data structure is obtained using reverse pointer arithmetic based on the offset. A doubly linked list is constructed based on the predecessor and successor pointers of intrusive nodes, and the integrity and exception safety of the linked list are maintained through atomic insertion and deletion operations.
2. The method according to claim 1, characterized in that, The step of embedding the linked list node as a member variable of the host data structure includes: The linked list node is defined as an embedded linked list node unit type in the host data structure and is directly included in the host data structure as a structure member variable; The host data structure and the linked list nodes are arranged consecutively in memory through a memory alignment mechanism.
3. The method according to claim 1, characterized in that, The step of calculating the offset of the linked list node in the host data structure using a predefined member offset calculation macro and storing the offset in the linked list container includes: The calculated offset is stored in the node offset attribute of the unsigned integer type of the linked list container; wherein, the node offset attribute is used for reverse pointer operation and host data access.
4. The method according to claim 1, characterized in that, The step of obtaining the address of the host data structure based on the offset using reverse pointer arithmetic includes: Using the offset as a parameter, the address of the host data structure is calculated in reverse using pointer arithmetic; wherein, the host data is directly accessed through the pointer arithmetic.
5. The method according to claim 1, characterized in that, The step of obtaining the address of the host data structure based on the offset using reverse pointer arithmetic further includes: Establish the association between the iterator and the linked list container; Get and store the offset of the linked list node in the host data structure within the linked list container; The target intrusive node in the linked list is located based on the traversal mechanism of the iterator. Based on the iterator and the offset, reverse pointer operation is performed to calculate the address of the host data structure to which the target intrusive node belongs. The iterator includes an automatic repair mode, a traditional traversal mode, and a pre-cached next node mode.
6. The method according to claim 1, characterized in that, The linked list container is used to perform at least one of the following operations: inserting a node at the head of the linked list, inserting a node at the tail of the linked list, removing the head node of the linked list, removing the tail node of the linked list, accessing the data of the head node of the linked list, accessing the data of the tail node of the linked list, determining whether the linked list is empty, and clearing all nodes in the linked list.
7. The method according to claim 1, characterized in that, The method further includes: The intrusive node is directly embedded as a member variable of the host data structure, so that the intrusive node is completed synchronously with the memory allocation of the host data structure. By embedding the intrusive nodes with the host data structure, the intrusive nodes and the host data form a compact and continuous layout in memory; By embedding nodes directly into the host data structure in an intrusive manner, the linked list nodes and the host data form a compact and continuous memory layout. This avoids the memory access dispersion problem caused by the random scattering of nodes in different memory locations in traditional linked lists, reduces the memory page cache miss rate, improves the memory access hit rate, and optimizes memory locality.
8. An optimization device for an embedded linked list container, characterized in that, include: An embedding unit is used to embed a linked list node as a member variable of the host data structure into the host data structure; The calculation unit is used to calculate the offset of the linked list node in the host data structure through a predefined member offset calculation macro, and store the offset in the linked list container; The acquisition unit is used to obtain the address of the host data structure based on the offset by using reverse pointer arithmetic; The building unit is used to construct a doubly linked list based on the predecessor and successor pointers of intrusive nodes, and maintains the integrity and exception safety of the linked list through atomic insertion and deletion operations.
9. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor, configured to implement the steps of the optimized method for the embedded linked list container as described in any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps of the optimized method for the embedded linked list container as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Link table management method and device, equipment and medium
CN116401415A
Cited By
Hybrid storage and paging display method and system for historical record data
CN121541834A
A hybrid storage and paging display method and system for historical data
CN121541834B