go:build
go:linkname
1
//go:linkname localname importpath.name
importpath.namelocalnameunsafeimportpath.namelocalnamelocalnameunsafe
例如
time/time.go
1
2
// Provided by package runtime.
func now() (sec int64, nsec int32, mono int64)
runtime/timestub.go
1
2
3
4
5
6
7
import _ "unsafe" // for go:linkname
//go:linkname time_now time.now
func time_now() (sec int64, nsec int32, mono int64) {
sec, nsec = walltime()
return sec, nsec, nanotime()
}
runtimeruntimeunsafe//go:lickname time_now time.now
time_nowtime.now//go:lickname
go buildgo:linknamecompilego build-complete
go:noscape
1
//go:noscape
该指令指定下一个有声明但没有主体(意味着实现有可能不是 Go)的函数,不允许编译器对其做逃逸分析。
一般情况下,该指令用于内存分配优化。编译器默认会进行逃逸分析,会通过规则判定一个变量是分配到堆上还是栈上。
go:noescape
例如
1
2
3
4
// memmove copies n bytes from "from" to "to".
// in memmove_*.s
//go:noescape
func memmove(to, from unsafe.Pointer, n uintptr)
我们观察一下这个案例,它满足了该指令的常见特性。如下:
memmove_*.s: 只有声明,没有主体,其主体是由底层汇编实现的。
memmove: 函数功能,在栈上处理性能会更好。
go:noslip
1
//go:noslip
该指令指定文件中声明的下一个函数不得包含堆栈溢出检查。
简单来讲,就是这个函数跳过堆栈溢出的检查。
例如
1
2
3
4
//go:nosplit
func key32(p *uintptr) *uint32 {
return (*uint32)(unsafe.Pointer(p))
}
go:nowritebarrierrec
1
//go:nowritebarrierrec
该指令表示编译器遇到写屏障时就会产生一个错误,并且允许递归。也就是这个函数调用的其他函数如果有写屏障也会报错。
简单来讲,就是针对写屏障的处理,防止其死循环。
例如
1
2
3
4
5
//go:nowritebarrierrec
func gcFlushBgCredit(scanWork int64) {
...
}
go:yeswritebarrierrec
1
//go:yeswritebarrierrec
go:nowritebarrierrecgo:nowritebarrierrec
go:yeswritebarrierrec
例如
1
2
3
4
//go:yeswritebarrierrec
func gchelper() {
...
}
go:noinline
1
//go:noinline
该指令表示该函数禁止进行内联。
1
2
3
4
//go:noinline
func unexportedPanicForTesting(b []byte, i int) byte {
return b[i]
}
例如
我们观察一下这个案例,是直接通过索引取值,逻辑比较简单。如果不加上 go:noinline 的话,就会出现编译器对其进行内联优化。
显然,内联有好有坏。该指令就是提供这一特殊处理。
go:norace
1
//go:norace
该指令表示禁止进行竞态检测。
常见的形式就是在启动时执行 go run -race,能够检测应用程序中是否存在双向的数据竞争,非常有用。
例如
1
2
3
4
//go:norace
func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
...
}
go:notinheap
1
//go:notinheap
该指令常用于类型声明,它表示这个类型不允许从 GC 堆上进行申请内存。
在运行时中常用其来做较低层次的内部结构,避免调度器和内存分配中的写屏障,能够提高性能。
例如
1
2
3
4
5
6
7
8
9
// notInHeap is off-heap memory allocated by a lower-level allocator
// like sysAlloc or persistentAlloc.
//
// In general, it's better to use real types marked as go:notinheap,
// but this serves as a generic type for situations where that isn't
// possible (like in the allocators).
//
//go:notinheap
type notInHeap struct{}
每日一算
描述
2个逆序的链表,要求从低位开始相加,得出结果也逆序输出,返回值是逆序结果链表的头结点
解题思路
需要注意的是进位的问题,极端情况如下:
Input: (9 -> 9 -> 9 -> 9) + (1 -> ) Output 0 -> 0 -> 0 -> 0 -> 1
为了处理方法统一,可以先建立一个虚拟头结点,这个虚拟头结点的Next指向真正的head,这样head不需要单独处理,直接wihle循环即可。另外判断循环终止的条件不用是 p.Next != nil,这样最后一位还需要额外计算,终止条件应该是 p != nil。
代码如下:
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
52
53
54
55
56
57
58
59
60
61
62
63
type ListNode struct {
Val int
Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil || l2 == nil{
return nil
}
// 虚拟头结点
head := &ListNode{
Val: 0,
Next: nil,
}
current := head
carry := 0 // 是否需要进位
// 遍历
for l1 != nil || l2 != nil {
var x, y int
if l1 == nil {
x = 0
}else{
x = l1.Val
}
if l2 == nil {
y = 0
}else {
y = l2.Val
}
current.Next = &ListNode{
Val: (x + y + carry) % 10,
Next: nil,
}
current = current.Next
carry = (x+y+carry) / 10
if l1 != nil {
l1 = l1.Next
}
if l2 != nil {
l2 = l2.Next
}
fmt.Println("carry:", carry)
}
if carry > 0 { // 最后一位相加又进位,要在尾结点再加一个结点
current.Next = &ListNode{
Val: carry % 10,
Next: nil,
}
}
return head.Next
}