Cache replacement method and device, electronic equipment and storage medium

By constructing an N-layer data structure and marking replacement identifiers according to the priority of cache line data, combined with a pseudo-least recently used algorithm, the problem of decreased hit rate in tree-structured cache replacement strategy is solved, thereby improving cache hit rate and data retention efficiency.

CN121833559AActive Publication Date: 2026-04-10SHANGHAI BIREN TECH CO LTD
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202511862267.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-11
Publication Date
2026-04-10
Estimated Expiration
2045-12-11

AI Technical Summary

Technical Problem

Existing tree-based cache replacement strategies suffer from a decrease in hit rate and cannot meet current control requirements.

Method used

A cache replacement method is adopted, which constructs an N-layer data structure, marks the nodes with replacement identifiers according to the priority of the cache line data, marks the replacement identifiers layer by layer, and determines the target cache line by combining a pseudo least recently used algorithm, giving priority to the replacement strategy of high-priority data.

Benefits of technology

This increases the probability of cache hits, reduces the chance of useful data being mistakenly replaced, and improves cache performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121833559A_ABST
    Figure CN121833559A_ABST
Patent Text Reader

Abstract

The invention provides a cache replacement method and device, electronic equipment and a storage medium, and relates to the technical field of artificial intelligence chips, and the cache replacement method comprises the following steps: obtaining a first data structure; labeling replacement identifiers for nodes in the first data structure according to the priority of data cached in each cache line in the cache to obtain a labeled first data structure; and based on the labeled first data structure, determining a target cache line used for being replaced in the cache. The marked replacement identifier is determined according to the priority of the data cached in each cache line in the cache, so that the marked first data structure can represent the priority of the data cached in each cache line, and when the marked first data structure is utilized to determine the target cache line for replacement in the cache, the target cache line can be replaced. In this way, the priority of the data cached in each cache line can be considered, so that the situation that useful data are replaced by mistake is improved, and the probability of cache hit is improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of artificial intelligence chips, and in particular to a cache replacement method and device, electronic equipment and a storage medium. BACKGROUND

[0002] In a chip architecture, the hit rate of a cache affects the performance of an actual program, wherein cache replacement is a rule for deciding to replace old data in the cache when the cache space is full, and it is strongly related to the cache hit rate. A suitable cache replacement algorithm can significantly improve the hit rate, and an unreasonable algorithm can cause the hit rate to decrease.

[0003] The core logic is to reduce the case of "useful data being mistakenly replaced" by fitting the principle of locality of program access, thereby affecting the probability of cache hits. Different replacement algorithms have a significant difference in the impact on the hit rate.

[0004] In a related technical solution, a tree-shaped cache replacement strategy is used for replacement, wherein the tree-shaped cache replacement strategy applies a pseudo least recently used algorithm, uses a tree-shaped road state to record access history, and selects a cache line that has not been accessed for the longest time as the cache line to be replaced when replacing.

[0005] However, the tree-shaped cache replacement strategy still has the problem of decreased hit rate, which cannot meet the control needs at the present stage. SUMMARY

[0006] The present application provides a cache replacement method, device, electronic equipment and storage medium to solve the problem that the tree-shaped cache replacement strategy in the related technical solution still has the problem of decreased hit rate, which cannot meet the control needs at the present stage.

[0007] The present application provides a cache replacement method, comprising the following steps: Obtain a first data structure, wherein the first data structure is an N-layer data structure corresponding to a cache, the M-1 layer data structure in the first data structure includes at least one first node, the M layer data structure includes a plurality of second nodes, each first node is associated with two second nodes, 2≤M According to the priority of the data cached in each cache line in the cache, mark a replacement identifier for the nodes in the first data structure, to obtain the marked first data structure. Based on the marked first data structure, determine a target cache line in the cache to be replaced.

[0008] The application provides a cache replacement method, wherein a replacement identifier is marked for each node in the first data structure according to the priority of the data cached in each cache line in the cache, and a marked first data structure is obtained, comprising: The replacement identifier of each third node is determined according to the priority of the data cached in each cache line in the cache; The replacement identifier of each node in the first data structure is marked layer by layer based on the replacement identifier of each third node, and the marking of the first data structure is ended until the node identifier in the first layer data structure, so as to obtain the marked first data structure.

