类型和Sizeof
Go的类型系统比较简单,从reflect包可以窥得一二:
// A Kind represents the specific kind of type that a Type represents.
// The zero Kind is not a valid kind.
type Kind uint
const (
Invalid Kind = iota
Bool
Int
Int8
Int16
Int32
Int64
Uint
Uint8
Uint16
Uint32
Uint64
Uintptr
Float32
Float64
Complex64
Complex128
Array
Chan
Func
Interface
Map
Ptr
Slice
String
Struct
UnsafePointer
)
针对每一种类型,了解每种类型所占的空间对于编写以及优化Go程序有较大的帮助。以下测试均在GOARCH=amd64环境下
type | Sizeof(字节数) |
---|---|
Bool | 1 |
Int | 8 |
Uint | 8 |
Uintptr | 8 |
Array | Sizeof(type) * len |
Chan | 8 |
Func | 8 |
Interface | 16 |
Map | 8 |
Ptr(Go指针) | 8 |
Slice | 8 |
String | 16 |
Struct | 需考虑字节对齐和填充 |
UnsafePointer | 8 |
Alignment and padding
Go是一个C家族语言,Go的结构体类型基于C语言的结构体演化而来,因此关于字节对齐等概念也是通用的。通过调整结构体字段的声明顺序有时可以优化程序性能,减少内存消耗,在一些内存受限的嵌入式系统或者操作系统内核,或者你的程序达到了内存上限的场景,只要内存是有限的,该项技术就仍然有用。
关于内存对齐,在Go语言规范中可以找到如下描述:
Computer architectures may require memory addresses to be aligned; that is, for addresses of a variable to be a multiple of a factor, the variable's type's alignment. The function Alignof takes an expression denoting a variable of any type and returns the alignment of the (type of the) variable in bytes. For a variable x:
uintptr(unsafe.Pointer(&x)) % unsafe.Alignof(x) == 0
关于更详尽的Go内存布局可参考Go语言规范和go101 Memory Layouts。
关于以C语言结构体为基础的字节对齐和填充可参考这篇介绍详尽的文章:
关于结构体字段的字节偏移和大小可以使用下面的工具进行显示和字段调整参考:
另外,这是一个优化的小例子:
从Go1.5开始,有一点需要注意。在一个结构体结尾的一个零长度的字段(一个零长度的数组或者空结构体)要占一个字节。在padding-is-hard这篇文章可以找到详细的讨论。下面是一个简单的测试:
type EmptyEndStruct struct {
Field bool
_ struct{}
}
testT := EmptyEndStruct{true, struct{}{}}
fmt.Println("struct type Sizeof:", unsafe.Sizeof(testT)) // struct type Sizeof: 2
unsafe、reflect和sync/atomic
unsafe包在编译时进行计算,而reflect在运行时计算对齐的长度
- unsafe.Alignof(t)
- unsafe.Alignof(x.t)
- reflect.TypeOf(t).Align()
- reflect.TypeOf(t).FieldAlign()
另外,在sync/atomic的文档底部,详细说明了atomic包的64位原子函数由于字节对齐导致的在32位芯片上的使用限制。
Sizeof完整的测试代码
boolTemp := true
intTemp := 99
var uintTemp uint = 99
uintptrTemp := (uintptr)(unsafe.Pointer(&intTemp))
arrayTemp := [3]int8{1, 2}
// arrayTemp2 := [3]string{}
chanTemp := make(<-chan string, 100)
ll := func(a int, b string) (int, error) {
return a + 1, errors.New("a error")
}
type interfaceTest interface {
test(int, int) int
}
var interfaceTemp interfaceTest
mapTemp := make(map[string]string)
sliceTemp := []int{1, 2, 3}
stringTemp := "hongyi"
// Alignment and padding
type StructTemp struct {
Field3 bool // 1
Field2 int //8
Field4 uint64 // 8
Field1 string //16
}
structTemp1 := StructTemp{true, 1, 89, "hongyi"}
unsafePointerTemp := unsafe.Pointer(&intTemp)
fmt.Println("bool type Sizeof:", unsafe.Sizeof(boolTemp))
fmt.Println("int type Sizeof:", unsafe.Sizeof(intTemp))
fmt.Println("uint type Sizeof:", unsafe.Sizeof(uintTemp))
fmt.Println("uintptr type Sizeof:", unsafe.Sizeof(uintptrTemp))
fmt.Println("array type Sizeof:", unsafe.Sizeof(arrayTemp))
fmt.Println("chan type Sizeof:", unsafe.Sizeof(chanTemp))
fmt.Println("func type Sizeof:", unsafe.Sizeof(ll))
fmt.Println("interface type Sizeof:", unsafe.Sizeof(interfaceTemp))
fmt.Println("map type Sizeof:", unsafe.Sizeof(mapTemp))
if reflect.TypeOf(&intTemp).Kind() == reflect.Ptr {
fmt.Println("ptr type Sizeof:", unsafe.Sizeof(&intTemp))
}
fmt.Println("slice type Sizeof:", unsafe.Sizeof(&sliceTemp))
fmt.Println("string type Sizeof:", unsafe.Sizeof(stringTemp))
fmt.Println("struct type Sizeof:", unsafe.Sizeof(structTemp1))
fmt.Println("unsafePointer type Sizeof:", unsafe.Sizeof(unsafePointerTemp))
Go结构体内存对齐举例
type T1 struct {
a int8
// On 64-bit architectures, to make field b
// 8-byte aligned, 7 bytes need to be padded
// here. On 32-bit architectures, to make
// field b 4-byte aligned, 3 bytes need to be
// padded here.
b int64
c int16
// To make the size of type T1 be a multiple
// of the alignment guarantee of T1, on 64-bit
// architectures, 6 bytes need to be padded
// here, and on 32-bit architectures, 2 bytes
// need to be padded here.
}
// The size of T1 is 24 (= 1 + 7 + 8 + 2 + 6)
// bytes on 64-bit architectures and is 16
// (= 1 + 3 + 8 + 2 + 2) on 32-bit architectures.
type T2 struct {
a int8
// To make field c 2-byte aligned, one byte
// needs to be padded here on both 64-bit
// and 32-bit architectures.
c int16
// On 64-bit architectures, to make field b
// 8-byte aligned, 4 bytes need to be padded
// here. On 32-bit architectures, field b is
// already 4-byte aligned, so no bytes need
// to be padded here.
b int64
}
// The size of T2 is 16 (= 1 + 1 + 2 + 4 + 8)
// bytes on 64-bit architectures, and is 12
// (= 1 + 1 + 2 + 8) on 32-bit architectures.