Go界面优化

With reference to Russ Cox's December 2009 article, Go Data Structures: Interfaces

In the section on Memory Optimizations, Russ suggested that if the data being stored in the interface{} is smaller that the size of a uintptr then the value would be stored in the interface directly and no allocation of the data, followed by taking it's address would be needed.

If I test this with the following code: -

package main

import (
    "fmt"
    "unsafe"
)

type iface struct {
    _    unsafe.Pointer
    data unsafe.Pointer
}

func main() {
    var i interface{} = 12
    var pi = (*iface)(unsafe.Pointer(&i))
    fmt.Printf("if.data: %p", pi.data)
}

The result is: -

if.data: 0x127e2c

Clearly an address and not the value 12 that would be expected had the optimization been performed.

Does Go no longer support the interface optimization or am I missing something?

Compiler And Runtime Optimizations

This page lists optimizations done by the compilers. Note that these are not guaranteed by the language specification.

Interface values

Word-sized value in an interface value

Putting a word-sized-or-less non-pointer type in an interface value doesn't allocate.

gc: 1.0-1.3, but not in 1.4+
gccgo: never

Go no longer performs that optimization for Go1.4+.