ݺߣ

ݺߣShare a Scribd company logo
 source code W golang
channel
G7
Agenda
 A PR title : runtime: speed up receive
on empty closed channel
 How channel work
 How the PR do to improve performance
 Analysis
A PR title : runtime: speed up receive
on empty closed channel
runtime: speed up receive on empty closed channel
Currently, nonblocking receive on an open channel is about
700 times faster than nonblocking receive on a closed channel.
This change makes closed channels equally fast.
How channel work
channel mechanism: circular queue
channel mechanism: block
goroutine would be blocked if executing channel operations.
 v <- chan
 chan <- v
channel mechanism: non-block
select {
case msg := <-chan
fmt.Println(Receive msg from channel)
default:
fmt.Println(No message received)
}
channel mechanism: send to full channel
 Keep data in goroutine
 Add goroutine to sendq
channel mechanism: send to full channel
 Add goroutine to recvq
source code: chan <- val
close block full empty recvq return
false false true true false
true - - - panic
false - - false true
false - false true true
false true true true (block until complete)
true
source code: val <- chan
close block empty empty sendq return (select,
received)
false false true true (false, false)
true - - true (true, false)
false - - false (true, true)
- - false true (true, true)
false true true true (block until complete)
(true !close)
How the PR do to improve performance
partial pseudo code in receive: go1.14
if !block && empty && !close
return
lock()
if close && empty
unlock
return true, false
partial pseudo code in receive: cb14bd8
if !block && empty
if !close
return
else
return true, false
lock()
Analysis
Life demo
demo: run benchmark & pporf
QA

More Related Content

Learn channel-from-from-golang-source-code

  • 1. source code W golang channel G7
  • 2. Agenda A PR title : runtime: speed up receive on empty closed channel How channel work How the PR do to improve performance Analysis
  • 3. A PR title : runtime: speed up receive on empty closed channel
  • 4. runtime: speed up receive on empty closed channel Currently, nonblocking receive on an open channel is about 700 times faster than nonblocking receive on a closed channel. This change makes closed channels equally fast.
  • 7. channel mechanism: block goroutine would be blocked if executing channel operations. v <- chan chan <- v
  • 8. channel mechanism: non-block select { case msg := <-chan fmt.Println(Receive msg from channel) default: fmt.Println(No message received) }
  • 9. channel mechanism: send to full channel Keep data in goroutine Add goroutine to sendq
  • 10. channel mechanism: send to full channel Add goroutine to recvq
  • 11. source code: chan <- val close block full empty recvq return false false true true false true - - - panic false - - false true false - false true true false true true true (block until complete) true
  • 12. source code: val <- chan close block empty empty sendq return (select, received) false false true true (false, false) true - - true (true, false) false - - false (true, true) - - false true (true, true) false true true true (block until complete) (true !close)
  • 13. How the PR do to improve performance
  • 14. partial pseudo code in receive: go1.14 if !block && empty && !close return lock() if close && empty unlock return true, false
  • 15. partial pseudo code in receive: cb14bd8 if !block && empty if !close return else return true, false lock()
  • 17. Life demo demo: run benchmark & pporf
  • 18. QA