FAQ
If I declare a local array variable to back a slice, will the compiler
definitely put it on the stack? Or will it do "the right thing" based on
the size of the array?

As an example: http://play.golang.org/p/KLiczR-pvI. I'm basically trying to
decide among implementations fLocal, fMake, or fGlobal. How should these be
ranked, in terms of style and performance? How does the answer to this
question change as the size changes?

Here's how far I've been able to get with answering this question:
1) If the size of the slice isn't statically known, clearly fMake is the
only choice that makes sense
2) If it's used from multiple goroutines concurrently, fGlobal clearly
causes issues

I know the answer to this question could potentially change in the future
or be different on different platforms, but I was curious what the current
behavior is and how people would rank these.

--
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/groups/opt_out.

Search Discussions

  • Dave Cheney at Feb 10, 2014 at 8:48 pm
    Don't guess, you can get the compiler to answer these questions.

    go build -gcflags -m



    On Tue, Feb 11, 2014 at 4:04 AM, David Thomas wrote:

    If I declare a local array variable to back a slice, will the compiler
    definitely put it on the stack? Or will it do "the right thing" based on
    the size of the array?

    As an example: http://play.golang.org/p/KLiczR-pvI. I'm basically trying
    to decide among implementations fLocal, fMake, or fGlobal. How should these
    be ranked, in terms of style and performance? How does the answer to this
    question change as the size changes?

    Here's how far I've been able to get with answering this question:
    1) If the size of the slice isn't statically known, clearly fMake is the
    only choice that makes sense
    2) If it's used from multiple goroutines concurrently, fGlobal clearly
    causes issues

    I know the answer to this question could potentially change in the future
    or be different on different platforms, but I was curious what the current
    behavior is and how people would rank these.

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • David Thomas at Feb 10, 2014 at 9:48 pm
    Thanks! I didn't know about the -m flag.

    To get a rough picture of what kind of a practical difference calling one
    versus another of these three might make, I ran a benchmark on the code I
    posted (I'm on darwin_amd64) and got the following:

    BenchmarkFLocal 10000 275129 ns/op

    BenchmarkFMake 10000 287809 ns/op

    BenchmarkFGlobal 10000 273063 ns/op

    And just to see, when I pull all of the computation out of all three (so
    that whatever allocation happens is basically all that's going on), I get
    this:

    BenchmarkFLocal 500000 2961 ns/op

    BenchmarkFMake 200000 13062 ns/op

    BenchmarkFGlobal 500000000 5.22 ns/op
    On Monday, February 10, 2014 3:47:59 PM UTC-5, Dave Cheney wrote:

    Don't guess, you can get the compiler to answer these questions.

    go build -gcflags -m




    On Tue, Feb 11, 2014 at 4:04 AM, David Thomas <davidth...@gmail.com<javascript:>
    wrote:
    If I declare a local array variable to back a slice, will the compiler
    definitely put it on the stack? Or will it do "the right thing" based on
    the size of the array?

    As an example: http://play.golang.org/p/KLiczR-pvI. I'm basically trying
    to decide among implementations fLocal, fMake, or fGlobal. How should these
    be ranked, in terms of style and performance? How does the answer to this
    question change as the size changes?

    Here's how far I've been able to get with answering this question:
    1) If the size of the slice isn't statically known, clearly fMake is the
    only choice that makes sense
    2) If it's used from multiple goroutines concurrently, fGlobal clearly
    causes issues

    I know the answer to this question could potentially change in the future
    or be different on different platforms, but I was curious what the current
    behavior is and how people would rank these.

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 10, '14 at 5:04p
activeFeb 10, '14 at 9:48p
posts3
users2
websitegolang.org

2 users in discussion

David Thomas: 2 posts Dave Cheney: 1 post

People

Translate

site design / logo © 2022 Grokbase