[0009] The application provides a cache replacement method, wherein the replacement identifier comprises a first identifier or a second identifier, the first identifier is used to indicate that it can be replaced, the second identifier is used to indicate that it cannot be replaced, the nodes contained in the 0th layer data structure in the first data structure comprise two fourth nodes, the nodes contained in the (O-1)th layer data structure comprise a fifth node, the two fourth nodes are associated with the fifth node, and 2≤O<N; Based on the fact that at least one first identifier exists in the replacement identifiers corresponding to the two fourth nodes, it is determined that the replacement identifier corresponding to the fifth node is the first identifier; Based on the fact that the replacement identifiers corresponding to the two fourth nodes are both the second identifier, it is determined that the replacement identifier corresponding to the fifth node is the second identifier.

[0010] The application provides a cache replacement method, wherein the nodes contained in the (N-1)th layer data structure comprise a sixth node, the plurality of third nodes comprised in the Nth layer data structure comprise a first sub-node and a second sub-node, and the first sub-node and the second sub-node are associated with the sixth node; The target cache line to be replaced in the cache is determined based on the marked first data structure, comprising: The selection state bit and the replacement identifier corresponding to each node in the marked first data structure are obtained, and the selection state bit is a state bit determined according to a pseudo least recently used algorithm; Based on the fact that the selection state bit corresponding to the sixth node is a first state bit and the replacement identifier corresponding to the first sub-node is a first identifier, the cache line corresponding to the first sub-node is selected as the target cache line to be replaced; Based on the fact that the selection state bit corresponding to the sixth node is a first state bit and the replacement identifier corresponding to the first sub-node is a second identifier, the cache line corresponding to the second sub-node is selected as the target cache line to be replaced; The first state bit is used to indicate that the cache line corresponding to the first sub-node is selected as the target cache line to be replaced according to a pseudo least recently used algorithm, the first identifier is used to indicate that the cache line can be replaced, and the second identifier is used to indicate that the cache line cannot be replaced.

[0011] The cache replacement method further comprises: receiving a cache request of data to be written; in a case where the replacement identifier of the node in the first layer of the first data structure is the second identifier, canceling responding to the cache request; The second identifier is used to indicate that the cache line cannot be replaced.

[0012] The first data structure is a binary tree constructed according to a pseudo least recently used algorithm.

[0013] The cache replacement method further comprises: receiving a cache request of data to be written; in response to the cache request, releasing the data cached in the target cache line, and caching the data to be written to the target cache line.

[0014] The cache replacement method further comprises: The cache replacement method further comprises: The first data structure is an N-layer data structure corresponding to the cache, the first data structure comprises at least one first node, the Mth layer of the data structure comprises a plurality of second nodes, each first node is associated with two second nodes, 2≤M The first data structure is an N-layer data structure corresponding to the cache, the first data structure comprises at least one first node, the Mth layer of the data structure comprises a plurality of second nodes, each first node is associated with two second nodes, 2≤M

[0015] The application further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the cache replacement method according to any one of the above.

[0016] The application further provides a non-transitory computer readable storage medium, which stores a computer program, wherein the computer program is executable on a processor to implement the cache replacement method according to any one of the above.

[0017] The application further provides a computer program product comprising a computer program which, when executed by a processor, implements the cache replacement method according to any one of the above.

[0018] The cache replacement method, device, electronic equipment and storage medium provided by the application, in the case of obtaining a first data structure, the priority of the data cached in each cache line in the cache is used to mark the nodes in the first data structure, and the first data structure after marking is obtained, wherein the replacement identifier is determined according to the priority of the data cached in each cache line in the cache, so that the priority of the data cached in each cache line can be represented in the first data structure after marking, and when the target cache line to be replaced in the cache is determined by using the first data structure after marking, the priority of the data cached in each cache line can be considered, so as to improve the situation that useful data is mistakenly replaced, and the probability of cache hit is improved. BRIEF DESCRIPTION OF DRAWINGS

[0019] In order to more clearly illustrate the technical solutions in the application or the prior art, the drawings needed in the embodiments or the prior art description will be briefly introduced below. Obviously, the drawings in the following description are some embodiments of the application, and other drawings can be obtained by those skilled in the art without creative labor.

[0020] Figure 1 is a flowchart of the cache replacement method provided by the application; Figure 2 is a flowchart of the cache replacement method provided by the application; Figure 3 is a flowchart of the cache replacement method provided by the application; Figure 4 is a schematic diagram of the first data structure after marking provided by the application; Figure 5 is a schematic block diagram of the cache replacement device provided by the application; Figure 6 is a structural schematic diagram of the electronic equipment provided by the application.

