collymy
collymy

Reputation: 77

Go interface optimization

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?

Upvotes: 3

Views: 312

Answers (0)

Related Questions