Can you force Golang compiler to inline function if the only reason why the function is not being inlined is exceeding inline budget? There is no defer nor any other statements that prevent compiler to inline function - only budget limit. Or if you can't, is it possible to change the budget from 80 to let's say 160? Or maybe do you know any patterns that may help me to split it somehow.
I've got function like this:
func New() *Foo{
f := &Foo{}
f.Init()
return f
}
It's a little more complicated so it exceeds 80 budget - and because of that, it is 10x slower than it could be. It should be inlined, but not manually so the compiler can also apply some optimizations.
CodePudding user response:
As you figured out, the short answer is "no, not without modifying the compiler", but this is likely to change in the future. Uber has been doing work with profile-guided optimization, and today published a pull request containing that work. This will take a little time to digest (I work on the compiler, we're trying to get all our ducks in a row for the 1.18 release, which is big because it contains generics) but it is safe to say that we're interested. The soonest this could appear is in 1.19, which is scheduled for 8ish months from now.
Depending on the cause of your slowdown, it might be profitable to mark f.Init()
//go:noinline
, which then allows New
in your example to be inlined. If that allocation is not leaked to the heap in the caller, then doing that inline (instead of inlining f.Init()
) exposes the ability to do a stack allocation, and you can save some time that way. Be sure to mark the "noinline" with a comment explaining why you did it :-).
You can also tinker with the compiler, but you didn't hear that from me (and any bugs that you report against a tinkered compiler by default get the stink-eye).