[0021] Reference signs: 501, acquisition module; 502, marking module; 503, determination module; 610, processor; 620, communication interface; 630, memory; 640, communication bus. DETAILED DESCRIPTION

[0022] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.

[0023] It should be noted that in the description of this invention, the terms "comprising," "including," or any other variations thereof are intended to cover a 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. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element. The terms "upper," "lower," etc., indicating orientation or positional relationships based on the orientation or positional relationships shown in the accompanying drawings, are used only for the convenience of describing the invention and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore should not be construed as a limitation of the invention. Unless otherwise expressly specified and limited, the terms "installed," "connected," and "linked" should be interpreted broadly, for example, as a fixed connection, a detachable connection, or an integral connection; a mechanical connection or an electrical connection; a direct connection or an indirect connection through an intermediate medium; or a connection within two elements. Those skilled in the art can understand the specific meaning of the above terms in this invention according to the specific circumstances.

[0024] The terms "first," "second," etc., used in this invention are used to distinguish similar objects, not to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of the invention can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class, without limiting the number of objects; for example, a first object can be one or more. Furthermore, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects are in an "or" relationship.

[0025] The following is combined with Figures 1 to 6 The cache replacement method, apparatus, electronic device, and storage medium provided by this invention aim to improve the problem that the tree-structured cache replacement strategy in related technical solutions still suffers from a decrease in hit rate and cannot meet the current control needs.

[0026] Figure 1 This is a flowchart illustrating the cache replacement method provided by the present invention, as follows: Figure 1 As shown, it includes: Step 101: Obtain the first data structure.

[0027] The first data structure is an N-level data structure corresponding to the cache. The data structure of the (M-1)th level in the first data structure includes at least one first node, the data structure of the Mth level includes multiple second nodes, each first node is associated with two second nodes, 2≤M<N, and the data structure of the Nth level includes multiple third nodes used to represent cache lines.

[0028] In some embodiments, the cache organization method can be either fully associative cache or set-associative cache.

[0029] In fully associative caches, any block in main memory can be mapped to any location in the cache, providing greater flexibility and a lower probability of block conflicts. However, the design and implementation are more complex, making it suitable for small-capacity caches.

[0030] For set-associative caches, the cache is divided into multiple sets. Blocks within each set can be flexibly mapped to specific blocks in main memory, while direct mapping is used between sets.

[0031] In embodiments of the present invention, the proposed cache replacement method is applicable to each group in fully associative caches and set associative caches.

[0032] In some embodiments, when N is 3, the first data structure is a three-layer data structure, wherein the first layer data structure includes a first node, the second layer data structure includes two second nodes, and the third layer data structure includes four third nodes, wherein every two third nodes are connected to the same second node, and every two second nodes are connected to the same first node.

[0033] In some embodiments, when N is 5, the first data structure is a five-layer data structure, wherein the first layer data structure includes one node, the second layer data structure includes two nodes, the third layer data structure includes four nodes, the fourth layer data structure includes eight nodes, and the fifth layer data structure includes 16 nodes.

[0034] Specifically, the 16 nodes in the 5th layer data structure are child nodes, which are respectively connected to the 8 parent nodes in the 4th layer data structure. Similarly, the 8 nodes in the 4th layer data structure are child nodes, which are respectively connected to the 4 parent nodes in the 3rd layer data structure. The 4 nodes in the 3rd layer data structure are child nodes, which are respectively connected to the 2 parent nodes in the 2nd layer data structure. The 2 nodes in the 2nd layer data structure are child nodes, which are connected to the 1 parent node in the 1st layer data structure, thus forming the first data structure.

[0035] Step 102: Based on the priority of the data cached in each cache line in the cache, label the nodes in the first data structure with replacement identifiers to obtain the labeled first data structure.

[0036] In some embodiments, the priority of the data cached in each cache line can be determined based on the data's usage scenario.

[0037] Specifically, the priority of data cached in each cache line is different for different use cases. Specifically, data with high read and write frequency has higher priority, while data with fewer uses or low latency dependence has lower priority. Based on this, a priority can be assigned to the data cached in each cache line.

[0038] For example, data that is frequently read and written has the highest priority, while data that is used less often or is less dependent on latency has the highest priority. The first priority has a higher priority than the second priority.

