???????????μ?boost.context??cc????????????????????libcopp???Щ?????????????????????????????????????????????????????Щ????????????????????????????????????????????L1cachemiss???????檔
???????????????????????????v2?汾?????????????????????????????????????????????????????????????????cppcheck??clang-analyzer????????????д????dev?????????????????????????Э???????£?benchmark????????????????????????1/3??
????????valgrind??perf??????????????????????L1cachemiss?????68%???????v2????L1cachemiss????98%??????????????Э???Э??????????Э???????actor????malloc??????????????v2??????????????????????????????????????malloc????????????????2??Э????????actor????????÷??????????L1cache???????????64????????????????L1cachemiss??????????benchmark????????????????????????????????????λ???????????????????
????????????????????????benchmark???????????????ù????е?????ο????????????????????????ó????м??????cachemiss???????????????????????????л?????????
????????????????go?????goroutine?????????????????п????л????Cachemiss??????????????????????????????????????????go?????????????????????????????????Щ?????????????????????????????
????goroutine???????
???????????????libcopp??????????????benchmark?????????:
package main

import (
    "fmt"
    "os"
    "strconv"
    "time"
)

func runCallback(in?? out chan int64) {
    for n?? ok := <-in; ok; n?? ok = <-in {
        out <- n
    }
}

func runTest(round int?? coroutineNum?? switchTimes int64) {
    fmt.Printf("##### Round: %v "?? round)
    start := time.Now()
    channelsIn?? channelsOut := make([]chan int64?? coroutineNum)?? make([]chan int64?? coroutineNum)
    for i := int64(0); i < coroutineNum; i++ {
        channelsIn[i] = make(chan int64?? 1)
        channelsOut[i] = make(chan int64?? 1)
    }
    end := time.Now()
    fmt.Printf("Create %v goroutines and channels cost %vns?? avg %vns "?? coroutineNum?? end.Sub(start).Nanoseconds()?? end.Sub(start).Nanoseconds()/coroutineNum)

    start = time.Now()
    for i := int64(0); i < coroutineNum; i++ {
        go runCallback(channelsIn[i]?? channelsOut[i])
    }
    end = time.Now()
    fmt.Printf("Start %v goroutines and channels cost %vns?? avg %vns "?? coroutineNum?? end.Sub(start).Nanoseconds()?? end.Sub(start).Nanoseconds()/coroutineNum)

    var sum int64 = 0
    start = time.Now()
    for i := int64(0); i < switchTimes; i++ {
        for j := int64(0); j < coroutineNum; j++ {
            channelsIn[j] <- 1
            sum += <-channelsOut[j]
        }
    }
    end = time.Now()
    fmt.Printf("Switch %v goroutines for %v times cost %vns?? avg %vns "?? coroutineNum?? sum?? end.Sub(start).Nanoseconds()?? end.Sub(start).Nanoseconds()/sum)

    start = time.Now()
    for i := int64(0); i < coroutineNum; i++ {
        close(channelsIn[i])
        close(channelsOut[i])
    }
    end = time.Now()
    fmt.Printf("Close %v goroutines cost %vns?? avg %vns "?? coroutineNum?? end.Sub(start).Nanoseconds()?? end.Sub(start).Nanoseconds()/coroutineNum)
}

