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: avoid closure for captured dynamic constant #5370

Open
lexprfuncall opened this Issue Apr 29, 2013 · 8 comments

Comments

Projects
None yet
3 participants
@lexprfuncall
Copy link

lexprfuncall commented Apr 29, 2013

The compiler is not very smart about compiling away closures.  The following function
needlessly allocates a closure at runtime

func main() {
  s := ""
  f := func () { println(s) }
  g(f)
}

This example should succumb to closure analysis.  The closure should be eliminated at
compile time and replaced by an ordinary function.
@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Apr 29, 2013

Comment 1:

You have not shown us a complete program. The correct compilation here depends on what g
does with f.
If I add func g(func()) with no body, then in the absence of information the compiler
allocates a closure, correctly.
If I add func g(func()) {}, then since the argument does not escape, the compiler makes
a function on the stack, again correctly.
So I think this is working.
@lexprfuncall

This comment has been minimized.

Copy link
Author

lexprfuncall commented Apr 29, 2013

Comment 2:

I do not believe the body of g is important for demonstration purposes.
Put another way, the compiler should recognize that the program is equivalent to the
following
func f() {
  println("")
}
func main() {
  g(f)
}
and avoid emitting code to allocate a closure either on the stack or on the heap. 
Instead, the compiler should arrange to have f point to a non-closure function similar
to what I have shown above.
@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Apr 29, 2013

Comment 4:

Oh, you want the closure to go away because s is a constant.
That's a bit less compelling but sure.

Labels changed: added priority-later, removed priority-triage.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Jul 30, 2013

Comment 5:

Labels changed: added go1.3.

@robpike

This comment has been minimized.

Copy link
Contributor

robpike commented Aug 20, 2013

Comment 6:

Labels changed: removed go1.3.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Nov 27, 2013

Comment 7:

Labels changed: added go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Dec 4, 2013

Comment 8:

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

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Dec 4, 2013

Comment 9:

Labels changed: added repo-main.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc changed the title cmd/gc: avoid closure for captured dynamic constant cmd/compile: avoid closure for captured dynamic constant Jun 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.