[0039] Specifically, for two adjacent third nodes, if the priority of the data cached in the cache line corresponding to one third node is higher than the priority of the data cached in the cache line corresponding to the other third node, then the replacement identifier corresponding to the third node with higher priority is the first identifier, and the replacement identifier corresponding to the third node with lower priority is the second identifier.

[0040] In some embodiments, a priority evaluation system is pre-built, and then, when the data cached in each cache line is obtained, the priority corresponding to the data cached in each cache line is determined.

[0041] Step 103: Based on the first data structure after annotation, determine the target cache line in the cache to be replaced.

[0042] In this embodiment, after obtaining the first data structure, the nodes in the first data structure are labeled using the priority of the data cached in each cache line in the cache, resulting in a labeled first data structure. The replacement identifier of the label is determined according to the priority of the data cached in each cache line in the cache. Therefore, the labeled first data structure can represent the priority of the data cached in each cache line. Thus, when determining the target cache line to be replaced in the cache using the labeled first data structure, the priority of the data cached in each cache line can be taken into account. That is, the target cache line, as the selected cache line, is the cache line that is not expected to be retained, while the unselected cache line is the cache line that is expected to be retained. This improves the situation of "useful data being mistakenly replaced" and increases the probability of cache hit.

[0043] In some embodiments, the cache replacement method provided by the present invention, Figure 2 This is a flowchart illustrating the process provided by the present invention, which uses the priority of the data cached in each cache line in the cache to label and replace the node identifier in the first data structure, thereby obtaining the labeled first data structure. Figure 2 As shown, based on the priority of the data cached in each cache line in the cache, the nodes in the first data structure are labeled with replacement identifiers, resulting in the labeled first data structure, which includes: Step 201: Determine the replacement identifier for each third node based on the priority of the data cached in each cache line in the cache. Step 202: Based on the replacement identifier of each third node, identify the replacement identifier of each node in the first data structure layer by layer until the node identification in the first layer data structure ends, so as to obtain the labeled first data structure.

[0044] In this embodiment, the replacement identifiers of multiple third nodes included in the Nth layer data structure can be used to infer the replacement identifiers of multiple nodes included in each layer data structure, thereby marking each node in the first data structure with a replacement identifier.

[0045] In this process, the replacement identifier of the node in the first-level data structure can reflect the replacement identifier of the third node at the bottom level. This allows the replacement identifier of the node in each level of the data structure to be referenced when determining the target cache line to be replaced using the annotated first data structure. Since the annotated replacement identifier is determined based on the priority of the data cached in each cache line, the annotated first data structure can represent the priority of the data cached in each cache line. Therefore, when determining the target cache line to be replaced using the annotated first data structure, the priority of the data cached in each cache line can be taken into account, thereby improving the situation of "useful data being mistakenly replaced" and increasing the probability of cache hit.

[0046] For example, when N is 3, that is, the first data structure is a three-level data structure, wherein the first level data structure includes a first node, the second level data structure includes two second nodes, and the third level data structure includes four third nodes, wherein every two third nodes are connected to the same second node, and every two second nodes are connected to the same first node.

[0047] For ease of description, the first node is represented by NO101, the two second nodes in the second-level data structure are represented by NO201 and NO202 respectively, and the four third nodes in the third-level data structure are represented by NO301, NO302, NO303 and NO304 respectively. Among them, NO301 and NO302 are associated with NO201, NO303 and NO304 are associated with NO202, and NO201 and NO202 are associated with NO101. The replacement identifier of NO201 is determined based on the replacement identifier of NO301 and NO302, the replacement identifier of NO202 is determined based on the replacement identifier of NO303 and NO304, and the replacement identifier of NO101 is determined based on the replacement identifier of NO201 and NO202, thus realizing layer-by-layer identification.

[0048] For example, when N is 5, the first data structure is a five-layer data structure, wherein the first layer data structure includes one node, the second layer data structure includes two nodes, the third layer data structure includes four nodes, the fourth layer data structure includes eight nodes, and the fifth layer data structure includes 16 nodes.

[0049] For ease of description, the nodes in the first-level data structure are represented by NO101, the nodes in the second-level data structure are represented by NO201 and NO202, the nodes in the third-level data structure are represented by NO301, NO302, NO303 and NO304, the nodes in the fourth-level data structure are represented by NO401, NO402, NO403, NO404, NO405, NO406, NO407 and NO408, and the nodes in the fifth-level data structure are represented by NO501, NO502, NO503, NO504, NO505, NO506, NO507, NO508, NO509, NO510, NO511, NO512, NO513, NO514, NO515 and NO516.

