FAQ
Hi there, I have a function that needs to introspect on the types of
elements in a []interface{} slice. The interface{} values may be either
pointer-to-huge-struct or huge-struct values. That is, it is really
[]MyBigStruct or []*MyBigStruct. For background, sometimes it is more
efficient to inline big structs in the slice to reduce GC burden.

How can I efficiently determine which element type is present in a
[]interface{} slice? Using reflection naively may cause the large struct
values to be rewritten on the stack multiple times, which I must avoid.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Lance Taylor at Nov 20, 2015 at 12:29 am

    On Thu, Nov 19, 2015 at 3:03 PM, Daniel wrote:
    Hi there, I have a function that needs to introspect on the types of
    elements in a []interface{} slice. The interface{} values may be either
    pointer-to-huge-struct or huge-struct values. That is, it is really
    []MyBigStruct or []*MyBigStruct. For background, sometimes it is more
    efficient to inline big structs in the slice to reduce GC burden.

    How can I efficiently determine which element type is present in a
    []interface{} slice? Using reflection naively may cause the large struct
    values to be rewritten on the stack multiple times, which I must avoid.
    Reflecting on a large struct doesn't cause the struct to be passed on
    the stack multiple times. Converting a struct to an interface value
    will make one copy. After that, it won't be copied again.

    In any case, you have a slice, and passing the slice to reflect.Type
    isn't going to copy the slice contents. You want something like
    reflect.Type(s).Elem().Kind(). If the result is reflect.Ptr, you have
    []*MyBigStruct. If the result is reflect.Struct you have
    []*MyBigStruct.

    It may help to read http://blog.golang.org/laws-of-reflection .

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 20, '15 at 12:15a
activeNov 20, '15 at 12:29a
posts2
users2
websitegolang.org

2 users in discussion

Daniel: 1 post Ian Lance Taylor: 1 post

People

Translate

site design / logo © 2022 Grokbase