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: inlining callback functions #70260

Open
tulzke opened this issue Nov 8, 2024 · 5 comments
Open

cmd/compile: inlining callback functions #70260

tulzke opened this issue Nov 8, 2024 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@tulzke
Copy link

tulzke commented Nov 8, 2024

Proposal Details

I'm sorry if this is a duplicate. I honestly tried to find a similar issue

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

go 1.23

  1. What operating system and processor architecture are you using?

MacOS

  1. What did you do?

I wrote simple generic functions to simplify the work.
Example:

func SeparateIntoTwo[T any](slice []T, isFirstGroupFunc func(T) bool) [2][]T {
	result := [2][]T{
		make([]T, 0, len(slice)/2),
		make([]T, 0, len(slice)/2),
	}
	for _, elem := range slice {
		if isFirstGroupFunc(elem) {
			result[0] = append(result[0], elem)
		} else {
			result[1] = append(result[1], elem)
		}
	}

	return result
}

func foo() {
    slice := []SomeStruct{...}
    // We divide slice here into two groups: deleted and not.
    // func(s SomeStruct) IsDeleted() bool
    SeparateIntoTwo(slice, SomeStruct.IsDeleted)
}

Most of them accept callback functions for various groupings and aggregations.
I compared the performance with the same function, but instead of a callback, it used a method.
I found that the performance of functions with callback is slightly lower than without them.

Benchmark_SeparateIntoTwo/callback_100000
Benchmark_SeparateIntoTwo/callback_100000-8     	    3776	    322542 ns/op	 2408448 B/op	       2 allocs/op
Benchmark_SeparateIntoTwo/method_100000
Benchmark_SeparateIntoTwo/method_100000-8       	    4939	    245211 ns/op	 2408448 B/op	       2 allocs/op

The reason is that the methods were inline by the compiler, while the callback remained unchanged.
Moreover, the callback is not inline even when the SeparateIntoTwo function itself is inline. At this point, the compiler had information about the complexity of the function being passed as an argument.

  1. What did you expect to see?

An attempt to inline a callback function by the compiler in cases where the function taking the callback as an argument is also inline

  1. What did you see instead?

Lack of inlining

@tulzke tulzke added the Proposal label Nov 8, 2024
@gopherbot gopherbot added this to the Proposal milestone Nov 8, 2024
@tulzke tulzke changed the title proposal: import/path: proposal title proposal: inlining callback functions Nov 8, 2024
@timothy-king timothy-king changed the title proposal: inlining callback functions cmd/compile: inlining callback functions Nov 8, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 8, 2024
@timothy-king
Copy link
Contributor

Not a proposal https://github.com/golang/proposal#readme. Taking out of the proposal process.

@gabyhelp
Copy link

gabyhelp commented Nov 8, 2024

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@randall77
Copy link
Contributor

@dr2chase

@cherrymui cherrymui modified the milestones: Proposal, Unplanned Nov 11, 2024
@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 11, 2024
@dr2chase
Copy link
Contributor

I think this is the same root cause as #69411 ; there's a glitch in the "constant propagation" for some-but-not-all closures.

@dr2chase
Copy link
Contributor

If you could include that benchmark here, or put it up on the playground (won't run as a benchmark but that is okay with me) then I could add it to the collection of "is this really all the same problem?" bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

8 participants