[0050] Among them, NO501 and NO502 are associated with NO401; NO503 and NO504 are associated with NO402; NO505 and NO506 are associated with NO403; NO507 and NO508 are associated with NO404; NO509 and NO510 are associated with NO405; NO511 and NO512 are associated with NO406; NO513 and NO514 are associated with NO407; NO515 and NO516 are associated with NO408; N O401 and NO402 are associated with NO301; NO403 and NO404 are associated with NO302; NO405 and NO406 are associated with NO303; NO407 and NO408 are associated with NO304; NO301 and NO302 are associated with NO201; NO303 and NO304 are associated with NO202; and NO201 and NO202 are associated with NO101. Therefore, NO401 ​​is determined based on the substitution identifiers of NO501 and NO502. The replacement identifiers are determined by the following steps: NO503 and NO504 are used to determine the replacement identifier for NO402; NO505 and NO506 are used to determine the replacement identifier for NO403; NO507 and NO508 are used to determine the replacement identifier for NO404; NO509 and NO510 are used to determine the replacement identifier for NO405; NO511 and NO512 are used to determine the replacement identifier for NO406; NO513 and NO514 are used to determine the replacement identifier for NO407; NO515 and NO516 are used to determine the replacement identifier for NO408, and so on. The replacement identifier for NO201 is determined by the replacement identifiers for NO301 and NO302; the replacement identifier for NO202 is determined by the replacement identifiers for NO303 and NO304; and the replacement identifier for NO101 is determined by the replacement identifiers for NO201 and NO202, thus achieving layer-by-layer identification.

[0051] In some embodiments, the replacement identifier includes a first identifier or a second identifier, the first identifier being used to indicate that it can be used for replacement, and the second identifier being used to indicate that it cannot be used for replacement. The nodes contained in the data structure of the O-th level in the first data structure include two fourth nodes, and the nodes contained in the data structure of the O-1-th level include a fifth node. The two fourth nodes are associated with the fifth node, and 2 ≤ O < N. Based on the fact that at least one first identifier exists among the replacement identifiers corresponding to the two fourth nodes, the replacement identifier corresponding to the fifth node is determined to be the first identifier; Since the replacement identifiers corresponding to the two fourth nodes are both the second identifier, the replacement identifier corresponding to the fifth node is determined to be the second identifier.

[0052] In this embodiment, a detailed explanation of determining the replacement identifier for each node is provided. By limiting the replacement identifiers corresponding to the two fourth nodes to at least one first identifier, the replacement identifier corresponding to the fifth node is determined to be the first identifier, so that the fifth node reflects the replacement identifier of the fourth node, and so on. This allows the replacement identifiers of nodes in each layer of the data structure to be referenced when determining the target cache line to be replaced in the cache using the annotated first data structure. Since the annotated replacement identifier is determined based on the priority of the data cached in each cache line in the cache, the annotated first data structure can represent the priority of the data cached in each cache line. Therefore, when determining the target cache line to be replaced in the cache using the annotated first data structure, the priority of the data cached in each cache line can be taken into account, thereby improving the situation of "useful data being mistakenly replaced" and increasing the probability of cache hit.

[0053] For example, if the first identifier is "1" and the second identifier is "0", then if both replacement identifiers corresponding to the two fourth nodes are "1" or one of them is "1", the replacement identifier corresponding to the fifth node is determined to be "1". Correspondingly, if both replacement identifiers corresponding to the two fourth nodes are "0", the replacement identifier corresponding to the fifth node is determined to be "0".

[0054] For example, when N is 3, that is, the first data structure is a three-level data structure, wherein the first level data structure includes a first node, the second level data structure includes two second nodes, and the third level data structure includes four third nodes, wherein every two third nodes are connected to the same second node, and every two second nodes are connected to the same first node.

[0055] For ease of description, the first node is represented by NO101, the two second nodes in the second-level data structure are represented by NO201 and NO202, and the four third nodes in the third-level data structure are represented by NO301, NO302, NO303, and NO304. NO301 and NO302 are associated with NO201, NO303 and NO304 are associated with NO202, and NO201 and NO202 are associated with NO101. If the replacement identifier of NO301 is "1", the replacement identifier of NO302 is "0", the replacement identifier of NO303 is "1", and the replacement identifier of NO304 is "0", then the replacement identifier of NO201 is determined to be "1" based on the replacement identifiers of NO301 and NO302, the replacement identifier of NO202 is determined to be "1" based on the replacement identifiers of NO303 and NO304, and the replacement identifier of NO101 is determined to be "1" based on the replacement identifiers of NO201 and NO202.

