Skip to content

Commit c98136e

Browse files
feat: add the drawio render and replace some mermaid code (#30)
* feat: add the drawio render and replace some mermaid code * fix: ci scripts error * fix: ci scripts error
1 parent 2357288 commit c98136e

27 files changed

Lines changed: 930 additions & 251 deletions
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<mxfile host="app.diagrams.net">
2+
<diagram name="AoS Layout" id="aos-layout">
3+
<mxGraphModel dx="1000" dy="600" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
4+
<root>
5+
<mxCell id="0" />
6+
<mxCell id="1" parent="0" />
7+
8+
<mxCell id="title" value="AoS 布局:每个元素的 x,y,z,r,g,b 紧挨在一起(24 字节)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;" vertex="1" parent="1">
9+
<mxGeometry x="80" y="30" width="500" height="30" as="geometry" />
10+
</mxCell>
11+
12+
<!-- Vertex 0 -->
13+
<mxCell id="v0x" value="x₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
14+
<mxGeometry x="80" y="80" width="50" height="40" as="geometry" />
15+
</mxCell>
16+
<mxCell id="v0y" value="y₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
17+
<mxGeometry x="130" y="80" width="50" height="40" as="geometry" />
18+
</mxCell>
19+
<mxCell id="v0z" value="z₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
20+
<mxGeometry x="180" y="80" width="50" height="40" as="geometry" />
21+
</mxCell>
22+
<mxCell id="v0r" value="r₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#C62828;strokeWidth=1;fillColor=#FFCDD2;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
23+
<mxGeometry x="230" y="80" width="50" height="40" as="geometry" />
24+
</mxCell>
25+
<mxCell id="v0g" value="g₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#C62828;strokeWidth=1;fillColor=#FFCDD2;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
26+
<mxGeometry x="280" y="80" width="50" height="40" as="geometry" />
27+
</mxCell>
28+
<mxCell id="v0b" value="b₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#C62828;strokeWidth=1;fillColor=#FFCDD2;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
29+
<mxGeometry x="330" y="80" width="50" height="40" as="geometry" />
30+
</mxCell>
31+
32+
<!-- Vertex 1 -->
33+
<mxCell id="v1x" value="x₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
34+
<mxGeometry x="380" y="80" width="50" height="40" as="geometry" />
35+
</mxCell>
36+
<mxCell id="v1y" value="y₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
37+
<mxGeometry x="430" y="80" width="50" height="40" as="geometry" />
38+
</mxCell>
39+
<mxCell id="v1z" value="z₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
40+
<mxGeometry x="480" y="80" width="50" height="40" as="geometry" />
41+
</mxCell>
42+
<mxCell id="v1r" value="r₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#C62828;strokeWidth=1;fillColor=#FFCDD2;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
43+
<mxGeometry x="530" y="80" width="50" height="40" as="geometry" />
44+
</mxCell>
45+
<mxCell id="v1g" value="g₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#C62828;strokeWidth=1;fillColor=#FFCDD2;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
46+
<mxGeometry x="580" y="80" width="50" height="40" as="geometry" />
47+
</mxCell>
48+
<mxCell id="v1b" value="b₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#C62828;strokeWidth=1;fillColor=#FFCDD2;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
49+
<mxGeometry x="630" y="80" width="50" height="40" as="geometry" />
50+
</mxCell>
51+
52+
<!-- Legend -->
53+
<mxCell id="legend_g" value="坐标数据(热路径)" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=11;align=center;verticalAlign=middle;" vertex="1" parent="1">
54+
<mxGeometry x="200" y="145" width="130" height="28" as="geometry" />
55+
</mxCell>
56+
<mxCell id="legend_r" value="颜色数据(冷路径)" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#C62828;strokeWidth=1;fillColor=#FFCDD2;fontSize=11;align=center;verticalAlign=middle;" vertex="1" parent="1">
57+
<mxGeometry x="370" y="145" width="130" height="28" as="geometry" />
58+
</mxCell>
59+
60+
</root>
61+
</mxGraphModel>
62+
</diagram>
63+
</mxfile>

documents/vol1-fundamentals/c_tutorials/advanced_feature/02-cache-and-memory-hierarchy.md

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,7 @@ Python 和 Java 这类语言把内存管理彻底抽象掉了,程序员基本
5959

6060
这个金字塔结构的核心设计思想叫做**局部性原理**(Principle of Locality)。局部性分两种:**时间局部性**指的是如果一个数据刚被访问过,那它很可能会在不久之后再次被访问;**空间局部性**指的是如果一个数据被访问了,那它附近地址的数据很可能也会被访问。Cache 的所有设计决策——缓存行的大小、预取策略、替换策略——全都是围绕这两个局部性来的。我们可以用一张简图来直观感受这个金字塔:
6161

62-
```mermaid
63-
graph TD
64-
subgraph "越往上越快、越小、越贵;越往下越慢、越大、越便宜"
65-
Reg["<b>寄存器</b><br/>~1 周期 | 容量: ~数百字节"]
66-
L1["<b>L1 Cache</b><br/>~3-4 周期 | 容量: 32-64 KB"]
67-
L2["<b>L2 Cache</b><br/>~10-14 周期 | 容量: 256 KB-1 MB"]
68-
L3["<b>L3 Cache</b><br/>~30-50 周期 | 容量: 数 MB-数十 MB"]
69-
DRAM["<b>主存 DRAM</b><br/>~100-300 周期 | 容量: GB 级"]
70-
Disk["<b>SSD/HDD</b><br/>~微秒/毫秒 | 容量: TB 级"]
71-
end
72-
Reg ~~~ L1 ~~~ L2 ~~~ L3 ~~~ DRAM ~~~ Disk
73-
```
62+
![存储器层次结构金字塔示意](./02-memory-hierarchy.drawio)
7463

7564
你可以在 Linux 上用 `lscpu` 命令查看自己机器的 Cache 配置,输出的 `L1d cache``L2 cache``L3 cache` 那几行就是你的 CPU 实际情况。接下来我们一层一层往下拆。
7665

@@ -294,24 +283,9 @@ typedef struct {
294283

295284
对比一下两者在内存中的布局差异:
296285

297-
```mermaid
298-
graph LR
299-
subgraph "AoS 布局:每个元素的 x,y,z,r,g,b 紧挨在一起(24 字节)"
300-
A0["x0 y0 z0 r0 g0 b0"]
301-
A1["x1 y1 z1 r1 g1 b1"]
302-
A2["x2 y2 z2 r2 g2 b2"]
303-
A0 --> A1 --> A2
304-
end
305-
```
286+
![AoS 内存布局](./02-aos-layout.drawio)
306287

307-
```mermaid
308-
graph LR
309-
subgraph "SoA 布局:所有 x 连续,所有 y 连续,以此类推"
310-
SX["连续的 x<br/>x0 x1 x2 x3 x4 ..."]
311-
SY["连续的 y<br/>y0 y1 y2 y3 y4 ..."]
312-
SZ["连续的 z<br/>z0 z1 z2 z3 z4 ..."]
313-
end
314-
```
288+
![SoA 内存布局](./02-soa-layout.drawio)
315289

316290
如果你的热路径只处理坐标 `x``y``z`,而不碰颜色 `r``g``b`,那 SoA 的优势就非常明显了——你连续遍历 `x[0]``x[1]``x[2]`……数据在内存里完全连续,Cache 命中率接近 100%。而 AoS 的情况下,每访问一个 `x` 都会顺带把同一结构体里的 `y``z``r``g``b` 也拉进 Cache(因为它们在同一条缓存行上),但我们暂时用不到颜色数据,这些空间就浪费了。
317291

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<mxfile host="app.diagrams.net">
2+
<diagram name="Memory Hierarchy" id="memory-hierarchy">
3+
<mxGraphModel dx="1200" dy="800" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
4+
<root>
5+
<mxCell id="0" />
6+
<mxCell id="1" parent="0" />
7+
8+
<!-- Title -->
9+
<mxCell id="title" value="内存层次结构金字塔" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;" vertex="1" parent="1">
10+
<mxGeometry x="200" y="20" width="300" height="35" as="geometry" />
11+
</mxCell>
12+
13+
<!-- Registers -->
14+
<mxCell id="reg" value="&lt;b&gt;寄存器&lt;/b&gt;&lt;br&gt;~1 周期 | ~数百字节" style="rounded=1;whiteSpace=wrap;html=1;strokeColor=#B71C1C;strokeWidth=2;fillColor=#FFCDD2;fontSize=14;align=center;verticalAlign=middle;" vertex="1" parent="1">
15+
<mxGeometry x="300" y="70" width="180" height="50" as="geometry" />
16+
</mxCell>
17+
18+
<!-- L1 -->
19+
<mxCell id="l1" value="&lt;b&gt;L1 Cache&lt;/b&gt;&lt;br&gt;~3-4 周期 | 32-64 KB" style="rounded=1;whiteSpace=wrap;html=1;strokeColor=#C62828;strokeWidth=2;fillColor=#EF9A9A;fontSize=14;align=center;verticalAlign=middle;" vertex="1" parent="1">
20+
<mxGeometry x="270" y="135" width="240" height="50" as="geometry" />
21+
</mxCell>
22+
23+
<!-- L2 -->
24+
<mxCell id="l2" value="&lt;b&gt;L2 Cache&lt;/b&gt;&lt;br&gt;~10-14 周期 | 256 KB - 1 MB" style="rounded=1;whiteSpace=wrap;html=1;strokeColor=#E65100;strokeWidth=2;fillColor=#FFCC80;fontSize=14;align=center;verticalAlign=middle;" vertex="1" parent="1">
25+
<mxGeometry x="230" y="200" width="320" height="50" as="geometry" />
26+
</mxCell>
27+
28+
<!-- L3 -->
29+
<mxCell id="l3" value="&lt;b&gt;L3 Cache&lt;/b&gt;&lt;br&gt;~30-50 周期 | 数 MB - 数十 MB" style="rounded=1;whiteSpace=wrap;html=1;strokeColor=#F9A825;strokeWidth=2;fillColor=#FFF59D;fontSize=14;align=center;verticalAlign=middle;" vertex="1" parent="1">
30+
<mxGeometry x="190" y="265" width="400" height="50" as="geometry" />
31+
</mxCell>
32+
33+
<!-- DRAM -->
34+
<mxCell id="dram" value="&lt;b&gt;主存 DRAM&lt;/b&gt;&lt;br&gt;~100-300 周期 | GB 级" style="rounded=1;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=2;fillColor=#A5D6A7;fontSize=14;align=center;verticalAlign=middle;" vertex="1" parent="1">
35+
<mxGeometry x="150" y="330" width="480" height="50" as="geometry" />
36+
</mxCell>
37+
38+
<!-- Disk -->
39+
<mxCell id="disk" value="&lt;b&gt;SSD / HDD&lt;/b&gt;&lt;br&gt;~微秒 / 毫秒 | TB 级" style="rounded=1;whiteSpace=wrap;html=1;strokeColor=#1565C0;strokeWidth=2;fillColor=#90CAF9;fontSize=14;align=center;verticalAlign=middle;" vertex="1" parent="1">
40+
<mxGeometry x="110" y="395" width="560" height="50" as="geometry" />
41+
</mxCell>
42+
43+
<!-- Left annotation: faster/smaller -->
44+
<mxCell id="left_label" value="快&lt;br&gt;↑&lt;br&gt;小&lt;br&gt;↑&lt;br&gt;贵" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#B71C1C;rotation=0;" vertex="1" parent="1">
45+
<mxGeometry x="30" y="130" width="50" height="250" as="geometry" />
46+
</mxCell>
47+
48+
<!-- Right annotation: slower/larger -->
49+
<mxCell id="right_label" value="慢&lt;br&gt;↓&lt;br&gt;大&lt;br&gt;↓&lt;br&gt;便宜" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1565C0;" vertex="1" parent="1">
50+
<mxGeometry x="700" y="130" width="50" height="250" as="geometry" />
51+
</mxCell>
52+
53+
</root>
54+
</mxGraphModel>
55+
</diagram>
56+
</mxfile>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<mxfile host="app.diagrams.net">
2+
<diagram name="SoA Layout" id="soa-layout">
3+
<mxGraphModel dx="1000" dy="600" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
4+
<root>
5+
<mxCell id="0" />
6+
<mxCell id="1" parent="0" />
7+
8+
<mxCell id="title" value="SoA 布局:所有 x 连续,所有 y 连续,以此类推" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;" vertex="1" parent="1">
9+
<mxGeometry x="100" y="20" width="460" height="30" as="geometry" />
10+
</mxCell>
11+
12+
<!-- x array -->
13+
<mxCell id="xl" value="x[]" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=2;fillColor=#E8F5E9;fontSize=14;fontStyle=1;align=center;verticalAlign=middle;" vertex="1" parent="1">
14+
<mxGeometry x="80" y="70" width="50" height="35" as="geometry" />
15+
</mxCell>
16+
<mxCell id="x0" value="x₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
17+
<mxGeometry x="130" y="70" width="50" height="35" as="geometry" />
18+
</mxCell>
19+
<mxCell id="x1" value="x₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
20+
<mxGeometry x="180" y="70" width="50" height="35" as="geometry" />
21+
</mxCell>
22+
<mxCell id="x2" value="x₂" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
23+
<mxGeometry x="230" y="70" width="50" height="35" as="geometry" />
24+
</mxCell>
25+
<mxCell id="xd" value="x₃ x₄ ..." style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=12;align=center;verticalAlign=middle;dashed=1;" vertex="1" parent="1">
26+
<mxGeometry x="280" y="70" width="100" height="35" as="geometry" />
27+
</mxCell>
28+
29+
<!-- y array -->
30+
<mxCell id="yl" value="y[]" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=2;fillColor=#E8F5E9;fontSize=14;fontStyle=1;align=center;verticalAlign=middle;" vertex="1" parent="1">
31+
<mxGeometry x="80" y="115" width="50" height="35" as="geometry" />
32+
</mxCell>
33+
<mxCell id="y0" value="y₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
34+
<mxGeometry x="130" y="115" width="50" height="35" as="geometry" />
35+
</mxCell>
36+
<mxCell id="y1" value="y₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
37+
<mxGeometry x="180" y="115" width="50" height="35" as="geometry" />
38+
</mxCell>
39+
<mxCell id="y2" value="y₂" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
40+
<mxGeometry x="230" y="115" width="50" height="35" as="geometry" />
41+
</mxCell>
42+
<mxCell id="yd" value="y₃ y₄ ..." style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=12;align=center;verticalAlign=middle;dashed=1;" vertex="1" parent="1">
43+
<mxGeometry x="280" y="115" width="100" height="35" as="geometry" />
44+
</mxCell>
45+
46+
<!-- z array -->
47+
<mxCell id="zl" value="z[]" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=2;fillColor=#E8F5E9;fontSize=14;fontStyle=1;align=center;verticalAlign=middle;" vertex="1" parent="1">
48+
<mxGeometry x="80" y="160" width="50" height="35" as="geometry" />
49+
</mxCell>
50+
<mxCell id="z0" value="z₀" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
51+
<mxGeometry x="130" y="160" width="50" height="35" as="geometry" />
52+
</mxCell>
53+
<mxCell id="z1" value="z₁" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
54+
<mxGeometry x="180" y="160" width="50" height="35" as="geometry" />
55+
</mxCell>
56+
<mxCell id="z2" value="z₂" style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=13;align=center;verticalAlign=middle;" vertex="1" parent="1">
57+
<mxGeometry x="230" y="160" width="50" height="35" as="geometry" />
58+
</mxCell>
59+
<mxCell id="zd" value="z₃ z₄ ..." style="rounded=0;whiteSpace=wrap;html=1;strokeColor=#2E7D32;strokeWidth=1;fillColor=#C8E6C9;fontSize=12;align=center;verticalAlign=middle;dashed=1;" vertex="1" parent="1">
60+
<mxGeometry x="280" y="160" width="100" height="35" as="geometry" />
61+
</mxCell>
62+
63+
</root>
64+
</mxGraphModel>
65+
</diagram>
66+
</mxfile>

0 commit comments

Comments
 (0)