Navigation Menu

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

cmd/compile: recognize x[:] of global array x as static data #7651

Open
mpvl opened this issue Mar 27, 2014 · 6 comments
Open

cmd/compile: recognize x[:] of global array x as static data #7651

mpvl opened this issue Mar 27, 2014 · 6 comments
Milestone

Comments

@mpvl
Copy link
Contributor

mpvl commented Mar 27, 2014

If an array is defined in the global scope and concerted to a slice in an unused struct
literal, it will not get garbage collected by the linker.

See code below to reproduce the problem.

go version devel +0134c7020c40 Wed Mar 26 15:23:31 2014 -0700 darwin/amd64

===BEGIN===
package main

func main() {
}

type foo struct {
    arr []int
}

var f = foo{
    arr: a[:], // Remove this line and a will get collected.
}

var a = [1000111]int{3} // Removing the 3 will cause the array to be collected.

===END===
@rsc
Copy link
Contributor

rsc commented Mar 27, 2014

Comment 1:

Are you saying that you have to remove both lines to have 'a' be removed as dead code by
the linker, or does removing either one suffice?

@gopherbot
Copy link

Comment 2 by mpvl@audacitycapital.com:

No, either one works.
More importantly.  Just writing var a as a slice works as well (see code below).  So
there is a workaround. 
// This works:
var f = foo{
    arr: a, 
}
var a = []int{3}

@rsc
Copy link
Contributor

rsc commented Apr 24, 2014

Comment 4:

Removing the use of a[:] lets the linker drop the variable, no matter what. That slice
operation happens in a generated func init either way. If we recognized that in the
'init->static' converter it would always get collected. I've changed the bug report to
track that.
The confusion about {} vs {3} happens because if you use {} the binary gets smaller,
because 'a' is moved into the bss. But it's still there if and only if a[:] is used in
the original.
Here are four test cases showing the variations from the original report:
g% cat x1.go # original report
package main
func main() {}
type foo struct { arr []int }
var f = foo{arr: a[:]}
var a = [1000111]int{3}
g% cat x2.go # {3} -> {}
package main
func main() {}
type foo struct { arr []int }
var f = foo{arr: a[:]}
var a = [1000111]int{}
g% cat x3.go # foo{a[:]} -> foo{}
package main
func main() {}
type foo struct { arr []int }
var f = foo{}
var a = [1000111]int{3}
g% cat x4.go # {3} -> {}, foo{a[:]} -> foo{}
package main
func main() {}
type foo struct { arr []int }
var f = foo{}
var a = [1000111]int{}
# a still here for both {} and {3}. difference in size is initialized data vs bss.
g% go build x1.go && echo $(go tool nm x1 | grep main.a) $(ls -l x1)
55020 D main.a -rwxr-xr-x 1 rsc 5000 8490784 Apr 24 17:29 x1
g% go build x2.go && echo $(go tool nm x2 | grep main.a) $(ls -l x2)
75ce0 B main.a -rwxr-xr-x 1 rsc 5000 487200 Apr 24 17:29 x2
# removing use of a removes from binary (no nm output)
g% go build x3.go && echo $(go tool nm x3 | grep main.a) $(ls -l x3)
-rwxr-xr-x 1 rsc 5000 483024 Apr 24 17:30 x3
g% go build x4.go && echo $(go tool nm x4 | grep main.a) $(ls -l x4)
-rwxr-xr-x 1 rsc 5000 483024 Apr 24 17:30 x4

Labels changed: added release-go1.4, removed release-go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Sep 15, 2014

Comment 5:

Labels changed: added release-go1.5, removed release-go1.4.

@griesemer
Copy link
Contributor

Comment 6:

Labels changed: added repo-main.

@bradfitz bradfitz modified the milestone: Go1.5 Dec 16, 2014
@rsc rsc removed accepted labels Apr 14, 2015
@rsc rsc changed the title cmd/ld: recognize x[:] of global array x as static data cmd/gc: recognize x[:] of global array x as static data Apr 26, 2015
@rsc rsc changed the title cmd/gc: recognize x[:] of global array x as static data cmd/compile: recognize x[:] of global array x as static data Jun 8, 2015
@rsc
Copy link
Contributor

rsc commented Jun 29, 2015

Too late for Go 1.5.

@rsc rsc modified the milestones: Go1.6Early, Go1.5 Jun 29, 2015
@rsc rsc modified the milestones: Unplanned, Go1.6Early Nov 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants