FAQ
Hi all,

Today in https://github.com/valyala/gorpc/pull/1 I discovered that
non-blocking select is faster than blocking select by more than 6 times:

func makeChans(n int) (bCh chan struct{}, nbCh chan struct{}) {
         nbCh = make(chan struct{}, n)
         bCh = make(chan struct{})
         return
}

func BenchmarkBlockingSelect(b *testing.B) {
         bCh, nbCh := makeChans(b.N)

         b.ResetTimer()
         for i := 0; i < b.N; i++ {
                 select {
                 case nbCh <- struct{}{}:
                 case <-bCh:
                 }
         }
}

func BenchmarkNonBlockingSelect(b *testing.B) {
         bCh, nbCh := makeChans(b.N)

         b.ResetTimer()
         for i := 0; i < b.N; i++ {
                 select {
                 case nbCh <- struct{}{}:
                 default:
                         b.Fatalf("Unexpected code path")
                         select {
                         case nbCh <- struct{}{}:
                         case <-bCh:
                         }
                 }
         }
}


Results:

BenchmarkBlockingSelect 5000000 256 ns/op
BenchmarkNonBlockingSelect 30000000 43.2 ns/op

Is there a real reason behind such big performance difference other than
unoptimized code inside golang for select {} containing at least one
non-blocking case?