[0056] In some embodiments, the nodes included in the data structure of the (N-1)th layer include a sixth node, and the multiple third nodes included in the data structure of the Nth layer include a first child node and a second child node, and the first child node and the second child node are associated with the sixth node; Figure 3 This is a flowchart illustrating the process of determining the target cache line to be replaced in the cache based on the first labeled data structure provided by this invention, as shown below. Figure 3 As shown, based on the first labeled data structure, the target cache line in the cache to be replaced is determined, including: Step 301: Obtain the selection status bit and replacement identifier corresponding to each node in the first labeled data structure. The selection status bit is determined according to the pseudo least recently used algorithm. Step 302: Based on the selection status bit corresponding to the sixth node being the first status bit and the replacement identifier corresponding to the first child node being the first identifier, select the cache line corresponding to the first child node as the target cache line to be replaced; Step 303: Based on the selection status bit corresponding to the sixth node being the first status bit and the replacement identifier corresponding to the first child node being the second identifier, select the cache line corresponding to the second child node as the target cache line to be replaced; The first status bit is used to indicate that the cache line corresponding to the first child node is selected as the target cache line to be replaced according to the pseudo least recently used algorithm. The first flag is used to indicate that it can be replaced and the second flag is used to indicate that it cannot be replaced.

[0057] In this embodiment, when selecting the target cache line to be replaced in the cache, the selection status bit and replacement identifier corresponding to each node are obtained. The selection status bit is a status bit determined according to the pseudo least recently used algorithm. Therefore, the least recently used cache line can be selected by using the selection status bit. Combined with the replacement identifier corresponding to the first child node, the least recently used cache line with a relatively low priority can be filtered out as the target cache line. This improves the situation of "useful data being mistakenly replaced" and increases the probability of cache hit.

[0058] For example, when the selected status bit is the first status bit, the selected status bit is expressed as "1". If the replacement flag corresponding to the first child node is "1", then the cache line corresponding to the first child node is selected as the target cache line to be replaced; if the replacement flag corresponding to the first child node is "0", then the cache line corresponding to the second child node is selected as the target cache line to be replaced.

[0059] For example, the first data structure is the data structure corresponding to a 4-way associative cache. In the first data structure, the nodes included in the first layer data structure are represented by the first node NO101, the nodes included in the second layer data structure are represented by NO201 and NO202, and the nodes included in the third layer data structure are represented by NO301, NO302, NO303 and NO304. The replacement identifier of NO301 is valid_3, the replacement identifier of NO302 is valid_2, the replacement identifier of NO303 is valid_1, the replacement identifier of NO304 is valid_0, the replacement identifier of NO201 is valid_5, the replacement identifier of NO202 is valid_4, and the replacement identifier of NO101 is valid_6.

[0060] like Figure 4 As shown, when the selection status bit PLRU_2==1 and valid_3 is "1" in node NO201, one side of NO301 is selected.

[0061] Specifically, if the replacement identifier valid_3 of NO301 is "1" and the replacement identifier valid_2 of NO302 is "0", then NO301 is selected based on the selection status bit of node NO201.

[0062] If the replacement flag valid_3 of NO301 is "1" and the replacement flag valid_2 of NO302 is also "1", then NO301 or NO302 is selected based on the selection status bit of node NO201.

[0063] In some embodiments, the cache replacement method further includes: Receive cache requests for data to be written; If the replacement identifier of a node in the first-level data structure of the first data structure is the second identifier, cancel the response cache request; The second identifier is used to indicate that it cannot be used for replacement.

[0064] In this embodiment, since the replacement identifier of the node in each layer of the first data structure is obtained by reverse deduction from the bottom layer node, if the replacement identifier of the node in the first layer data structure is the second identifier, it is considered that there is no cache line that can be replaced in the current cache. The cache request is canceled to avoid data overflow and thus ensure the reliability of data processing.

[0065] In some embodiments, the first data structure is a binary tree constructed according to a pseudo least recently used algorithm.

[0066] The first data structure is a binary tree constructed according to a pseudo least recently used algorithm. This binary tree is constructed according to a pseudo least recently used algorithm, which is the algorithm used in related technical solutions. Therefore, it can be seen that the technical solution of the present invention is compatible with the technical solutions used in related technical solutions.

