编程笔记

lifelong learning & practice makes perfect

Go|gopls史诗级更新-优化编译及内存占用

gopls

gopls是go官方维护的工具,提供代码提示,补全,格式化等诸多功能,之前的版本在vscode+wsl里使用,内存/cpu占
用都不小,大项目打开时容易卡顿.
在2023.9.8官方发文提到了新版本所做的优化和实现原理,更新
体验了下,确实有明显的体验提升.

使用以下命令更新

1
go install golang.org/x/tools/gopls@latest

目前最新版本是 golang.org/x/tools/gopls v0.13.2

更新内容

Reductions in memory use and startup time

减少内存占用,启动时间,官方测试了28个github上流行的项目大部分可减少60%~80%的内存占用

Across these repos, the savings average around 75%, but memory reductions are non-linear: as projects get larger, so does the relative decrease in memory usage. We’ll explain this in more detail below.

在这些存储库中,平均节省约75%,但内存减少是非线性的:随着项目变大,内存使用的相对减少也是如此

Separate compilation

之前,go原本最小的编译单位是package,为了获取import的package信息,需要将所有import的package提前编译好

在v0.12开始通过使用package summary和file-based cache优化最小编译单元,使gopls可以重用部分已编译的package

Fine-grained invalidation

在之前,对package代码做了变更后,gopls必须重新编译直接或间接import该package的package(增量构建系统的基本原理)

When you make a change in one package, it’s only necessary to recompile the packages that import that one, directly or indirectly. This idea is the basis of all incremental build systems since Make in the 1970s, and gopls has been using it since its inception

从v0.12开始,只要代码变更不影响import summary就不需要重新编译

optimize static anlysis

在此之前,gopls无法实现在内存中做太多static anlysis(会引入大量依赖,内存占用飙升)

v0.12优化内存占用后引入了新的anlysis,实现类似go vet的静态分析

参考

欢迎关注我的其它发布渠道