Golang unsafe包使用笔记
unsafe包简介
unsafe包提供了访问底层内存的方法。是用unsafe函数可以提高访问对象的速度。
应用场景
通常用于对大数组的遍历。
访问数组
package main import ( "fmt" "unsafe" ) func main() { array := []int{1, 2, 3} base := uintptr(unsafe.Pointer(&array[0])) size := unsafe.Sizeof(array[0]) ptr := unsafe.Pointer(base + 2*size) element := *(*int)(ptr) fmt.Println(element, array[2]) }
访问结构体
package main import ( "fmt" "unsafe" ) type Foo struct { A int B int } func main() { foo := &Foo{1, 2} fmt.Println(foo) base := uintptr(unsafe.Pointer(foo)) offset := unsafe.Offsetof(foo.A) ptr := unsafe.Pointer(base + offset) *(*int)(ptr) = 3 fmt.Println(foo) }
类型转换
package main import ( "fmt" "unsafe" ) func main() { var f float64 = 0.0 var x uint64 = *(*uint64)(unsafe.Pointer(&f)) fmt.Println(f, x) }
注意事项
- unsafe可能引起兼容性问题。
性能测试
代码:
func sum(arr []int) int { sum := 0 for _, x := range arr { sum += x } return sum } func unsafeSum(arr []int) int { ptr := unsafe.Pointer(&arr[0]) count := len(arr) step := unsafe.Sizeof(arr[0]) sum := 0 for i := 0; i < count; i++ { sum += *(*int)(ptr) ptr = unsafe.Pointer(uintptr(ptr) + step) } return sum } func unsafeSum2(arr []int) int { base := unsafe.Pointer(&arr[0]) count := len(arr) step := unsafe.Sizeof(arr[0]) sum := 0 for i := 0; i < count; i++ { ptr := unsafe.Pointer(uintptr(base) + uintptr(i)*step) sum += *(*int)(ptr) } return sum } func unsafeSum3(arr []int) int { beg := unsafe.Pointer(&arr[0]) size := unsafe.Sizeof(arr[0]) end := unsafe.Pointer(uintptr(beg) + uintptr(len(arr))*size) ptr := beg sum := 0 for ptr != end { sum += *(*int)(ptr) ptr = unsafe.Pointer(uintptr(ptr) + size) } return sum }
测试代码:
package main import ( "testing" ) var ( array = []int{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, } ) func BenchmarkUnsafeSum(b *testing.B) { for i := 0; i < b.N; i++ { unsafeSum(array) } } func BenchmarkSum(b *testing.B) { for i := 0; i < b.N; i++ { sum(array) } } func TestUnsafeSum(t *testing.T) { if result := unsafeSum(array); result != 550 { t.Fatal("expect %v get %v", 550, result) } } func TestUnsafeSum2(t *testing.T) { if result := unsafeSum2(array); result != 550 { t.Fatal("expect %v get %v", 550, result) } } func BenchmarkUnsafeSum2(b *testing.B) { for i := 0; i < b.N; i++ { unsafeSum2(array) } } func TestUnsafeSum3(t *testing.T) { if result := unsafeSum3(array); result != 550 { t.Fatal("expect %v get %v", 550, result) } } func BenchmarkUnsafeSum3(b *testing.B) { for i := 0; i < b.N; i++ { unsafeSum3(array) } }
结果:
BenchmarkUnsafeSum-2 20000000 94.0 ns/op BenchmarkSum-2 10000000 129 ns/op BenchmarkUnsafeSum2-2 10000000 126 ns/op BenchmarkUnsafeSum3-2 20000000 84.0 ns/op PASS ok tq/lab/Unsafe 6.926s
参考资料
修订记录
- 2017年06月15日 建立文档。
有疑问加站长微信联系(非本文作者)