美文网首页Go
Go的Sizeof和内存对齐浅析

Go的Sizeof和内存对齐浅析

作者: 杏壳 | 来源:发表于2019-06-12 18:26 被阅读0次

类型和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.

相关文章

  • Go的Sizeof和内存对齐浅析

    类型和Sizeof Go的类型系统比较简单,从reflect包可以窥得一二: 针对每一种类型,了解每种类型所占的空...

  • 内存对齐

    知识点概要 OC对象内存对齐结构体内存对齐 OC对象内存对齐 计算内存大小的三种方式 1.sizeof:系统提供的...

  • sizeof与字节对齐

    参考 【面试题】sizeof引发的血案编译器与字节对齐c 语言字节对齐问题详解C/C++内存对齐内存存取粒度C和C...

  • iOS-OC底层二 :修改系统内存对齐

    一、内存对齐 获取内存大小的三种方式分别是: sizeof:sizeof计算内存大小时,传入的主要对象是数据类型,...

  • sizeof之字节对齐/内存对齐

    想用结构体来存储一些数据(Objective-c环境下),结构体如下: 那如果itemName是NSString类...

  • iOS 内存分配特点

    结构体分配的时候会实现内存对齐 sizeof()底层空间分配的时候也会实现对齐 并且是16的倍数 不是需要多少就...

  • Go的内存对齐问题

    我们都知道,在Go语言中,空的结构体不占用任何存储空间,比如: 运行结果: 但是,在某些情况下,以上结论并不是完全...

  • 2.iOS底层学习之内存对齐

    学习了内存对齐之后的疑问?? 1.为啥要内存对齐?2.内存对齐的规则?3.内存对齐实例分析。 内存对齐的目的 上网...

  • 内存对齐

    本次主要讨论三个问题: 什么是内存对齐 内存对齐的好处 如何对齐 内存对齐 内存对齐是一种提高内存访问速度的策略。...

  • IOS面试题

    1.一个NSObject对象占用多少内存? 64bit: sizeof 也是以8字节对齐,是个运算符直接传类型计算...

网友评论

    本文标题:Go的Sizeof和内存对齐浅析

    本文链接:https://www.haomeiwen.com/subject/kyikfctx.html