chore(go): 章节通用样式提到 doc.css,并修 5 处内容硬伤
- assets/doc.css 新增 .tag/.tag.s/.figure/.figcap/p.take/article>h4,14 章共用 - 01/02 删除页内 style,改用共享样式 - 04 §4.8 写屏障注释与正文补上「栈未扫完才 shade 新值」,与 §4.6 对齐 - 07 末段锚点 sec-10-9 → sec-10-10 - 06 §6.2 正文「五种」→「四种」,与标题一致 - 08 §8.1 「2014 年」→「2015 年底(Go 1.6)」 - 08 §8.4 发送路径补 panic(send on closed channel),与 §8.6 对齐
This commit is contained in:
@@ -6,20 +6,6 @@
|
||||
<title>第 1 章 从进程到 goroutine:并发模型的底层账本 · Go 八股知识库</title>
|
||||
<meta name="description" content="按「现象 → 机制 → 代价」重讲 goroutine:三种调度单位的差别、2KB 动态栈与复制扩容、newproc 与 gogo 的创建切换、GOMAXPROCS 的真实含义、阻塞时被卡住的是 G 还是 M,并给出四张示意图与切换成本对比。">
|
||||
<link rel="stylesheet" href="assets/doc.css">
|
||||
<style>
|
||||
/* ---- 本章新增:段首标签、示意图、小结(仅作用于本页,不改动共享样式) ---- */
|
||||
.tag{display:inline-block;font-size:12.8px;font-weight:800;letter-spacing:.6px;padding:1px 9px;margin-right:9px;
|
||||
border-radius:999px;background:var(--brand-soft);color:var(--brand-ink);
|
||||
border:1px solid color-mix(in srgb,var(--brand) 32%,var(--line));vertical-align:2px;white-space:nowrap}
|
||||
.tag.s{background:var(--warn-bg);color:var(--warn);border-color:var(--warn-line)}
|
||||
.figure{margin:0 0 22px;padding:16px 18px 12px;border:1px solid var(--line);border-radius:13px;background:var(--soft)}
|
||||
.figure svg{display:block;width:100%;height:auto;font-family:inherit}
|
||||
.figcap{margin:10px 2px 0;font-size:15px;color:var(--muted);text-align:center;line-height:1.65}
|
||||
.figcap b{color:var(--ink)}
|
||||
p.take{border-left:3px solid var(--brand);padding:2px 0 2px 13px;margin:6px 0 22px;color:var(--muted);font-size:16.8px}
|
||||
p.take b{color:var(--ink)}
|
||||
@media print{.figure{break-inside:avoid}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
@@ -6,16 +6,6 @@
|
||||
<title>第 2 章 GMP 调度器:Go 如何把十万个 goroutine 跑起来 · Go 八股知识库</title>
|
||||
<meta name="description" content="从 GM 为什么不够用讲到 G、M、P、schedt 四个结构体,串起 schedule 到 goexit 的调度循环,讲透 work stealing、系统调用 handoff、异步抢占与 11 个调度场景。">
|
||||
<link rel="stylesheet" href="assets/doc.css">
|
||||
<style>
|
||||
.tag{display:inline-block;font-size:11.5px;font-weight:700;letter-spacing:.04em;color:var(--brand-ink);background:var(--brand-soft);border-radius:5px;padding:1px 7px;margin-right:8px;vertical-align:2px;white-space:nowrap}
|
||||
.tag.s{background:var(--line);color:var(--muted)}
|
||||
.figure{margin:22px 0 26px;padding:14px 10px 8px;border:1px solid var(--line);border-radius:12px;background:var(--card)}
|
||||
.figure svg{display:block;width:100%;height:auto}
|
||||
.figcap{margin:8px 6px 2px;font-size:13px;color:var(--muted);line-height:1.6}
|
||||
p.take{margin:14px 0 4px;padding:8px 14px;border-left:3px solid var(--brand);background:var(--soft);border-radius:0 8px 8px 0;font-size:14.5px}
|
||||
h4{font-size:15.5px;color:var(--brand-ink);margin:24px 0 8px;letter-spacing:.01em}
|
||||
@media print{.figure{break-inside:avoid}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
@@ -298,15 +298,17 @@ p.next = q
|
||||
|
||||
// 概念上的编译结果(简化)
|
||||
if writeBarrier.enabled { // 全局标志,只在 _GCmark 阶段为 true
|
||||
gcWriteBarrier(&p.next, q) // 内部:shade(*slot); shade(q); *slot = q
|
||||
gcWriteBarrier(&p.next, q) // 内部:shade(*slot); 当前栈未扫完时再 shade(q); *slot = q
|
||||
} else {
|
||||
*(*unsafe.Pointer)(unsafe.Pointer(&p.next)) = q // 退化成普通写入
|
||||
}</pre>
|
||||
<p><code>writeBarrier.enabled</code> 的判断很便宜:一次 load 加一个分支,非 GC 期间恒为 false,
|
||||
分支预测几乎不失败,对正常路径的影响通常是个位数百分比。</p>
|
||||
<p>屏障感知的是赋值语句的<strong>两端</strong>,不是整片内存:<strong>旧值 <code>*slot</code></strong>
|
||||
即将被覆盖,它指向的对象可能失去唯一引用,所以置灰(删除语义);<strong>新值 <code>ptr</code></strong>
|
||||
即将写入,它指向的对象可能还没标到,所以也置灰(插入语义)。实现上 <code>shade</code> 不直接改对象头、
|
||||
即将被覆盖,它指向的对象可能失去唯一引用,所以<strong>无条件</strong>置灰(删除语义);<strong>新值 <code>ptr</code></strong>
|
||||
即将写入,它指向的对象可能还没标到,所以<strong>在当前栈还没扫完时</strong>也置灰(插入语义)——
|
||||
这正是 4.6 那个 <code>if stackIsGrey()</code> 分支,栈一旦扫完变黑,新值就不再需要屏障保护。
|
||||
实现上 <code>shade</code> 不直接改对象头、
|
||||
加锁入队:Go 1.14 起每个 P 有一块 <code>wbBuf</code>,先把待置灰的指针攒进缓冲区,满一批再统一 flush,
|
||||
把「每次赋值一次原子操作」摊薄成「每批一次」。缓冲区本身也是根,这正是 mark termination
|
||||
必须 flush 所有 <code>wbBuf</code> 的原因。也因此它只拦指针写入、只在标记阶段生效——
|
||||
|
||||
@@ -118,7 +118,7 @@ b = [99]</pre>
|
||||
</div>
|
||||
|
||||
<h3 id="sec-6-2">6.2 四种初始化方式与 nil slice</h3>
|
||||
<p>初始化写法有五种,可分两类:<strong>要不要一块新数组</strong>。从已有数组或切片上切不分配;字面量和 <code>make</code>
|
||||
<p>初始化写法有四种,可分两类:<strong>要不要一块新数组</strong>。从已有数组或切片上切不分配;字面量和 <code>make</code>
|
||||
会准备数组;<code>var s []int</code> 连数组都没有。把它们的 <code>len</code>、<code>cap</code>、是否 <code>nil</code> 放在一起看,
|
||||
很多「为什么这里 panic」就不用猜了。</p>
|
||||
<pre data-lang="go" data-name="main.go:nil、空切片与 make 的区别">package main
|
||||
|
||||
@@ -579,7 +579,7 @@ m.Range(func(k, v any) bool {
|
||||
<p>把这一章压成一句话:map 在 1.23 及以前是「桶数组 + 桶内 8 槽 + 溢出链」的哈希表,1.24 起换成控制字 + 探测,
|
||||
用 6.5 的装载因子和随机种子守住平均 O(1),用渐进式扩容把搬迁成本摊平,用 fail-fast 拒绝并发写。
|
||||
切片扩容与 map 扩容的相似和不同可以对照 <a href="06-slice.html#sec-6-4">6.4</a> 看,
|
||||
锁与 <code>sync.Map</code> 的取舍和 <a href="10-sync.html#sec-10-9">10.9</a> 是同一套判断逻辑,
|
||||
锁与 <code>sync.Map</code> 的取舍和 <a href="10-sync.html#sec-10-10">10.10</a> 是同一套判断逻辑,
|
||||
想先补齐 goroutine 的退出模型可以回看 <a href="01-goroutine.html#sec-1-8">1.8</a>。</p>
|
||||
</article>
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
无缓冲时 send 必须等到接收方就绪才返回,这是一次会合(rendezvous);有缓冲时同步只发生在「未满/非空」上,
|
||||
收发双方在时间上解耦。所以 <code>make(chan T)</code> 与 <code>make(chan T, 1)</code> 的差别是耦合程度,不是性能。</p>
|
||||
<p>FIFO 也是修出来的:先阻塞在 <code>recvq</code> 的接收方先被服务。早期实现靠「写缓冲 → 唤醒 → 抢读 → 失败再睡」
|
||||
的重试,带缓冲 channel 并不严格 FIFO(issue #11506),2014 年改成直接交接才有今天的语义。</p>
|
||||
的重试,带缓冲 channel 并不严格 FIFO(issue #11506),2015 年底(Go 1.6)改成直接交接才有今天的语义。</p>
|
||||
<div class="box tip">
|
||||
<span class="bt">面试怎么答「channel 是什么」</span>
|
||||
<p>三句话一层:定位是 CSP 的载体、goroutine 间的同步点,不只是队列;实现是带锁的环形队列加两个 sudog 等待队列;
|
||||
@@ -199,7 +199,10 @@
|
||||
mysg.c = c
|
||||
c.sendq.enqueue(mysg)
|
||||
gopark(chanparkcommit, unsafe.Pointer(&c.lock), waitReasonChanSend, traceBlockChanSend, 2)
|
||||
// 醒来后:success 为 false 说明是被 close 唤醒的
|
||||
// 醒来后:success 为 false 表示 channel 已被关闭 —— 这里会 panic
|
||||
if !mysg.success {
|
||||
panic(plainError("send on closed channel"))
|
||||
}
|
||||
return true
|
||||
}</pre>
|
||||
<p>路径 ① 里,<code>send</code> 调 <code>sendDirect</code> 把 <code>ep</code> 直接拷到接收方的变量地址
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* ==========================================================================
|
||||
/* ==========================================================================
|
||||
Docker 专题 · 共用样式(与仓库内其它知识站点同一套设计变量)
|
||||
通过 --brand / --accent 注入 Go 青,目录页与章节页视觉一致
|
||||
========================================================================== */
|
||||
@@ -201,6 +201,26 @@ pre .hl{background:color-mix(in srgb,var(--accent) 22%,transparent);border-radiu
|
||||
.box.lab{background:var(--warn-bg);border-color:var(--warn-line);border-left-color:var(--warn)}
|
||||
.box.lab .bt{color:var(--warn)}
|
||||
|
||||
/* 章节通用组件:段首标签、示意图、一句话小结
|
||||
01/02 章原本把这几条写在页内 <style> 里,其余章节用不上;现统一提到共享样式表。
|
||||
.tag 段首药丸标签(现象 / 机制 / 代价)
|
||||
.tag.s 可跳过标记
|
||||
.figure SVG 示意图容器(<figure class="figure">)
|
||||
p.take 每节结尾的「一句话」小结 */
|
||||
.tag{display:inline-block;font-size:12.6px;font-weight:800;letter-spacing:.5px;padding:1px 9px;margin-right:9px;
|
||||
border-radius:999px;background:var(--brand-soft);color:var(--brand-ink);
|
||||
border:1px solid color-mix(in srgb,var(--brand) 30%,var(--line));vertical-align:2px;white-space:nowrap}
|
||||
.tag.s{background:var(--warn-bg);color:var(--warn);border-color:var(--warn-line)}
|
||||
.figure{margin:22px 0 24px;padding:16px 18px 12px;border:1px solid var(--line);border-radius:13px;background:var(--soft)}
|
||||
.figure svg{display:block;width:100%;height:auto;font-family:inherit}
|
||||
.figcap{margin:10px 2px 0;font-size:14.8px;color:var(--muted);text-align:center;line-height:1.65}
|
||||
.figcap b{color:var(--ink)}
|
||||
p.take{margin:14px 0 22px;padding:9px 15px;border-left:3px solid var(--brand);border-radius:0 9px 9px 0;
|
||||
background:var(--soft);color:var(--muted);font-size:16px}
|
||||
p.take b{color:var(--ink)}
|
||||
article > h4{font-size:16px;color:var(--brand-ink);margin:26px 0 9px;letter-spacing:.01em}
|
||||
@media print{.figure{break-inside:avoid}}
|
||||
|
||||
/* 场景步骤编号列表 */
|
||||
.steps{counter-reset:s;list-style:none;padding-left:0;margin:0 0 18px}
|
||||
.steps > li{counter-increment:s;position:relative;padding-left:46px;margin-bottom:12px}
|
||||
|
||||
Reference in New Issue
Block a user