I tried to find a way to initialize the size of the stack of a new created goroutine in advance to avoid several stack copies. Before this fix, the efficiencies of the two new goroutines are almost the same. After the fix, the second goroutine uses much less time than the first one.
package main import "fmt" import "time" const N = 1024 * 1024 var n byte = 123 func f(v [N]byte, n int) { if n > 0 { f(v, n-1) } } func main() { var c = make(chan time.Duration, 1) go func() { start := time.Now() f([N]byte{}, 32) c <- time.Since(start) }() fmt.Println(<-c) go func() { start := time.Now() if n == 0 { var a, b, c, d, e, f, g [N*10]byte n = a[n] + b[n] + c[n] + d[n] + e[n] + f[n] + g[n] } f([N]byte{}, 32) c <- time.Since(start) }() fmt.Println(<-c) } On Thursday, July 1, 2021 at 2:23:45 AM UTC-4 axel.wa...@googlemail.com wrote: > Okay, *now* I get what you are trying to say. I agree that it seems > inefficient to call it more than once, which is why the code tries to > optimize for that. I don't know why that optimization doesn't trigger in > your case - you might want to try and investigate that. There might be a > good reason why it doesn't (for example, I'll note that your code might be > using the system stack, which, AIUI, is special). > > As always, if you are experiencing a real problem due to this, you might > want to open an issue. > > On Thu, Jul 1, 2021 at 3:04 AM tapi...@gmail.com <tapi...@gmail.com> > wrote: > >> >> >> On Wednesday, June 30, 2021 at 8:46:19 PM UTC-4 axel.wa...@googlemail.com >> wrote: >> >>> On Thu, Jul 1, 2021 at 2:34 AM tapi...@gmail.com <tapi...@gmail.com> >>> wrote: >>> >>>> >>>> >>>> On Wednesday, June 30, 2021 at 11:56:45 AM UTC-4 Brian Candler wrote: >>>> >>>>> So I think what you're asking is, "under what scenario does the code >>>>> in L1066-1069 >>>>> <https://github.com/golang/go/blob/d19a53338fa6272b4fe9c39d66812a79e1464cd2/src/runtime/stack.go#L1065-L1070> >>>>> >>>>> get run?" - is that right? >>>> >>>> >>>> Almost true. >>>> >>>> Whether or not it should run, growing the stack from 2048 to 512M in >>>> 18+ steps looks not right. >>>> >>> >>> Why? 2048 • 2^18 = 2^11 • 2^18 = 2^29 = 536870912. >>> Seems like exactly the expected result. >>> >> >> It looks each step calls copystack once. >> Isn't one step more efficient? >> >> >>> >>> >>>> >>>> >>>>> >>>>> On Wednesday, 30 June 2021 at 14:21:21 UTC+1 tapi...@gmail.com wrote: >>>>> >>>>>> Sorry, I post the wrong anchor. It is line 1068: >>>>>> https://github.com/golang/go/blob/d19a53338fa6272b4fe9c39d66812a79e1464cd2/src/runtime/stack.go#L1068 >>>>>> >>>>>> On Wednesday, June 30, 2021 at 5:08:30 AM UTC-4 Brian Candler wrote: >>>>>> >>>>>>> On Wednesday, 30 June 2021 at 08:25:59 UTC+1 tapi...@gmail.com >>>>>>> wrote: >>>>>>> >>>>>>>> >>>>>>>> It looks this line >>>>>>>> https://github.com/golang/go/blob/master/src/runtime/stack.go#L1078 >>>>>>>> never gets executed. >>>>>>>> >>>>>>>> >>>>>>> Can you quote the line you're referring to? Line numbers can shift >>>>>>> up and down as commits are made to the master branch. Right now, L1078 >>>>>>> is a >>>>>>> blank line. >>>>>>> >>>>>> -- >>>> >>> 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. >>>> >>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/golang-nuts/5fab5a6e-5dd5-4df2-8b31-4a51aa825f92n%40googlegroups.com >>>> >>>> <https://groups.google.com/d/msgid/golang-nuts/5fab5a6e-5dd5-4df2-8b31-4a51aa825f92n%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >> 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. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/a06d022b-06c2-4379-b33b-56886ebaf1dbn%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/a06d022b-06c2-4379-b33b-56886ebaf1dbn%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/42aabf7f-7aa5-43f9-915b-f49f41f8856fn%40googlegroups.com.