Possible Memory Leak

实际上对于一个有GC的语言,我们不必太多关心内存泄漏的问题,因为程序的runtime帮我们很好地额回收不再使用的内存。但是,我们还是得了解一些特殊的场景,这些场景会产生暂时性或者永久性的内存泄漏。 待开坑...

December 25, 2020 · 1 min · 2 words · Me

Close Channels Gracefully

优雅地关闭通道 场景一:M个接收者和一个发送者。发送者通过关闭用来传输数据的通道来传递发送结束信号 这是最简单的一种情形。当发送者欲结束发送,让它关闭用来传输数据的通道即可。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 package main import ( "time" "math/rand" "sync" "log" ) func main() { rand.Seed(time.Now().UnixNano()) log.SetFlags(0) // ... const Max = 100000 const NumReceivers = 100 wgReceivers := sync.WaitGroup{} wgReceivers.Add(NumReceivers) // ... dataCh := make(chan int) // 发送者 go func() { for { if value := rand.Intn(Max); value == 0 { // 此唯一的发送者可以安全地关闭此数据通道。 close(dataCh) return } else { dataCh <- value } } }() // 接收者 for i := 0; i < NumReceivers; i++ { go func() { defer wgReceivers.Done() // 接收数据直到通道dataCh已关闭 // 并且dataCh的缓冲队列已空。 for value := range dataCh { log.Println(value) } }() } wgReceivers.Wait() } 场景二: 一个接收者和N个发送者,此唯一接收者通过关闭一个额外的信号通道来通知发送者不要在发送数据了 此情形比上一种情形复杂一些。我们不能让接收者关闭用来传输数据的通道来停止数据传输,因为这样做违反了通道关闭原则。 但是我们可以让接收者关闭一个额外的信号通道来通知发送者不要在发送数据了。 ...

December 24, 2020 · 7 min · 1377 words · Me

Channels Concurrency Work-Around

记录了一些channels常见的场景,以及自己的一些感受: 使用通道进行异步和并发编程是简单和惬意的; 通道同步技术比被很多其它语言采用的其它同步方案(比如角色模型和async/await模式)有着更多的应用场景和更多的使用变种。 通道作为同步手段,并非在任何情况下都是最佳的同步技术,本文也会补充原子操作和sync包内其他的技术作为参考。 将通道用做future/promise 很多其它流行语言支持future/promise来实现异步(并发)编程。 Future/promise常常用在请求/回应场合。 返回单向接收通道做为函数返回结果 在下面这个例子中,sumSquares函数调用的两个实参请求并发进行。 每个通道读取操作将阻塞到请求返回结果为止。 两个实参总共需要大约3秒钟(而不是6秒钟)准备完毕(以较慢的一个为准)。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package main import ( "time" "math/rand" "fmt" ) func longTimeRequest() <-chan int32 { r := make(chan int32) go func() { time.Sleep(time.Second * 3) // 模拟一个工作负载 r <- rand.Int31n(100) }() return r } func sumSquares(a, b int32) int32 { return a*a + b*b } func main() { rand.Seed(time.Now().UnixNano()) a, b := longTimeRequest(), longTimeRequest() fmt.Println(sumSquares(<-a, <-b)) } 将单向发送通道类型用做函数实参 和上例一样,在下面这个例子中,sumSquares函数调用的两个实参的请求也是并发进行的。 和上例不同的是longTimeRequest函数接收一个单向发送通道类型参数而不是返回一个单向接收通道结果。 ...

December 22, 2020 · 14 min · 2852 words · Me

Black Magic

阅读到的一些方便、有趣的技巧或者ideas的随手记录,后续考虑对相关话题专门开坑 一行代码画出专业的论文图 SciencePlots 计算机专业向来不缺少专业的绘图软件,从Excel到PPT,从最近沸沸扬扬的Matlab到Matplotlib、pyplot、ggplot,乃至其他更为专业的软件,着实丰富了我们的画图生活。 但是,这些软件或工具的背后,常常需要我们付出更多的努力:调色、统一格式、展示要高大上,等等。 现在,一款开源的软件工具包问世了:SciencePlots。它让你用一行代码画出天然高端且美观的论文图。 SciencePlots是一个依附于Matplotlib的扩展包,可以通过pip一键安装: pip install SciencePlots 然后我们在画图时,只需要一句with.plt.style.context(['science']):,就可以画出非常美观且专业的图: 你还可以加一个选项with.plt.style.context(['science','ieee']):,就能画出IEEE格式的图: 甚至是超美的散点图: 还有很多自定义的图像风格,保证节约我们的画图时间 这个包默认会调用latex来画图,如果不想用latex(也不是完全需要),可以在context里写一个属性’nolatex’即可。不然如果没有安装latex或latex路径配置有问题,则会报错。

December 20, 2020 · 1 min · 15 words · Me

书单记录

这个post为记录目前正在阅读与研究的section Go语言设计 Go语言设计与实现 Go Under The Hood 这两本在写作目的和内容规划都是一致的,不过第二个原本不再维护内容,作者开了下面的新的项目,把撰写原本而积累的与Go相关的资源进行了重新的整理。 Go设计历史 pprof对服务端性能影响的研究 考虑一些极端场景,比如极度追求性能,压榨系统资源以及技术栈必须是Go的业务场景下,是否能自己构建Reactor网络模型 GRPC框架对服务侧性能的影响 Russ Cox正则表达式系列 You should not be permitted to write production code if you do not have an journeyman license in regular expressions or floating point math. – Rob Pike Regular Expression Matching Can Be Simple And Fast 编译器词法分析:正则语言和正则表达式 Go内存原理与调度模型 正在整理专栏 Bound Checking Elimination Crafting Interpreter 时常看PL和Compiler的基础 crafting interpreters Kosaraju’s Algorithm 看William Lin的coding interview觉得用来处理树和图很好,算法4里也有 Heilmeier问题系列 思考某篇paper的选题 What are you trying to do? Articulate your objectives using absolutely no jargon. How is it done today, and what are the limits of current practice? Who cares? [Support other’s research? Shape research landscape? Power applications in industry?] What’s new in your approach and why do you think it will be successful? If you’re successful, what difference will it make? [e.g. Contributions in theory/modeling? Improve accuracy by 5% on dataset A, B, C…?] What are the risks and the payoffs? [Further, how would you mitigate the risks? If your proposed method does not work, what could be alternative design? These can end up as discussions such as ablation studies in your paper.] How much will it cost? [e.g. How many GPUs do your experiments require? How long is each training process? How about data storage?] How long will it take? [How many hours are you going to work on this per week? When is the submission DDL? Can you make it?] What are the midterm and final “exams” to check for success?

December 19, 2020 · 2 min · 239 words · Me