[0067] Furthermore, the implementation of the technical solution of the present invention results in a small increase in hardware timing and area, and does not require hardware replacement. Obviously, it improves the probability of cache hits without increasing hardware costs.

[0068] In some embodiments, the cache replacement method further includes: Receive cache requests for data to be written; Respond to cache requests, release the data cached in the target cache line, and cache the data to be written to the target cache line.

[0069] In this embodiment, cache replacement is achieved by releasing the data cached in the target cache line in order to respond to the cache request, thereby caching the data to be written to the target cache line.

[0070] In some embodiments, after caching the data to be written to the target cache line, the replacement identifier of each node in the first data structure is updated according to the priority of the data to be written, so as to obtain the updated first data structure.

[0071] It should be noted that the cache replacement device provided by the present invention can execute the cache replacement method of any of the above embodiments during specific operation, which will not be elaborated in this embodiment.

[0072] like Figure 5 As shown, the cache replacement device provided by the present invention includes: The acquisition module 501 is used to acquire the first data structure, which is an N-level data structure corresponding to the cache. The data structure of the (M-1)th level in the first data structure includes at least one first node, the data structure of the Mth level includes multiple second nodes, each first node is associated with two second nodes, 2≤M<N, and the data structure of the Nth level includes multiple third nodes for representing cache lines. The annotation module 502 is used to annotate and replace the node identifier in the first data structure according to the priority of the data cached in each cache line in the cache, so as to obtain the annotated first data structure. The determination module 503 is used to determine the target cache line in the cache to be replaced based on the first labeled data structure.

[0073] Figure 6 This is a schematic diagram of the structure of the electronic device provided by the present invention, such as... Figure 6As shown, the electronic device may include: a processor 610, a communication interface 620, a memory 630, and a communication bus 640, wherein the processor 610, the communication interface 620, and the memory 630 communicate with each other through the communication bus 640. The processor 610 can call logical instructions in the memory 630 to execute a cache replacement method, which includes: obtaining a first data structure, the first data structure being an N-level data structure corresponding to the cache, wherein the (M-1)th level data structure of the first data structure includes at least one first node, the Mth level data structure includes multiple second nodes, each first node is associated with two second nodes, 2≤M<N, and the Nth level data structure includes multiple third nodes for representing cache lines; marking the nodes in the first data structure with replacement identifiers according to the priority of the data cached in each cache line in the cache, to obtain a marked first data structure; and determining the target cache line in the cache to be replaced based on the marked first data structure.

[0074] Furthermore, the logical instructions in the aforementioned memory 630 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0075] On the other hand, the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions, and when the program instructions are executed by a computer, the computer is able to execute the cache replacement method provided in the above embodiments. The method includes: obtaining a first data structure, which is an N-level data structure corresponding to a cache. The data structure of the (M-1)th level in the first data structure includes at least one first node, and the data structure of the Mth level includes multiple second nodes. Each first node is associated with two second nodes, 2≤M<N. The data structure of the Nth level includes multiple third nodes for representing cache lines; marking the nodes in the first data structure with replacement identifiers according to the priority of the data cached in each cache line in the cache, thereby obtaining a marked first data structure; and determining the target cache line in the cache to be replaced based on the marked first data structure.

[0076] In another aspect, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the cache replacement method provided in the above embodiments. The method includes: obtaining a first data structure, the first data structure being an N-level data structure corresponding to a cache, wherein the (M-1)th level data structure of the first data structure includes at least one first node, the Mth level data structure includes multiple second nodes, each first node is associated with two second nodes, 2≤M<N, and the Nth level data structure includes multiple third nodes for representing cache lines; marking the nodes in the first data structure with replacement identifiers according to the priority of the data cached in each cache line in the cache, thereby obtaining a marked first data structure; and determining the target cache line in the cache to be replaced based on the marked first data structure.

[0077] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0078] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., including several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods of various embodiments or some parts of embodiments.

[0079] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A cache replacement method, characterized in that, include: Obtain a first data structure, which is an N-level data structure corresponding to the cache. The data structure of the (M-1)th level in the first data structure includes at least one first node, and the data structure of the Mth level includes multiple second nodes. Each first node is associated with two second nodes, and 2≤M<N. The data structure of the Nth level includes multiple third nodes for representing cache lines. The first data structure after annotation is obtained by using the priority of the cached data in each cache line in the cache as the node annotation replacement identifier in the first data structure; Based on the first data structure after annotation, the target cache line in the cache to be replaced is determined.

