noalloc 发布 Go 零堆分配静态检查工具

原标题:noalloc: Tooling and libraries to enforce zero heap allocations in Golang
45AI 研判分

开源项目 noalloc 推出针对 Golang 的静态分析工具,可递归检查函数调用树中的堆分配行为。该工具提供零分配的固定大小数据结构库,旨在帮助开发者在代码迭代中维持高性能内存管理。

报道正文

本文目录6 个章节

noalloc

Disclaimer: This repo is entirely written by AI. The README is largely written by AI. It is guided by a human and used in production at The Consensus in both the web service and our code search engine. It is experimental and may change vastly.

It's one thing to be able to write some code that doesn't allocate on the heap. It's another thing to be able to keep code from not allocating as the code changes. With noalloc you can assert Golang functions should not allocate and the noalloc CLI will check the entire call tree from that function for heap allocations.

noalloc will tell you every line that allocates, allowing you to remediate or exempt lines. noalloc also provides zero-allocating, fixed-sized data structures and algorithms (including a JSON encoder and decoder) to make it easier and cleaner to write code that does not allocate.

文本
go install github.com/theconsensuslabs/noalloc/cmd/noalloc@latest

You'll need Go 1.27 or later.

For example, mark a function with //noalloc:root and run noalloc in its module.

GO
//noalloc:root
func (c *Counter) Add(name string, n int) string {
	c.counts[name] += n
	return name + "=" + strconv.Itoa(c.counts[name])
}
文本
$ noalloc
counter.go:11: (*example.com/counter.Counter).Add: map write
counter.go:12: (*example.com/counter.Counter).Add: calls strconv.Itoa; string concatenation; escapes: name + "=" + ~r0 escapes to heap

noalloc exits 1 (it found an allocation) or 2 (it couldn't run at all). It allows allocations on an error or panic that ends the root's call and in first-use setup, which the caveats below cover.

Directives

  • //noalloc:root checks a function and everything it calls.
  • //noalloc:exempt <reason> exempts the line it's on and everything that line calls. On a line by itself, it applies to the next line.
  • //noalloc:fixed on a type says its methods never grow its slices, so noalloc treats them as reused memory.
  • //noalloc:append marks a function that works like append, so a call to it is judged like a call to append.

Reused memory is memory kept between requests, such as a buffer in a long-lived struct. An append to it is only allowed after a comparison proves there's room. You can also mark a type's fields as reused memory with -scratch pkg/path.Type. A call to a function outside your module is allowed when that function's own code allocates nothing, which noalloc works out from its source, or from its compiled code when it's written in assembly or is part of the runtime. Packages you check from source with -source, which takes an import path prefix, are checked as part of your module instead. Check noalloc's own libraries that way: -source github.com/theconsensuslabs/noalloc/bounded, and the same for encoding/json, so their //noalloc:fixed types and other directives count, which they don't in code outside your module. Run noalloc -h for every flag.

Designing with bounded

bounded has four containers with a fixed maximum capacity: Buf, Vec, Slice, and Map. When a write exceeds it, the write does nothing and marks the container full, so you can check once at the end instead of after every write. A container's memory can be your own, or borrowed from a Pool through a Region. These examples are also in example_test.go, where go test checks their output.

Buf

A Buf is a byte buffer. Give it memory with Reset, and once a write doesn't fit it stays full:

GO
var b bounded.Buf
b.Reset(make([]byte, 32), nil)
b.WriteString("id=")
b.WriteInt(42)
fmt.Println(string(b.Bytes()), b.Full()) // id=42 false

b.WriteString(strings.Repeat("x", 40))
fmt.Println(string(b.Bytes()), b.Full()) // id=42 true

Give it a writer too and it flushes to the writer instead of filling up:

GO
var b bounded.Buf
b.Reset(make([]byte, 8), os.Stdout)
b.WriteString("hello, ")
b.WriteString("world\n") // flushes "hello, " first
b.Flush()                // prints hello, world

Vec

A Vec is a list of any type. With memory you give it, it holds as many items as that memory's capacity:

GO
var v bounded.Vec[int]
v.Reset(make([]int, 0, 3))
for i := range 5 {
	v.Push(i * i)
}
fmt.Println(v.Items(), v.Full()) // [0 1 4] true

With ResetFrom it can borrow chunks from a pool as it fills, up to the region's budget. Its items aren't contiguous, so read it with At, or a chunk at a time with Chunks:

GO
pool := bounded.NewPool[int](4), 8
var r bounded.Region
r.Reset(2 * pool.ChunkBytes())
defer r.Release()

var v bounded.Vec[int]
v.ResetFrom(&r, pool)
for i := range 10 {
	v.Push(i)
}
fmt.Println(v.Len(), v.Full(), v.At(6)) // 8 true 6
for c := range v.Chunks() {
	fmt.Println(c) // [0 1 2 3], then [4 5 6 7]
}

Slice

A Slice is contiguous. When it fills a chunk, it can move to a chunk from the next larger pool, the way append moves a slice to a bigger array:

GO
classes := bounded.NewClasses(bounded.NewPool[int](4), 8, bounded.NewPool[int](16), 8)
var r bounded.Region
r.Reset(1 << 10)
defer r.Release()

var s bounded.Slice[int]
s.ResetFrom(&r, classes)
for i := range 10 {
	s.Push(i)
}
fmt.Println(s.Items(), s.Cap()) // [0 1 2 3 4 5 6 7 8 9] 16

Map

A Map holds up to the number of entries you reset it for. A new key past that isn't added, but you can still update the keys it has:

GO
var m bounded.Map[string, int]
m.Reset(2)
m.Set("a", 1)
m.Set("b", 2)
fmt.Println(m.Set("c", 3), m.Full()) // false true

v, ok := m.Get("a")
fmt.Println(v, ok, m.Len()) // 1 true 2

With Grow it starts small and can move to larger pools of slots as it fills:

GO
maps := bounded.NewMapClasses(bounded.NewMapPool[string, int](4), 8, bounded.NewMapPool[string, int](64), 8)
var r bounded.Region
r.Reset(1 << 20)
defer r.Release()

var m bounded.Map[string, int]
m.Grow(&r, maps)
for i := range 20 {
	m.Set(strconv.Itoa(i), i)
}
fmt.Println(m.Len(), m.Limit()) // 20 64

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)