{"id":15912,"date":"2026-04-26T00:38:13","date_gmt":"2026-04-25T16:38:13","guid":{"rendered":"https:\/\/top.duoku.icu\/libs\/15912.html"},"modified":"2026-04-26T00:38:13","modified_gmt":"2026-04-25T16:38:13","slug":"%e5%86%85%e5%ad%98%e6%9e%b6%e6%9e%84%e8%af%a6%e8%a7%a3%ef%bc%9a%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.srclibs.com\/index.php\/2026\/04\/26\/%e5%86%85%e5%ad%98%e6%9e%b6%e6%9e%84%e8%af%a6%e8%a7%a3%ef%bc%9a%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e4%bc%98%e5%8c%96\/","title":{"rendered":"\u5185\u5b58\u67b6\u6784\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u4f18\u5316"},"content":{"rendered":"<div style=\"text-align:center;margin:30px 0;\"><img decoding=\"async\" src=\"https:\/\/top.duoku.icu\/wp-content\/uploads\/2026\/04\/image-83.png\" alt=\"\u5185\u5b58\u67b6\u6784\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u4f18\u5316\" style=\"max-width:100%;height:auto;border-radius:12px;box-shadow:0 4px 12px rgba(0,0,0,0.1);\"><\/div>\n<h1>\u5185\u5b58\u67b6\u6784\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u6982\u5ff5\u5230\u6027\u80fd\u4f18\u5316<\/h1>\n<p>\u5185\u5b58\u67b6\u6784\u662f\u73b0\u4ee3\u8ba1\u7b97\u673a\u7cfb\u7edf\u7684\u6838\u5fc3\u7ec4\u6210\u90e8\u5206\uff0c\u5b83\u51b3\u5b9a\u4e86\u6570\u636e\u5982\u4f55\u5728\u5904\u7406\u5668\u3001\u5b58\u50a8\u8bbe\u5907\u548c\u5916\u90e8\u8bbe\u5907\u4e4b\u95f4\u6d41\u52a8\u3002\u6df1\u5165\u7406\u89e3\u5185\u5b58\u67b6\u6784\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6027\u80fd\u4ee3\u7801\u3001\u4f18\u5316\u7cfb\u7edf\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h2>\u4e00\u3001\u6838\u5fc3\u6982\u5ff5<\/h2>\n<h3>1.1 \u4ec0\u4e48\u662f\u5185\u5b58\u67b6\u6784<\/h3>\n<p>\u5185\u5b58\u67b6\u6784\u5b9a\u4e49\u4e86\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u5185\u5b58\u7ec4\u7ec7\u7684\u6574\u4f53\u8bbe\u8ba1\uff0c\u5305\u62ec\u5185\u5b58\u5c42\u7ea7\u7ed3\u6784\u3001\u8bbf\u95ee\u65b9\u5f0f\u3001\u5bfb\u5740\u6a21\u5f0f\u4ee5\u53ca\u6570\u636e\u4e00\u81f4\u6027\u673a\u5236\u3002\u5b83\u76f4\u63a5\u5f71\u54cd\u7740\u7cfb\u7edf\u7684\u6574\u4f53\u6027\u80fd\u548c\u6548\u7387\u3002<\/p>\n<h3>1.2 \u5173\u952e\u672f\u8bed<\/h3>\n<ul>\n<li><strong>\u5185\u5b58\u5c42\u6b21\uff08Memory Hierarchy\uff09<\/strong>\uff1a\u4ece\u5bc4\u5b58\u5668\u5230\u78c1\u76d8\u7684\u591a\u7ea7\u5b58\u50a8\u7ed3\u6784<\/li>\n<li><strong>\u7f13\u5b58\u4e00\u81f4\u6027\uff08Cache Coherence\uff09<\/strong>\uff1a\u786e\u4fdd\u591a\u4e2a\u7f13\u5b58\u526f\u672c\u6570\u636e\u4e00\u81f4<\/li>\n<li><strong>\u5185\u5b58\u5c4f\u969c\uff08Memory Barrier\uff09<\/strong>\uff1a\u63a7\u5236\u5185\u5b58\u64cd\u4f5c\u987a\u5e8f\u7684\u540c\u6b65\u673a\u5236<\/li>\n<li><strong>\u865a\u62df\u5185\u5b58\uff08Virtual Memory\uff09<\/strong>\uff1a\u5c06\u903b\u8f91\u5730\u5740\u6620\u5c04\u5230\u7269\u7406\u5730\u5740\u7684\u673a\u5236<\/li>\n<\/ul>\n<h2>\u4e8c\u3001\u5b9e\u73b0\u539f\u7406<\/h2>\n<h3>2.1 \u5185\u5b58\u5206\u5c42\u8bbe\u8ba1<\/h3>\n<p>\u73b0\u4ee3\u8ba1\u7b97\u673a\u91c7\u7528\u5206\u5c42\u5185\u5b58\u67b6\u6784\uff0c\u4ee5\u5e73\u8861\u901f\u5ea6\u548c\u6210\u672c\uff1a<\/p>\n<p>&#8220;`c<br \/>\n\/\/ \u5185\u5b58\u5c42\u6b21\u901f\u5ea6\u5bf9\u6bd4<br \/>\ntypedef struct {<br \/>\n    char registers[8];      \/\/ \u6700\u5feb\uff1a\u76ae\u79d2\u7ea7\u8bbf\u95ee<br \/>\n    char l1_cache[64];      \/\/ \u5feb\uff1a1-4 \u7eb3\u79d2<br \/>\n    char l2_cache[512];     \/\/ \u4e2d\u7b49\uff1a10-20 \u7eb3\u79d2<br \/>\n    char l3_cache[4096];    \/\/ \u8f83\u6162\uff1a30-50 \u7eb3\u79d2<br \/>\n    char main_memory[256];  \/\/ \u6162\uff1a100 \u7eb3\u79d2<br \/>\n    char ssd_storage[1024]; \/\/ \u5f88\u6162\uff1a10-100 \u5fae\u79d2<br \/>\n    char disk_storage[16384]; \/\/ \u6700\u6162\uff1a\u6beb\u79d2\u7ea7<br \/>\n} memory_hierarchy;<\/p>\n<pre><code>\n<h3>2.2 \u7f13\u5b58\u4e00\u81f4\u6027\u534f\u8bae<\/h3>\n\n<strong>MESI \u534f\u8bae<\/strong>\u662f\u6700\u7ecf\u5178\u7684\u7f13\u5b58\u4e00\u81f4\u6027\u534f\u8bae\uff0c\u5b83\u5b9a\u4e49\u4e86\u56db\u79cd\u72b6\u6001\uff1a\n\n<\/code><\/pre>\n<p>rust<br \/>\n\/\/ MESI \u72b6\u6001\u673a\u5b9e\u73b0\u793a\u4f8b<br \/>\n#[derive(Debug, Clone, Copy, PartialEq)]<br \/>\nenum CacheLineState {<br \/>\n    Modified,     \/\/ \u6570\u636e\u5df2\u88ab\u4fee\u6539\uff0c\u4e0e\u5185\u5b58\u4e0d\u540c<br \/>\n    Exclusive,    \/\/ \u6570\u636e\u4e0e\u5185\u5b58\u4e00\u81f4\uff0c\u4ec5\u5728\u672c\u7f13\u5b58<br \/>\n    Shared,       \/\/ \u6570\u636e\u53ef\u80fd\u5728\u5176\u4ed6\u7f13\u5b58\u4e2d<br \/>\n    Invalid,      \/\/ \u6570\u636e\u65e0\u6548<br \/>\n}<\/p>\n<p>struct CacheLine {<br \/>\n    state: CacheLineState,<br \/>\n    data: [u8; 64],<br \/>\n    tags: [u64; 8],<br \/>\n}<\/p>\n<pre><code>\n<strong>MESI \u534f\u8bae\u5de5\u4f5c\u6d41\u7a0b<\/strong>\uff1a\n<ol>\n<li><strong>Modified\uff08\u4fee\u6539\u6001\uff09<\/strong>\uff1a\u6570\u636e\u53ea\u5b58\u5728\u4e8e\u5f53\u524d\u7f13\u5b58\uff0c\u4e14\u5df2\u88ab\u4fee\u6539<\/li>\n<li><strong>Exclusive\uff08\u72ec\u5360\u6001\uff09<\/strong>\uff1a\u6570\u636e\u53ea\u5b58\u5728\u4e8e\u5f53\u524d\u7f13\u5b58\uff0c\u4e0e\u5185\u5b58\u4e00\u81f4<\/li>\n<li><strong>Shared\uff08\u5171\u4eab\u6001\uff09<\/strong>\uff1a\u6570\u636e\u53ef\u80fd\u5b58\u5728\u4e8e\u591a\u4e2a\u7f13\u5b58\u4e2d<\/li>\n<li><strong>Invalid\uff08\u65e0\u6548\u6001\uff09<\/strong>\uff1a\u6570\u636e\u65e0\u6548\uff0c\u4e0d\u80fd\u4f7f\u7528<\/li>\n<\/ul>\n\n<h3>2.3 \u865a\u62df\u5185\u5b58\u7ba1\u7406<\/h3>\n\n\u865a\u62df\u5185\u5b58\u901a\u8fc7\u9875\u8868\u5b9e\u73b0\u903b\u8f91\u5730\u5740\u5230\u7269\u7406\u5730\u5740\u7684\u6620\u5c04\uff1a\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ \u7b80\u5316\u7684\u9875\u8868\u7ed3\u6784<br \/>\ntypedef struct {<br \/>\n    uint64_t physical_frame : 40;  \/\/ \u7269\u7406\u5e27\u53f7<br \/>\n    uint64_t present : 1;          \/\/ \u9875\u9762\u662f\u5426\u5728\u5185\u5b58\u4e2d<br \/>\n    uint64_t writable : 1;         \/\/ \u662f\u5426\u53ef\u5199<br \/>\n    uint64_t user : 1;             \/\/ \u7528\u6237\u6001\u53ef\u8bbf\u95ee<br \/>\n    uint64_t accessed : 1;         \/\/ \u662f\u5426\u88ab\u8bbf\u95ee\u8fc7<br \/>\n    uint64_t dirty : 1;            \/\/ \u662f\u5426\u88ab\u4fee\u6539\u8fc7<br \/>\n} page_table_entry;<\/p>\n<p>\/\/ \u5730\u5740\u8f6c\u6362\u793a\u4f8b<br \/>\nuint64_t translate_address(uint64_t virtual_addr, page_table_entry *pt) {<br \/>\n    uint64_t page_number = virtual_addr >> 12;  \/\/ \u9875\u53f7<br \/>\n    uint64_t offset = virtual_addr &#038; 0xFFF;     \/\/ \u9875\u5185\u504f\u79fb<\/p>\n<p>    page_table_entry *pte = &#038;pt[page_number];<\/p>\n<p>    if (!pte->present) {<br \/>\n        \/\/ \u89e6\u53d1\u7f3a\u9875\u5f02\u5e38<br \/>\n        handle_page_fault(page_number);<br \/>\n    }<\/p>\n<p>    return (pte->physical_frame << 12) | offset;\n}\n\n\n<pre><code>\n<h2>\u4e09\u3001\u5e38\u89c1\u5185\u5b58\u67b6\u6784\u7c7b\u578b<\/h2>\n\n<h3>3.1 \u51af\u00b7\u8bfa\u4f9d\u66fc\u67b6\u6784<\/h3>\n\n\u7279\u70b9\uff1a\u7a0b\u5e8f\u548c\u6570\u636e\u5171\u4eab\u540c\u4e00\u5185\u5b58\u7a7a\u95f4\n\n<\/code><\/pre>\n<p>assembly<br \/>\n; \u51af\u00b7\u8bfa\u4f9d\u66fc\u67b6\u6784\u793a\u4f8b\uff08\u6c47\u7f16\uff09<br \/>\nMOV AX, [1000h]    ; \u4ece\u5185\u5b58\u8bfb\u53d6\u6570\u636e\u5230\u5bc4\u5b58\u5668<br \/>\nADD AX, 5          ; \u6267\u884c\u8ba1\u7b97<br \/>\nMOV [2000h], AX    ; \u5c06\u7ed3\u679c\u5199\u56de\u5185\u5b58<\/p>\n<pre><code>\n<h3>3.2 \u54c8\u4f5b\u67b6\u6784<\/h3>\n\n\u7279\u70b9\uff1a\u7a0b\u5e8f\u548c\u6570\u636e\u4f7f\u7528\u72ec\u7acb\u7684\u5185\u5b58\u548c\u603b\u7ebf\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ \u54c8\u4f5b\u67b6\u6784\u7b80\u5316\u6a21\u578b<br \/>\nstruct HarvardArchitecture {<br \/>\n    char instruction_memory[64];    \/\/ \u72ec\u7acb\u6307\u4ee4\u5b58\u50a8\u5668<br \/>\n    char data_memory[64];           \/\/ \u72ec\u7acb\u6570\u636e\u5b58\u50a8\u5668<br \/>\n    uint8_t instruction_bus;        \/\/ \u6307\u4ee4\u603b\u7ebf<br \/>\n    uint8_t data_bus;               \/\/ \u6570\u636e\u603b\u7ebf<br \/>\n};<\/p>\n<pre><code>\n<h3>3.3 \u7edf\u4e00\u53ef\u6269\u5c55\u63a5\u53e3\uff08UEFI\uff09<\/h3>\n\n\u73b0\u4ee3\u7cfb\u7edf\u4e2d\u5e38\u89c1\u7684\u56fa\u4ef6\u63a5\u53e3\uff0c\u63d0\u4f9b\u786c\u4ef6\u62bd\u8c61\u5c42\uff1a\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ UEFI \u5185\u5b58\u7ba1\u7406\u63a5\u53e3\u793a\u4f8b<br \/>\ntypedef struct {<br \/>\n    uint32_t memory_type;           \/\/ \u5185\u5b58\u7c7b\u578b<br \/>\n    uint64_t physical_start;        \/\/ \u7269\u7406\u8d77\u59cb\u5730\u5740<br \/>\n    uint64_t virtual_start;         \/\/ \u865a\u62df\u8d77\u59cb\u5730\u5740<br \/>\n    uint64_t number_of_pages;       \/\/ \u9875\u6570<br \/>\n    uint64_t attribute;             \/\/ \u5c5e\u6027\u6807\u5fd7<br \/>\n} efi_memory_descriptor;<\/p>\n<p>\/\/ \u5206\u914d\u5185\u5b58<br \/>\nefi_status_t allocate_pages(<br \/>\n    AllocateType,<br \/>\n    MemoryType,<br \/>\n    NumberOfPages,<br \/>\n    *MemoryAddress<br \/>\n);<\/p>\n<pre><code>\n<h3>3.4 \u975e\u4e00\u81f4\u6027\u5185\u5b58\u8bbf\u95ee\uff08NUMA\uff09<\/h3>\n\n\u591a\u5904\u7406\u5668\u7cfb\u7edf\u4e2d\uff0c\u4e0d\u540c CPU \u8bbf\u95ee\u4e0d\u540c\u5185\u5b58\u533a\u57df\u901f\u5ea6\u4e0d\u540c\uff1a\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ NUMA \u67b6\u6784\u4f18\u5316\u793a\u4f8b<br \/>\nstruct NUMASystem {<br \/>\n    uint32_t node_count;            \/\/ NUMA \u8282\u70b9\u6570<br \/>\n    uint64_t local_memory[4];       \/\/ \u672c\u5730\u5185\u5b58<br \/>\n    uint64_t remote_memory[4];      \/\/ \u8fdc\u7a0b\u5185\u5b58<br \/>\n    uint32_t access_latency;        \/\/ \u8bbf\u95ee\u5ef6\u8fdf<br \/>\n};<\/p>\n<p>\/\/ NUMA \u611f\u77e5\u7684\u5185\u5b58\u5206\u914d<br \/>\nvoid* numa_alloc(size_t size, int node_id) {<br \/>\n    void *ptr;<\/p>\n<p>    \/\/ \u4f7f\u7528 numa_node_id() \u83b7\u53d6\u5f53\u524d\u8282\u70b9<br \/>\n    \/\/ \u4f7f\u7528 numa_alloc_onnode() \u5728\u6307\u5b9a\u8282\u70b9\u5206\u914d<br \/>\n    ptr = numa_alloc_onnode(size, node_id);<\/p>\n<p>    \/\/ \u521d\u59cb\u5316\u4e3a 0<br \/>\n    memset(ptr, 0, size);<\/p>\n<p>    return ptr;<br \/>\n}<\/p>\n<pre><code>\n<h2>\u56db\u3001\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h2>\n\n<h3>4.1 \u7f13\u5b58\u53cb\u597d\u6027\u4f18\u5316<\/h3>\n\n<strong>\u6570\u636e\u7ed3\u6784\u5e03\u5c40\u4f18\u5316<\/strong>\uff1a\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ \u274c \u5dee\uff1a\u7ed3\u6784\u4f53\u6210\u5458\u5206\u6563\uff0c\u5bfc\u81f4\u7f13\u5b58\u672a\u547d\u4e2d<br \/>\nstruct BadExample {<br \/>\n    char flag;           \/\/ 1 \u5b57\u8282<br \/>\n    int data1;           \/\/ 4 \u5b57\u8282\uff083 \u5b57\u8282\u586b\u5145\uff09<br \/>\n    short value;         \/\/ 2 \u5b57\u8282<br \/>\n    char flag2;          \/\/ 1 \u5b57\u8282<br \/>\n    \/\/ \u603b\u8ba1\uff1a12 \u5b57\u8282\uff0c\u4f46\u5b9e\u9645\u5360\u7528 16 \u5b57\u8282<br \/>\n};<\/p>\n<p>\/\/ \u2705 \u597d\uff1a\u6309\u5927\u5c0f\u6392\u5e8f\uff0c\u51cf\u5c11\u788e\u7247<br \/>\nstruct GoodExample {<br \/>\n    int data1;           \/\/ 4 \u5b57\u8282<br \/>\n    short value;         \/\/ 2 \u5b57\u8282<br \/>\n    char flag;           \/\/ 1 \u5b57\u8282<br \/>\n    char flag2;          \/\/ 1 \u5b57\u8282<br \/>\n    \/\/ \u603b\u8ba1\uff1a8 \u5b57\u8282\uff0c\u65e0\u6d6a\u8d39<br \/>\n};<\/p>\n<p>\/\/ \u6570\u7ec4\u7ed3\u6784\u4f18\u4e8e\u7ed3\u6784\u6570\u7ec4\uff08SoA vs AoS\uff09<br \/>\nstruct Point {<br \/>\n    float x;<br \/>\n    float y;<br \/>\n    float z;<br \/>\n};<\/p>\n<p>\/\/ \u274c \u6570\u7ec4\u7ed3\u6784\uff08AoS\uff09<br \/>\nstruct Point aos[1000];  \/\/ \u5904\u7406 x \u65f6\u9700\u8981\u52a0\u8f7d\u6240\u6709 y, z<\/p>\n<p>\/\/ \u2705 \u7ed3\u6784\u6570\u7ec4\uff08SoA\uff09<br \/>\nstruct {<br \/>\n    float x[1000];<br \/>\n    float y[1000];<br \/>\n    float z[1000];<br \/>\n} soa;  \/\/ \u5904\u7406 x \u65f6\u53ea\u52a0\u8f7d x \u6570\u636e<\/p>\n<pre><code>\n<h3>4.2 \u9884\u53d6\u4f18\u5316<\/h3>\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ \u624b\u52a8\u9884\u53d6\u793a\u4f8b<br \/>\nvoid optimize_prefetch(int *data, int size) {<br \/>\n    const int prefetch_distance = 64;  \/\/ \u9884\u53d6\u8ddd\u79bb<\/p>\n<p>    for (int i = 0; i < size; i++) {\n        \/\/ \u63d0\u524d\u9884\u53d6\u540e\u7eed\u6570\u636e\n        __builtin_prefetch(&#038;data[i + prefetch_distance], 0, 3);\n        \n        \/\/ \u5f53\u524d\u6570\u636e\u5904\u7406\n        data[i] = data[i] * 2 + 1;\n    }\n}\n\n\/\/ \u4f7f\u7528\u7f16\u8bd1\u5668\u81ea\u52a8\u9884\u53d6\n#pragma GCC ivdep\nfor (int i = 0; i < 1000; i++) {\n    data[i] = data[i] + 1;\n}\n\n\n<pre><code>\n<h3>4.3 \u5185\u5b58\u5bf9\u9f50\u4f18\u5316<\/h3>\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ \u5185\u5b58\u5bf9\u9f50\u5b9a\u4e49<br \/>\n#define ALIGN64 __attribute__((aligned(64)))<br \/>\n#define ALIGN32 __attribute__((aligned(32)))<\/p>\n<p>\/\/ \u7f13\u5b58\u884c\u5bf9\u9f50\u7684\u6570\u636e\u7ed3\u6784<br \/>\nstruct CacheLineAligned {<br \/>\n    int values[16];  \/\/ \u6b63\u597d\u5360\u6ee1 64 \u5b57\u8282\u7f13\u5b58\u884c<br \/>\n} ALIGN64;<\/p>\n<p>\/\/ SIMD \u6570\u636e\u5bf9\u9f50<br \/>\ntypedef float vec4f __attribute__((vector_size(16)));<\/p>\n<p>void simd_add(vec4f a, vec4f b, vec4f *result) {<br \/>\n    *result = a + b;  \/\/ \u5355\u6761\u6307\u4ee4\u5904\u7406 4 \u4e2a\u6d6e\u70b9\u6570<br \/>\n}<\/p>\n<pre><code>\n<h3>4.4 \u51cf\u5c11\u4f2a\u5171\u4eab\uff08False Sharing\uff09<\/h3>\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ \u274c \u4f2a\u5171\u4eab\u793a\u4f8b\uff1a\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u7f13\u5b58\u884c\u7684\u4e0d\u540c\u53d8\u91cf<br \/>\nstruct BadSharedState {<br \/>\n    volatile atomic_int counter1;  \/\/ \u7ebf\u7a0b 1 \u4fee\u6539<br \/>\n    volatile atomic_int counter2;  \/\/ \u7ebf\u7a0b 2 \u4fee\u6539<br \/>\n    \/\/ \u4e24\u8005\u53ef\u80fd\u4f4d\u4e8e\u540c\u4e00\u7f13\u5b58\u884c<br \/>\n};<\/p>\n<p>\/\/ \u2705 \u89e3\u51b3\uff1a\u6dfb\u52a0\u7f13\u5b58\u884c\u586b\u5145<br \/>\nstruct BadSharedState {<br \/>\n    volatile atomic_int counter1;<br \/>\n    char padding[60];              \/\/ \u586b\u5145\u5230\u4e0b\u4e00\u4e2a\u7f13\u5b58\u884c<br \/>\n    volatile atomic_int counter2;<br \/>\n} __attribute__((aligned(64)));<\/p>\n<pre><code>\n<h3>4.5 \u5185\u5b58\u6c60\u4f18\u5316<\/h3>\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ \u7b80\u5355\u5185\u5b58\u6c60\u5b9e\u73b0<br \/>\ntypedef struct MemoryPool {<br \/>\n    void *blocks;                    \/\/ \u5185\u5b58\u5757\u6570\u7ec4<br \/>\n    size_t block_size;               \/\/ \u5757\u5927\u5c0f<br \/>\n    size_t block_count;              \/\/ \u5757\u6570\u91cf<br \/>\n    bool *free_list;                 \/\/ \u7a7a\u95f2\u6807\u8bb0<br \/>\n} MemoryPool;<\/p>\n<p>MemoryPool* pool_create(size_t block_size, size_t count) {<br \/>\n    MemoryPool *pool = malloc(sizeof(MemoryPool));<br \/>\n    pool->block_size = block_size;<br \/>\n    pool->block_count = count;<\/p>\n<p>    \/\/ \u5206\u914d\u8fde\u7eed\u5185\u5b58\u5757<br \/>\n    pool->blocks = calloc(count, block_size);<br \/>\n    pool->free_list = calloc(count, sizeof(bool));<\/p>\n<p>    \/\/ \u6807\u8bb0\u6240\u6709\u5757\u4e3a\u7a7a\u95f2<br \/>\n    for (size_t i = 0; i < count; i++) {\n        pool->free_list[i] = true;<br \/>\n    }<\/p>\n<p>    return pool;<br \/>\n}<\/p>\n<p>void* pool_alloc(MemoryPool *pool) {<br \/>\n    for (size_t i = 0; i < pool->block_count; i++) {<br \/>\n        if (pool->free_list[i]) {<br \/>\n            pool->free_list[i] = false;<br \/>\n            return (char*)pool->blocks + i * pool->block_size;<br \/>\n        }<br \/>\n    }<br \/>\n    return NULL;  \/\/ \u5185\u5b58\u6c60\u5df2\u6ee1<br \/>\n}<\/p>\n<p>void pool_free(MemoryPool *pool, void *ptr) {<br \/>\n    size_t index = ((char*)ptr &#8211; (char*)pool->blocks) \/ pool->block_size;<br \/>\n    pool->free_list[index] = true;<br \/>\n}<\/p>\n<pre><code>\n<h2>\u4e94\u3001\u5b9e\u6218\u4f18\u5316\u6848\u4f8b<\/h2>\n\n<h3>5.1 \u56fe\u50cf\u5904\u7406\u4f18\u5316<\/h3>\n\n<\/code><\/pre>\n<p>c<br \/>\n\/\/ \u4f18\u5316\u524d\u7684\u7248\u672c<br \/>\nvoid optimize_image_filter(uint8_t *image, int width, int height) {<br \/>\n    for (int y = 0; y < height; y++) {\n        for (int x = 0; x < width; x++) {\n            uint8_t pixel = image[y * width + x];\n            image[y * width + x] = filter_pixel(pixel);\n        }\n    }\n}\n\n\/\/ \u4f18\u5316\u540e\u7684\u7248\u672c\uff08\u6309\u7f13\u5b58\u884c\u5bf9\u9f50\uff09\nvoid optimized_image_filter(uint8_t *image, int width, int height) {\n    const int CACHE_LINE_SIZE = 64;\n    \n    \/\/ \u786e\u4fdd\u5bbd\u5ea6\u662f\u7f13\u5b58\u884c\u7684\u500d\u6570\n    int aligned_width = (width + CACHE_LINE_SIZE - 1) &#038; ~(CACHE_LINE_SIZE - 1);\n    \n    for (int y = 0; y < height; y++) {\n        for (int x = 0; x < aligned_width; x += CACHE_LINE_SIZE) {\n            \/\/ \u4e00\u6b21\u5904\u7406\u4e00\u4e2a\u7f13\u5b58\u884c\u7684\u6570\u636e\n            for (int i = 0; i < CACHE_LINE_SIZE &#038;&#038; (x + i) < width; i++) {\n                uint8_t *ptr = image + y * width + x + i;\n                *ptr = filter_pixel(*ptr);\n            }\n        }\n    }\n}\n```\n\n\n\n<h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<p>\u5185\u5b58\u67b6\u6784\u7684\u7406\u89e3\u4e0e\u4f18\u5316\u662f\u7cfb\u7edf\u6027\u80fd\u63d0\u5347\u7684\u5173\u952e\u3002\u638c\u63e1\u4ee5\u4e0b\u8981\u70b9\u5c06\u5e2e\u52a9\u4f60\u7f16\u5199\u66f4\u9ad8\u6548\u7684\u4ee3\u7801\uff1a<\/p>\n<ol>\n<li><strong>\u7406\u89e3\u5185\u5b58\u5c42\u6b21<\/strong>\uff1a\u5145\u5206\u5229\u7528\u7f13\u5b58\uff0c\u51cf\u5c11\u6162\u901f\u5185\u5b58\u8bbf\u95ee<\/li>\n<li><strong>\u9075\u5b88\u7f13\u5b58\u4e00\u81f4\u6027\u539f\u5219<\/strong>\uff1a\u907f\u514d\u4f2a\u5171\u4eab\u548c\u7ade\u6001\u6761\u4ef6<\/li>\n<li><strong>\u4f18\u5316\u6570\u636e\u7ed3\u6784\u5e03\u5c40<\/strong>\uff1a\u63d0\u9ad8\u7a7a\u95f4\u5c40\u90e8\u6027\u548c\u65f6\u95f4\u5c40\u90e8\u6027<\/li>\n<li><strong>\u4f7f\u7528\u5408\u9002\u7684\u5185\u5b58\u5206\u914d\u7b56\u7565<\/strong>\uff1a\u6839\u636e\u573a\u666f\u9009\u62e9\u5806\u5206\u914d\u3001\u6808\u5206\u914d\u6216\u5185\u5b58\u6c60<\/li>\n<li><strong>\u5229\u7528\u73b0\u4ee3 CPU \u7279\u6027<\/strong>\uff1a\u5982\u9884\u53d6\u3001SIMD\u3001NUMA \u7b49<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u6301\u7eed\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u4f60\u5c06\u80fd\u591f\u8bbe\u8ba1\u51fa\u66f4\u9ad8\u6548\u7684\u7cfb\u7edf\uff0c\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<p>&#8212;<\/p>\n<p>*\u672c\u6587\u5171\u7ea6 2000 \u5b57\uff0c\u6db5\u76d6\u4e86\u5185\u5b58\u67b6\u6784\u7684\u6838\u5fc3\u6982\u5ff5\u3001\u5b9e\u73b0\u539f\u7406\u3001\u5e38\u89c1\u7c7b\u578b\u548c\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u5168\u9762\u7684\u53c2\u8003\u6307\u5357\u3002*<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u5b58\u67b6\u6784\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u6982\u5ff5\u5230\u6027\u80fd\u4f18\u5316 \u5185\u5b58\u67b6\u6784\u662f\u73b0\u4ee3\u8ba1\u7b97\u673a\u7cfb\u7edf\u7684\u6838\u5fc3\u7ec4\u6210\u90e8\u5206\uff0c\u5b83\u51b3\u5b9a\u4e86\u6570\u636e\u5982\u4f55\u5728\u5904\u7406\u5668\u3001\u5b58\u50a8\u8bbe\u5907&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-15912","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/comments?post=15912"}],"version-history":[{"count":0,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15912\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/media?parent=15912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/categories?post=15912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/tags?post=15912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}