func main() {
    var coroutineNum?? switchTimes int64 = 30000?? 1000

    fmt.Printf("### Run: ")
    for _?? v := range os.Args {
        fmt.Printf(" "%s""?? v)
    }
    fmt.Printf(" ")

    if (len(os.Args)) > 1 {
        v?? _ := strconv.Atoi(os.Args[1])
        coroutineNum = int64(v)
    }

    if (len(os.Args)) > 2 {
        v?? _ := strconv.Atoi(os.Args[2])
        switchTimes = int64(v)
    }

    for i := 1; i <= 5; i++ {
        runTest(i?? coroutineNum?? switchTimes)
    }
}
?????????£?
PS D:projs estgo> . est_goroutine.exe
### Run:  "D:projs estgo est_goroutine.exe"
##### Round: 1
Create 30000 goroutines and channels cost 6515200ns?? avg 217ns
Start 30000 goroutines and channels cost 79505000ns?? avg 2650ns
Switch 30000 goroutines for 30000000 times cost 42225426300ns?? avg 1407ns
Close 30000 goroutines cost 15017500ns?? avg 500ns
##### Round: 2
Create 30000 goroutines and channels cost 19868200ns?? avg 662ns
Start 30000 goroutines and channels cost 22487700ns?? avg 749ns
Switch 30000 goroutines for 30000000 times cost 44709165100ns?? avg 1490ns
Close 30000 goroutines cost 15559000ns?? avg 518ns
##### Round: 3
Create 30000 goroutines and channels cost 3999700ns?? avg 133ns
Start 30000 goroutines and channels cost 17508400ns?? avg 583ns
Switch 30000 goroutines for 30000000 times cost 50535999000ns?? avg 1684ns
Close 30000 goroutines cost 36289900ns?? avg 1209ns
##### Round: 4
Create 30000 goroutines and channels cost 5999600ns?? avg 199ns
Start 30000 goroutines and channels cost 44500300ns?? avg 1483ns
Switch 30000 goroutines for 30000000 times cost 45678842800ns?? avg 1522ns
Close 30000 goroutines cost 13005600ns?? avg 433ns
##### Round: 5
Create 30000 goroutines and channels cost 5000000ns?? avg 166ns
Start 30000 goroutines and channels cost 14001000ns?? avg 466ns
Switch 30000 goroutines for 30000000 times cost 47485810100ns?? avg 1582ns
Close 30000 goroutines cost 17999800ns?? avg 599ns
??????????????????Windows???????????????Linux?????????????????Щ?????????????????????????libcopp??Linux?????????Windows?o??????????????????????????????£??????????libcopp?????????
?????????????go?????????Э??乲????????????????????libcopp???????????????????????????chan?????????????????????goroutine???л???????????????????????????1us??????о?go????????????????goroutine??????????????????????????????
????????goroutine????濪????С??30000??goroutine???????ò?300MB??
????libcopp??????????????
??????????????Э?????????л???????????
  ###################### task (stack using stack pool) ###################
  ########## Cmd: .sample_benchmark_task_stack_pool.exe 30000 1000 64
  ### Round: 1 ###
  create 30000 task?? cost time: 0 s?? clock time: 104 ms?? avg: 3466 ns
  switch 30000 tasks 30000000 times?? cost time: 18 s?? clock time: 18500 ms?? avg: 616 ns
  remove 30000 tasks?? cost time: 0 s?? clock time: 28 ms?? avg: 933 ns
  ### Round: 2 ###
  create 30000 task?? cost time: 0 s?? clock time: 44 ms?? avg: 1466 ns
  switch 30000 tasks 30000000 times?? cost time: 19 s?? clock time: 18341 ms?? avg: 611 ns
  remove 30000 tasks?? cost time: 0 s?? clock time: 29 ms?? avg: 966 ns
  ### Round: 3 ###
  create 30000 task?? cost time: 0 s?? clock time: 44 ms?? avg: 1466 ns
  switch 30000 tasks 30000000 times?? cost time: 18 s?? clock time: 18188 ms?? avg: 606 ns
  remove 30000 tasks?? cost time: 0 s?? clock time: 28 ms?? avg: 933 ns
  ### Round: 4 ###
  create 30000 task?? cost time: 0 s?? clock time: 44 ms?? avg: 1466 ns
  switch 30000 tasks 30000000 times?? cost time: 18 s?? clock time: 18267 ms?? avg: 608 ns
  remove 30000 tasks?? cost time: 0 s?? clock time: 28 ms?? avg: 933 ns
  ### Round: 5 ###
  create 30000 task?? cost time: 0 s?? clock time: 44 ms?? avg: 1466 ns
  switch 30000 tasks 30000000 times?? cost time: 19 s?? clock time: 18772 ms?? avg: 625 ns
  remove 30000 tasks?? cost time: 0 s?? clock time: 26 ms?? avg: 866 ns
???????????£?libcopp???л?????С???????????????????????????????????????????????????????£????涼???????????????????????????С?????
???????????v2??????????????л????????????????Щ??????????Linux???????????????????????????汾????????Linux?????????????????1us??v2?????500ns???л????????????v2?汾?????300-400ns????
????????
????go????????????????????goroutine???????????????ü??????????????????????????????Э????????????go???????????????????????????????????????????????Щ??????????????????????????????????ο?libcopp???????libcopp??v2?汾?????????????Щ???????????沼??????????????L1 Cache?Ч????????????????????????merge??master????????????https://github.com/owt5008137/libcopp??v2?????
?????????λ?????и??????????????????????????????????????????????????????????????????÷????????libcopp???λ??????????????????????????????????????Э????????????????????????????????????????????Щ??????????????塣????????Щ???????????????????????л?????????????????????????????????????call_in_stack??????????????????????????????????