Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape analysis analogy seems not correct #30554

Closed
ramvasanth opened this issue Mar 4, 2019 · 6 comments
Closed

Escape analysis analogy seems not correct #30554

ramvasanth opened this issue Mar 4, 2019 · 6 comments

Comments

@ramvasanth
Copy link

ramvasanth commented Mar 4, 2019

What version of Go are you using (go version)?

go version go1.11.4 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\rama.moorthy\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\rama.moorthy\
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\RAMA~1.MOO\AppData\Local\Temp\go-build138480882=/tmp/go-build -gno-record-gcc-switches

What did you do?

escape analysis says that slice array b is moved to heap , but after printing the address of slice array , its address is adjacent to stack variable i address.

This is the program

image

https://play.golang.org/p/wGjw7wPQgu9
-->

What did you expect to see?

i expect to see the address of b should not be adjacent to stack address

What did you see instead?

image

@randall77
Copy link
Contributor

randall77 commented Mar 4, 2019

The address of b is a stack address. The address of the first element of the slice is &b[0], not &b.
You might want to read https://blog.golang.org/go-slices-usage-and-internals

@ramvasanth
Copy link
Author

Thanks @randall77 .

I read the slice internal doc. but still why slice is allocated in stack rather than in heap. escape analysis says it will allocate into heap.

@randall77
Copy link
Contributor

The backing store for the slice (the array) is allocated on the heap. The slice itself (the 3-word structure consisting of pointer to the array, a length, and a capacity) is allocated on the stack.

When the escape analysis says "b escapes to heap", it means that the values in b are written to the heap. So anything referenced by b must be in the heap also. b itself need not be.

If b itself needs to be on the heap, escape analysis will say "&b escapes to heap".

@ramvasanth
Copy link
Author

Hi @randall77 , Yes you are right, address of b[0] should refer to heap address(lower address in virtual memory). but its not pointing

address of i --> 0xc00007ff50

address of b[0], and b[1] --> 0xc00005a070, 0xc00005a071

my objective is that i wanna make sure that backing array is pointing to heap memory according to escape analysis.

Thanks.

@randall77
Copy link
Contributor

Your assumption that heap addresses are lower in virtual memory is incorrect. Pages of Go heap and pages of goroutine stacks are intermingled.

@ramvasanth
Copy link
Author

@randall77 , Thanks... that clears all my questions.

@golang golang locked and limited conversation to collaborators Mar 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants