4-数据类型内存结构-slice

5. slice

var var_slice []int = []int{1,2,3,4,5,6};

slice变量有三部分组成
1-数组地址
2-slice长度
3-slice容量

   1879 "".var_slice SDATA size=24
   1880         0x0000 00 00 00 00 00 00 00 00 07 00 00 00 00 00 00 00  ................
   1881         0x0010 07 00 00 00 00 00 00 00                          ........
   1882         rel 0+8 t=1 "".statictmp_1+0

1、使用runtime.newobject()创建一个数组类型对象。
2、将数据复制到新的数组对象中。
3、给slice对象地址设置为新的数组对象地址。
4、设置slice对象长度
5、设置slice对象容量

src\runtime\slice.go
type slice struct {
    array unsafe.Pointer
    len   int
    cap   int
}
     67         0x00a6 00166 (type.go:43)       LEAQ    type.[6]int(SB), AX //将数组类型放入AX
     68         0x00ad 00173 (type.go:43)       PCDATA  $2, $0
     69         0x00ad 00173 (type.go:43)       MOVQ    AX, (SP)    //将数组类型压入栈
     70         0x00b1 00177 (type.go:43)       CALL    runtime.newobject(SB)   //根据类型创建对象
     71         0x00b6 00182 (type.go:43)       PCDATA  $2, $1
     72         0x00b6 00182 (type.go:43)       MOVQ    8(SP), AX   //返回新建对象地址放入AX
     73         0x00bb 00187 (type.go:43)       PCDATA  $0, $2
     74         0x00bb 00187 (type.go:43)       MOVQ    AX, ""..autotmp_30+176(SP)
     75         0x00c3 00195 (type.go:43)       MOVUPS  "".statictmp_0(SB), X0 //statictmp_0为数组[]int{1,2,3,4,5,6}地址
     76         0x00ca 00202 (type.go:43)       MOVUPS  X0, (AX)
     77         0x00cd 00205 (type.go:43)       MOVUPS  "".statictmp_0+16(SB), X0
     78         0x00d4 00212 (type.go:43)       MOVUPS  X0, 16(AX)
     79         0x00d8 00216 (type.go:43)       MOVUPS  "".statictmp_0+32(SB), X0
     80         0x00df 00223 (type.go:43)       PCDATA  $2, $0
     81         0x00df 00223 (type.go:43)       MOVUPS  X0, 32(AX)
     82         0x00e3 00227 (type.go:43)       PCDATA  $2, $1
     83         0x00e3 00227 (type.go:43)       PCDATA  $0, $1
     84         0x00e3 00227 (type.go:43)       MOVQ    ""..autotmp_30+176(SP), AX
     85         0x00eb 00235 (type.go:43)       TESTB   AL, (AX)
     86         0x00ed 00237 (type.go:43)       JMP     239
     87         0x00ef 00239 (type.go:43)       PCDATA  $2, $0
     88         0x00ef 00239 (type.go:43)       PCDATA  $0, $3
     89         0x00ef 00239 (type.go:43)       MOVQ    AX, "".var2_slice+504(SP)
     90         0x00f7 00247 (type.go:43)       MOVQ    $6, "".var2_slice+512(SP)
     91         0x0103 00259 (type.go:43)       MOVQ    $6, "".var2_slice+520(SP)
   1899 "".statictmp_0 SRODATA size=48
   1900         0x0000 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00  ................
   1901         0x0010 03 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00  ................
   1902         0x0020 05 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00  ................
// implementation of new builtin
// compiler (both frontend and SSA backend) knows the signature
// of this function
func newobject(typ *_type) unsafe.Pointer {
    return mallocgc(typ.size, typ, true)
}

当数组元素个数<=9个时,会采用MOVUPS进行数据copy
当数组元素个数超过9个时,会改为DUFFCOPY进行内存copy。


     67         0x00a6 00166 (type.go:44)       LEAQ    type.[10]int(SB), AX
     68         0x00ad 00173 (type.go:44)       PCDATA  $2, $0
     69         0x00ad 00173 (type.go:44)       MOVQ    AX, (SP)
     70         0x00b1 00177 (type.go:44)       CALL    runtime.newobject(SB)
     71         0x00b6 00182 (type.go:44)       PCDATA  $2, $2
     72         0x00b6 00182 (type.go:44)       MOVQ    8(SP), DI
     73         0x00bb 00187 (type.go:44)       PCDATA  $0, $2
     74         0x00bb 00187 (type.go:44)       MOVQ    DI, ""..autotmp_30+176(SP)
     75         0x00c3 00195 (type.go:44)       PCDATA  $2, $3
     76         0x00c3 00195 (type.go:44)       LEAQ    "".statictmp_0(SB), SI
     77         0x00ca 00202 (type.go:44)       PCDATA  $2, $0
     78         0x00ca 00202 (type.go:44)       DUFFCOPY        $826
     79         0x00dd 00221 (type.go:44)       PCDATA  $2, $1
     80         0x00dd 00221 (type.go:44)       PCDATA  $0, $1
     81         0x00dd 00221 (type.go:44)       MOVQ    ""..autotmp_30+176(SP), AX
     82         0x00e5 00229 (type.go:44)       TESTB   AL, (AX)
     83         0x00e7 00231 (type.go:44)       JMP     233
     84         0x00e9 00233 (type.go:44)       PCDATA  $2, $0
     85         0x00e9 00233 (type.go:44)       PCDATA  $0, $3
     86         0x00e9 00233 (type.go:44)       MOVQ    AX, "".var2_slice+504(SP)
     87         0x00f1 00241 (type.go:44)       MOVQ    $10, "".var2_slice+512(SP)
     88         0x00fd 00253 (type.go:44)       MOVQ    $10, "".var2_slice+520(SP)

你可能感兴趣的:(4-数据类型内存结构-slice)