2. The cache replacement method according to claim 1, characterized in that, The step of assigning replacement identifiers to nodes in the first data structure based on the priority of the data cached in each cache line of the cache to obtain the labeled first data structure includes: The replacement identifier of each third node is determined according to the priority of the data cached in each cache line in the cache. Based on the replacement identifier of each of the third nodes, the replacement identifier of each node in the first data structure is identified layer by layer until the node identification in the first layer of the data structure ends, so as to obtain the labeled first data structure.

3. The cache replacement method according to claim 2, characterized in that, The replacement identifier includes a first identifier or a second identifier. The first identifier is used to indicate that it can be used for replacement, and the second identifier is used to indicate that it cannot be used for replacement. The nodes contained in the data structure of the O-th level in the first data structure include two fourth nodes, and the nodes contained in the data structure of the O-1 level include a fifth node. The two fourth nodes are associated with the fifth node, and 2 ≤ O < N. Based on the fact that at least one of the first identifiers exists among the replacement identifiers corresponding to the two fourth nodes, the replacement identifier corresponding to the fifth node is determined to be the first identifier; Since the replacement identifiers corresponding to the two fourth nodes are both the second identifier, the replacement identifier corresponding to the fifth node is determined to be the second identifier.

4. The cache replacement method according to claim 1, characterized in that, The data structure of the N-1th layer includes a sixth node, and the multiple third nodes of the data structure of the Nth layer include a first child node and a second child node, and the first child node and the second child node are associated with the sixth node; The step of determining the target cache line to be replaced in the cache based on the labeled first data structure includes: Obtain the selection status bit and replacement identifier corresponding to each node in the first data structure after annotation. The selection status bit is a status bit determined according to the pseudo least recent use algorithm. Based on the fact that the selection status bit corresponding to the sixth node is the first status bit and the replacement identifier corresponding to the first child node is the first identifier, the cache line corresponding to the first child node is selected as the target cache line to be replaced; Based on the fact that the selection status bit corresponding to the sixth node is the first status bit and the replacement identifier corresponding to the first child node is the second identifier, the cache line corresponding to the second child node is selected as the target cache line to be replaced; Wherein, the first status bit is used to indicate that the cache line corresponding to the first child node is selected as the target cache line to be replaced according to the pseudo least recently used algorithm, the first identifier is used to indicate that it can be replaced, and the second identifier is used to indicate that it cannot be replaced.

5. The cache replacement method according to any one of claims 1 to 4, characterized in that, The cache replacement method further includes: Receive cache requests for data to be written; If the replacement identifier of a node in the first layer of the data structure in the first data structure is the second identifier, then the response to the cache request is cancelled. The second identifier is used to indicate that it cannot be used for replacement.

6. The cache replacement method according to any one of claims 1 to 4, characterized in that, The first data structure is a binary tree constructed based on a pseudo least recently used algorithm.

7. The cache replacement method according to any one of claims 1 to 4, characterized in that, The cache replacement method further includes: Receive cache requests for data to be written; In response to the cache request, release the data cached in the target cache line and cache the data to be written to the target cache line.

8. A cache replacement device, characterized in that, include: The acquisition module is used to acquire a first data structure, which is an N-layer data structure corresponding to the cache. The data structure of the (M-1)th layer in the first data structure includes at least one first node, and the data structure of the Mth layer includes multiple second nodes. Each first node is associated with two second nodes, and 2≤M<N. The data structure of the Nth layer includes multiple third nodes for representing cache lines. The annotation module is used to annotate and replace the nodes in the first data structure according to the priority of the data cached in each cache line in the cache, so as to obtain the annotated first data structure. The determination module is used to determine the target cache line in the cache to be replaced based on the first labeled data structure.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the cache replacement method as described in any one of claims 1 to 7.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the cache replacement method as described in any one of claims 1 to 7.

Citation Information

Patent Citations

  • Data replacement method and device, electronic equipment and storage medium

    CN114610656A

  • Cache replacement strategy management method and device, equipment, medium and program product

    CN116107926A

  • Cache line replacement method and device

    CN117349198A

  • Cache replacement method and device, electronic equipment and readable storage medium

    CN118550853A

  • Cache line replacement method and device, chip and electronic equipment

    CN120803979A