主要是源码分析,可能也会有一些使用上的知识点。
因为用了 stackedit 支持的 mermaid 图表,这些图表在 github 上显示不出来,如果想看完整内容的话,可以在 stackedit.io 上导出为 html。
Go source code analysis(zh-cn)
License: Other
主要是源码分析,可能也会有一些使用上的知识点。
因为用了 stackedit 支持的 mermaid 图表,这些图表在 github 上显示不出来,如果想看完整内容的话,可以在 stackedit.io 上导出为 html。
分两部分:
如题
select的源码中在被唤醒后,会把在其他channel上等待的都删除。
但是这有一个问题,就是channel如何识别这是第一个select,以及如何在唤醒第一个select后其他的channel碰到此select的不会去唤醒他?
详情可看chan源码,我也在解决这个问题,可惜还是有疑惑
slice 原理应该和c++的vector差不多的实现,就是传递的数组内存的首指针,等同于引用传递吧,然后扩容的话,就算不传递,append后的结果你不赋值给原slice,也不会产生影响。只是一点疑问,希望得到回复
有些指令没有介绍,所以在读一些新功能实现的时候比较费劲,比如 1.14 的 defer,所以应该补充一些额外的指令,以排除这些障碍。
SETEQ
ORL
ANDL
等
在分析 golang 启动过程时,经常会提到这么一行
LEAQ runtime·m0+m_tls(SB), DI
意思是将m0.tls的地址存入DI寄存器,查看源码时有不少地方用到 m_tls,比如如下:
#ifdef GOOS_windows
CMPQ BX, $0
JNE settls
MOVQ $0, 0x28(GS)
RET
settls:
MOVQ g_m(BX), AX
LEAQ m_tls(AX), AX
MOVQ AX, 0x28(GS)
#endif
get_tls(CX)
MOVQ BX, g(CX)
RET
我感觉应该也是类似 get_tls (src/runtime/go_tls.h) 这样一个宏,但是翻遍 golang 源码里面好像找不到具体的定义
func main() {
ch := make(chan int, 100)
for elem := range ch { // 主要就是这里的 for range...
fmt.Println(i)
}
}
这个地方,在输出通道数据后,会报异常,fatal error: all goroutines are asleep - deadlock!
这个问题要怎么解决呢?
在两个for循环里面,第一个if 语句里面缺少退出条件
if b.tophash[i] == emptyRest {
break bucketloop
}
如果i的槽位后面是空的(emptyRest),那么就可以在这个位置插入新的数据,就可以直接退出。
来自西班牙的程序员渴望共享作者的资料库,以寻找英文版本。
我真的很喜欢这种语言,很少有资源可以从低层次上解释广泛使用的工具。 但是,由于中文的命令有限,我目前无法用中文阅读。 我正在等待作者的英文出版物。 谢谢! !
C系语言中,调用函数把硬件寄存器BP值写入栈中,被调用函数ret后,读取当前bp加固定位移得到调用函数的硬件寄存器BP值,即可恢复现场,看plan9的文章对4个伪寄存器并没有进行类似操作。
请问 伪寄存器在函数调用前后是如何恢复现场的?
曹大后面会追加编译流程的部分吗?谢谢解答,阿里嘎多.
需要能让类似这种有一堆 hack 指令的函数更易让人读懂:
// func gogo(buf *gobuf)
// restore state from Gobuf; longjmp
TEXT runtime·gogo(SB), NOSPLIT, $16-8
MOVQ buf+0(FP), BX // gobuf
MOVQ gobuf_g(BX), DX
MOVQ 0(DX), CX // make sure g != nil
get_tls(CX)
MOVQ DX, g(CX)
MOVQ gobuf_sp(BX), SP // restore SP
MOVQ gobuf_ret(BX), AX
MOVQ gobuf_ctxt(BX), DX
MOVQ gobuf_bp(BX), BP
MOVQ $0, gobuf_sp(BX) // clear to help garbage collector
MOVQ $0, gobuf_ret(BX)
MOVQ $0, gobuf_ctxt(BX)
MOVQ $0, gobuf_bp(BX)
MOVQ gobuf_pc(BX), BX
JMP BX
https://github.com/cch123/golang-notes/blob/master/assembly.md#%E5%9C%B0%E5%9D%80%E8%BF%90%E7%AE%97
这个地方不是很明白。另外,movq也有Scale的概念吗,比如下面的代码。
#define g(r) 0(r)(TLS*1)
get_tls(CX)
MOVQ g(CX), AX
展开后是 movq 0(CX)(TLS*1),AX
这是什么鬼?
我看内存初始化章接,将内存划分为三个部分: span (512 MB) │ bitmap (32 GB) │ arena (512 GB),但是对于64位的虚拟地址空间来不应该有512G那么大吧,所以想请问下这里的512M、32GB和512GB是指内存空间吗?
➜ splicetest go test -benchmem -run=^$ -bench="BenchmarkNormalReadWrite|BenchmarkGoSplice" splice_test.go
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i3 CPU M 330 @ 2.13GHz
BenchmarkNormalReadWrite-4 705 1820369 ns/op 1079975 B/op 236 allocs/op
--- BENCH: BenchmarkNormalReadWrite-4
splice_test.go:178: test:BenchmarkNormalReadWrite,total: 1,rate: 100.00%
splice_test.go:178: test:BenchmarkNormalReadWrite,total: 100,rate: 57.00%
splice_test.go:178: test:BenchmarkNormalReadWrite,total: 705,rate: 53.00%
BenchmarkGoSplice-4 2021/05/02 03:51:45 http: Accept error: accept tcp [::]:9610: accept4: too many open files; retrying in 5ms
2021/05/02 03:51:45 http: Accept error: accept tcp [::]:9610: accept4: too many open files; retrying in 5ms
2021/05/02 03:51:45 http: Accept error: accept tcp [::]:9610: accept4: too many open files; retrying in 5ms
...
➜ ~ lsof -p 31086
...
main.test 31086 root 988u IPv4 679284 0t0 TCP localhost:9612->localhost:50176 (CLOSE_WAIT)
main.test 31086 root 989u IPv4 676803 0t0 TCP localhost:9612->localhost:50214 (CLOSE_WAIT)
main.test 31086 root 992u IPv4 678352 0t0 TCP localhost:9612->localhost:50192 (CLOSE_WAIT)
main.test 31086 root 998u IPv4 679298 0t0 TCP localhost:9612->localhost:50198 (CLOSE_WAIT)
main.test 31086 root 999u IPv4 679327 0t0 TCP localhost:9612->localhost:50228 (CLOSE_WAIT)
main.test 31086 root 1001u IPv4 679315 0t0 TCP localhost:9612->localhost:50208 (CLOSE_WAIT)
main.test 31086 root 1006u IPv4 677623 0t0 TCP localhost:9612->localhost:50222 (CLOSE_WAIT)
main.test 31086 root 1013u IPv4 679331 0t0 TCP localhost:9612->localhost:50230 (CLOSE_WAIT)
main.test 31086 root 1015u sock 0,9 0t0 680028 protocol: TCP
main.test 31086 root 1017u IPv4 677630 0t0 TCP localhost:9612->localhost:50232 (CLOSE_WAIT)
main.test 31086 root 1021u IPv4 676840 0t0 TCP localhost:9612->localhost:50238 (CLOSE_WAIT)
如上,https://github.com/cch123/golang-notes/blob/master/io.md 里最后splice的benchmark代码有sockfd泄露,
下面for循环里的defer n.Close()
应该是执行不到的,close逻辑放到对应的normal和gosplice函数里合理一点。
for {
n, err := l.Accept()
if err != nil {
continue
}
defer n.Close()
...
}
文章表述:主要是对 key 进行 hash 计算,计算后用 low bits 和高 8 位 hash 找到对应的位置。
https://github.com/cch123/golang-notes/blob/master/map.md
问题:如果两个key的hash值不同,但是low bits 和高 8 位 hash 这两项相同。这个时候元素如何查找,以元素如何插入。
"在 hint <= 7 时,会调用 makemap_small 来进行初始化,如果 hint > 7,则调用 makemap",我在调试的时候发现不是而是8。这应该跟编译器有关。
文章中 https://github.com/cch123/golang-notes/blob/master/defer.md 的 fn newdefer 中使用systemstack生成_defer结构体,后面为什么会出现获取不到d的情况,此时在p的deferpool中应该有值
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.