Skip to content

cmd/compile: not inlined closures can't inline inner closures #41988

Closed

Description

@DmitriyMV

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

$ go version
go version go1.15 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

Wrote code:

package go_closures

func callInt(val int) int {
	getInt := func() int { return val }
	return callFn(getInt)
}

func callFn(getInt func() int) int {
	outer := func() int { return getInt() }
	return globalFunc(outer)
}

var globalFunc = func(f func() int) int {
	return f()
}

What did you expect to see?

getInt is inlined into outer. outer escapes to the heap.

What did you see instead?

getInt isn't inlined into outer. Both outer and getInt escape to the heap.

Activity

  1. DmitriyMV commented on Oct 15, 2020

    @DmitriyMV
    ContributorAuthor

    The main problem here is that getInt closure escapes when it can be folded into outer (even with call).

  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Oct 15, 2020
  3. mdempsky commented on Oct 18, 2020

    @mdempsky
    Contributor

    The issue here is we don't currently support inlining functions that contain closures. We'd first need to inline callFn into callInt to be able to inline getInt into outer.

    Escape analysis is a red herring here. Escape analysis runs after inlining and doesn't affect inlining decisions.

  4. mdempsky commented on Jan 18, 2021

    @mdempsky
    Contributor

    /cc @danscales , who's working on making functions that contain closures inlinable

  5. danscales commented on Jan 18, 2021

    @danscales
    Contributor

    Yes, I will look at this bug, after I submit the base change that allows inlining of functions with closures.

  6. DmitriyMV commented on Aug 17, 2021

    @DmitriyMV
    ContributorAuthor

    Still reproduces on 1.17:

    package main
    
    import (
    	"fmt"
    	"testing"
    )
    
    func callInt(val int) int {
    	getInt := func() int { return val }
    	return callFn(getInt)
    }
    
    func callFn(getInt func() int) int {
    	outer := func() int { return getInt() }
    	return globalFunc(outer)
    }
    
    var globalFunc = func(f func() int) int {
    	return f()
    }
    
    func main() {
    	result := testing.Benchmark(Bench)
    	fmt.Println("result.AllocsPerOp()", result.AllocsPerOp())
    }
    
    func Bench(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		callInt(i)
    	}
    }
    
    
  7. self-assigned this
    on Aug 17, 2021
  8. danscales commented on Aug 17, 2021

    @danscales
    Contributor

    Thank you, will take a look if any improvement is possible for 1.18.

  9. added this to the Unplanned milestone on Aug 27, 2022
  10. DmitriyMV commented on Oct 12, 2024

    @DmitriyMV
    ContributorAuthor

    Looks like it's fixed on Go 1.23 - there is now only two allocations for variable and for function literal.

  11. locked and limited conversation to collaborators on Oct 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions