It looks all stacks start from 2048 bytes,
even if gc has estimated the start function of a goroutine will use much 
larger stack.
Why not initialize the stack with a large size as needed?

For example:

package main

const N = 300 * 1024 * 1024
var v byte = 123

func f(c chan struct{}) {
    var s = []byte{N: 0}
    for i := range s { s[i] = v }
    println(s[v])
    close(c)
}

func main() {
    c := make(chan struct{})
    go f(c)
    <-c
}

-gcflags=-S shows the function f will use 314572840 bytes on stack:

TEXT    "".f(SB), ABIInternal, $314572840-8

But the stack of the new goroutine still starts from 2048 bytes.

On Wednesday, June 30, 2021 at 3:25:59 AM UTC-4 tapi...@gmail.com wrote:

>
> It looks this line 
> https://github.com/golang/go/blob/master/src/runtime/stack.go#L1078 never 
> gets executed.
>
> An example:
>
> package main
>
> const N = 512 * 1024 * 1024 // 500M
> var v byte = 123
>
> func main() {
>     var s = []byte{N: 0}
>     for i := range s { s[i] = v }
>     println(s[v]) 
> }
>
> I added a println line: println (oldsize, "=>", newsize)
> at L#1078: 
> https://github.com/golang/go/blob/master/src/runtime/stack.go#L1078
> and got the following output:
>
> 2048 => 4096
> 4096 => 8192
> 8192 => 16384
> 16384 => 32768
> 32768 => 65536
> 65536 => 131072
> 131072 => 262144
> 262144 => 524288
> 524288 => 1048576
> 1048576 => 2097152
> 2097152 => 4194304
> 4194304 => 8388608
> 8388608 => 16777216
> 16777216 => 33554432
> 33554432 => 67108864
> 67108864 => 134217728
> 134217728 => 268435456
> 268435456 => 536870912
> 536870912 => 1073741824
> runtime: goroutine stack exceeds 1000000000-byte limit
> runtime: sp=0xc0400dff70 stack=[0xc0200e0000, 0xc0400e0000]
> fatal error: stack overflow
>
>

-- 
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/d3d2ea73-fb06-4b9d-85a8-13c79659aab9n%40googlegroups.com.

Reply via email to