--
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

  • Dmitry Vyukov at Mar 5, 2015 at 11:24 am

    On Thu, Mar 5, 2015 at 2:14 PM, Aliaksandr Valialkin wrote:
    Hi all,

    Today in https://github.com/valyala/gorpc/pull/1 I discovered that
    non-blocking select is faster than blocking select by more than 6 times:

    func makeChans(n int) (bCh chan struct{}, nbCh chan struct{}) {
    nbCh = make(chan struct{}, n)
    bCh = make(chan struct{})
    return
    }

    func BenchmarkBlockingSelect(b *testing.B) {
    bCh, nbCh := makeChans(b.N)

    b.ResetTimer()
    for i := 0; i < b.N; i++ {
    select {
    case nbCh <- struct{}{}:
    case <-bCh:
    }
    }
    }

    func BenchmarkNonBlockingSelect(b *testing.B) {
    bCh, nbCh := makeChans(b.N)

    b.ResetTimer()
    for i := 0; i < b.N; i++ {
    select {
    case nbCh <- struct{}{}:
    default:
    b.Fatalf("Unexpected code path")
    select {
    case nbCh <- struct{}{}:
    case <-bCh:
    }
    }
    }
    }


    Results:
    BenchmarkBlockingSelect 5000000 256 ns/op
    BenchmarkNonBlockingSelect 30000000 43.2 ns/op


    Is there a real reason behind such big performance difference other than
    unoptimized code inside golang for select {} containing at least one
    non-blocking case?
    Objective reasons:
    - half of the time select starts by trying to receive from bCh, which
    fails but consumes time
    - full-fledged select requires more bookkeeping

    The rest is inefficient implementation. There is a patch that
    substantially speedups selects:
    https://codereview.appspot.com/112990043/

    --
    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.
  • Aliaksandr Valialkin at Mar 5, 2015 at 12:51 pm

    On Thursday, March 5, 2015 at 1:24:35 PM UTC+2, Dmitry Vyukov wrote:

    Objective reasons:
    - half of the time select starts by trying to receive from bCh, which
    fails but consumes time
    - full-fledged select requires more bookkeeping

    The rest is inefficient implementation. There is a patch that
    substantially speedups selects:
    https://codereview.appspot.com/112990043/
    This patch exists for 7 months, but didn't proceed to master branch yet.
    Why?

    --
    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.
  • Dmitry Vyukov at Mar 5, 2015 at 1:03 pm
    it was not reviewed
    On Thu, Mar 5, 2015 at 3:51 PM, Aliaksandr Valialkin wrote:

    On Thursday, March 5, 2015 at 1:24:35 PM UTC+2, Dmitry Vyukov wrote:


    Objective reasons:
    - half of the time select starts by trying to receive from bCh, which
    fails but consumes time
    - full-fledged select requires more bookkeeping

    The rest is inefficient implementation. There is a patch that
    substantially speedups selects:
    https://codereview.appspot.com/112990043/

    This patch exists for 7 months, but didn't proceed to master branch yet.
    Why?

    --
    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.
    --
    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.
  • Dan Kortschak at Mar 5, 2015 at 7:21 pm
    Will you try again?
    On 05/03/2015, at 11:33 PM, "'Dmitry Vyukov' via golang-nuts" wrote:

    it was not reviewed
    --
    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.
  • Dmitry Vyukov at Mar 6, 2015 at 7:09 am
    If somebody will review it, then sure.
    On Thu, Mar 5, 2015 at 10:21 PM, Dan Kortschak wrote:

    Will you try again?

    On 05/03/2015, at 11:33 PM, "'Dmitry Vyukov' via golang-nuts" <
    golang-nuts@googlegroups.com> wrote:
    it was not reviewed
    --
    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.
  • Dan Kortschak at Mar 6, 2015 at 9:05 am
    My runtime internals understanding is not up to reviewing this, but I would like to see these improvements.
    On 06/03/2015, at 5:40 PM, "'Dmitry Vyukov' via golang-nuts" wrote:

    If somebody will review it, then sure.
    --
    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.
  • Silviunotathome at Mar 9, 2015 at 3:39 pm
    As someone else without the internals know-how, but a huge fan of Dmitry's
    overall behind-the-scene optimization efforts, can we hope to have this in
    time for 1.5 ?
    Or it's being deemed secondary priority ?

    cheers,
    s.
    On Friday, March 6, 2015 at 4:05:35 AM UTC-5, kortschak wrote:

    My runtime internals understanding is not up to reviewing this, but I
    would like to see these improvements.

    On 06/03/2015, at 5:40 PM, "'Dmitry Vyukov' via golang-nuts" <
    golan...@googlegroups.com <javascript:>> wrote:
    If somebody will review it, then sure.
    --
    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.
  • Dmitry Vyukov at Mar 10, 2015 at 7:42 am
    +Russ, can you answer this question?
    It is about https://codereview.appspot.com/112990043/

    I can update the patch iff it will be reviewed before it get rotten again.

    On Mon, Mar 9, 2015 at 6:39 PM, wrote:
    As someone else without the internals know-how, but a huge fan of Dmitry's
    overall behind-the-scene optimization efforts, can we hope to have this in
    time for 1.5 ?
    Or it's being deemed secondary priority ?

    cheers,
    s.
    On Friday, March 6, 2015 at 4:05:35 AM UTC-5, kortschak wrote:

    My runtime internals understanding is not up to reviewing this, but I
    would like to see these improvements.

    On 06/03/2015, at 5:40 PM, "'Dmitry Vyukov' via golang-nuts"
    wrote:
    If somebody will review it, then sure.
    --
    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.
  • Russ Cox at Mar 11, 2015 at 12:26 am

    On Tue, Mar 10, 2015 at 3:41 AM, Dmitry Vyukov wrote:

    +Russ, can you answer this question?
    It is about https://codereview.appspot.com/112990043/

    I can update the patch iff it will be reviewed before it get rotten again.
    I am very much in favor of the idea of locking just one channel at a time.
    It looks to me like the PTAL was just before the release freeze and I
    didn't get to it in time, and then we moved off of Rietveld. My apologies
    for all that.

    Please do recreate the CL for our current world and send it to me, Keith,
    and Rob.

    Thanks.
    Russ

    --
    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.
  • Dmitry Vyukov at Mar 13, 2015 at 10:35 am

    On Wed, Mar 11, 2015 at 3:26 AM, Russ Cox wrote:
    On Tue, Mar 10, 2015 at 3:41 AM, Dmitry Vyukov wrote:

    +Russ, can you answer this question?
    It is about https://codereview.appspot.com/112990043/

    I can update the patch iff it will be reviewed before it get rotten again.

    I am very much in favor of the idea of locking just one channel at a time.
    It looks to me like the PTAL was just before the release freeze and I didn't
    get to it in time, and then we moved off of Rietveld. My apologies for all
    that.
    The change was waiting for review for 1.5 months before release freeze
    (or blocked on the factored out part in
    https://codereview.appspot.com/110580043 which was also waiting for
    review).
    Now I need to super carefully port 450 lines of code line-by-line one
    more time...
    Please do recreate the CL for our current world and send it to me, Keith,
    and Rob.

    Thanks.
    Russ
    --
    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
postedMar 5, '15 at 11:14a
activeMar 13, '15 at 10:35a
posts11
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase